posthog-rails 3.21.0 → 3.22.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 80e2400e119100e9916f8ecc8092d96b0d9552596c8c865895cf36e4389f78cb
4
- data.tar.gz: 80c09b13dbafaf6f9a5efe3359e9a02dc4d92175a493f214d885c82f6194c9d8
3
+ metadata.gz: 1cb18a06ebc035ab3aadc995985ce0dffca4423d7a3f4354f35505d1728e9838
4
+ data.tar.gz: f2f7532ff5015d52fe4df57bf3d4a18ce1ae823d0373ce92b514774735cf5e53
5
5
  SHA512:
6
- metadata.gz: 89ebcf81d463b9b3990500f95b0a73dc5c7ba6e3b06d7573a96c7c9299a31ef007ec116649733f6629809be4809b8aea5e774d1ff8ed271f486fcb658f42135f
7
- data.tar.gz: b150e2c4c23ac510bd0b6b50fb64a3c8290bf01503bae5d2795a8c5f214104c7c1ec72a5e47f51442cc35627a71630e4195610cf2532dfb340609cb48df1748b
6
+ metadata.gz: 62379efc38117719a96bb99071337f8d4809fcf46786268cfa578a51894f4ef0d3d1bb048f4c20b11d15062c83cca8af1ca2d8e14f97d570b0cdd1aa5309469f
7
+ data.tar.gz: 6d6236612649dbb5f29f47be866ef39779f5c9430b11bca847a5f69999dc76baeffc5fa30e3fbac034c3e92fbce6820464dbef4e89be2d8cb3a57547f1c62ddb
@@ -11,7 +11,7 @@
11
11
 
12
12
  PostHog::Rails.configure do |config|
13
13
  # Automatically capture exceptions (default: false)
14
- # Set to true to enable automatic exception tracking
14
+ # Includes Action Cable connect/disconnect, channel subscribe/unsubscribe, and channel actions.
15
15
  # config.auto_capture_exceptions = true
16
16
 
17
17
  # Report exceptions that Rails rescues (e.g., with rescue_from) (default: false)
@@ -0,0 +1,220 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'posthog/rails/capture_exceptions'
4
+ require 'posthog/internal/context'
5
+ require 'posthog/rails/request_metadata'
6
+
7
+ module PostHog
8
+ module Rails
9
+ # @api private
10
+ class ActionCableCapture < CaptureExceptions
11
+ def initialize(connection, operation, channel: nil, data: nil)
12
+ super(nil)
13
+ @connection = connection
14
+ @operation = operation
15
+ @channel = channel
16
+ @data = data
17
+ end
18
+
19
+ def call
20
+ with_context do
21
+ yield
22
+ rescue StandardError => e
23
+ dispatch = ActionCableDispatch.current(@connection) if @channel
24
+ if dispatch
25
+ dispatch.defer(self, e)
26
+ else
27
+ capture_exception(e, connection_env)
28
+ end
29
+ raise
30
+ end
31
+ end
32
+
33
+ def with_context(&block)
34
+ Internal::Context.with_context(context_data(connection_env), fresh: true, owner: PostHog.client,
35
+ enrich_current_span: false, &block)
36
+ end
37
+
38
+ def report(exception)
39
+ with_context { capture_exception(exception, connection_env) }
40
+ end
41
+
42
+ private
43
+
44
+ def connection_env
45
+ env = @connection.env if @connection.respond_to?(:env)
46
+ env.is_a?(Hash) ? env : {}
47
+ rescue StandardError
48
+ {}
49
+ end
50
+
51
+ def context_data(env)
52
+ request = ActionDispatch::Request.new(env)
53
+ data = { properties: RequestMetadata.extract(request) }
54
+ return data if PostHog::Rails.config&.use_tracing_headers == false
55
+
56
+ data.merge(
57
+ distinct_id: TracingHeaders.extract_header(request, 'X-POSTHOG-DISTINCT-ID'),
58
+ session_id: TracingHeaders.extract_header(request, 'X-POSTHOG-SESSION-ID')
59
+ )
60
+ rescue StandardError
61
+ {}
62
+ end
63
+
64
+ def capture_exception(exception, env)
65
+ return unless PostHog.initialized? && PostHog.client.enabled?
66
+ return unless should_capture?(exception)
67
+
68
+ PostHog.capture_exception(
69
+ exception,
70
+ extract_distinct_id(env),
71
+ build_cable_properties,
72
+ mechanism: { 'type' => 'middleware', 'handled' => false }
73
+ )
74
+ rescue StandardError => e
75
+ PostHog::Logging.logger.error("Failed to capture Action Cable exception: #{e.class}")
76
+ end
77
+
78
+ def extract_distinct_id(env)
79
+ super
80
+ rescue StandardError
81
+ nil
82
+ end
83
+
84
+ def extract_current_user(controller)
85
+ return super if controller
86
+
87
+ method_name = PostHog::Rails.config&.current_user_method || :current_user
88
+ @connection.send(method_name) if @connection.respond_to?(method_name, true)
89
+ rescue StandardError
90
+ nil
91
+ end
92
+
93
+ def filter_sensitive_params(params)
94
+ return ParameterFilter::EMPTY_HASH unless params.is_a?(Hash)
95
+
96
+ filters = Array(::Rails.application&.config&.filter_parameters)
97
+ config = @connection.config if @connection.respond_to?(:config)
98
+ filters += Array(config.filter_parameters) if config.respond_to?(:filter_parameters)
99
+ ParameterFilter.backend.new(filters.uniq).filter(params)
100
+ end
101
+
102
+ def build_cable_properties
103
+ properties = {
104
+ '$exception_source' => 'rails.action_cable',
105
+ '$exception_level' => 'error',
106
+ '$action_cable_operation' => @operation,
107
+ '$connection_class' => @connection.class.name
108
+ }
109
+ return properties unless @channel
110
+
111
+ properties['$channel_class'] = @channel.class.name
112
+ properties['$channel_params'] = safe_serialize(filter_sensitive_params(@channel.params))
113
+ if @data
114
+ filtered_data = safe_serialize(filter_sensitive_params(@data))
115
+ properties['$action_data'] = filtered_data
116
+ properties['$action'] = filtered_data['action'].presence || 'receive'
117
+ end
118
+ properties
119
+ end
120
+ end
121
+
122
+ # A channel error reaches connection rescue handlers outside the channel boundary.
123
+ # Keep its capture pending only for the current command, not on the shared socket.
124
+ # @api private
125
+ class ActionCableDispatch
126
+ def self.current(connection)
127
+ dispatch = Thread.current[:posthog_cable_dispatch]
128
+ dispatch if dispatch&.connection.equal?(connection)
129
+ end
130
+
131
+ attr_reader :connection, :exception
132
+ attr_accessor :handled
133
+
134
+ def initialize(connection)
135
+ @connection = connection
136
+ end
137
+
138
+ def defer(capture, exception)
139
+ return if @capture
140
+
141
+ @capture = capture
142
+ @exception = exception
143
+ end
144
+
145
+ def call
146
+ previous = Thread.current[:posthog_cable_dispatch]
147
+ Thread.current[:posthog_cable_dispatch] = self
148
+ result = yield
149
+ @capture&.report(@exception) unless handled
150
+ result
151
+ rescue StandardError => e
152
+ @capture&.report(e)
153
+ raise
154
+ ensure
155
+ Thread.current[:posthog_cable_dispatch] = previous
156
+ end
157
+ end
158
+
159
+ # @api private
160
+ module ActionCableSubscriptionsExtensions
161
+ def execute_command(data)
162
+ ActionCableCapture.new(@connection, 'command').with_context do
163
+ ActionCableDispatch.new(@connection).call { super }
164
+ end
165
+ end
166
+ end
167
+
168
+ # @api private
169
+ module ActionCableContext
170
+ private
171
+
172
+ def with_posthog_cable_context(operation, connection: self, channel: nil, data: nil, &block)
173
+ ActionCableCapture.new(connection, operation, channel: channel, data: data).call(&block)
174
+ end
175
+ end
176
+
177
+ # @api private
178
+ module ActionCableConnectionExtensions
179
+ include ActionCableContext
180
+
181
+ def rescue_with_handler(exception)
182
+ result = super
183
+ dispatch = ActionCableDispatch.current(self)
184
+ dispatch.handled = true if result && dispatch&.exception.equal?(exception)
185
+ result
186
+ end
187
+
188
+ private
189
+
190
+ def handle_open
191
+ with_posthog_cable_context('connect') { super }
192
+ end
193
+
194
+ def handle_close
195
+ with_posthog_cable_context('disconnect') { super }
196
+ end
197
+ end
198
+
199
+ # @api private
200
+ module ActionCableChannelExtensions
201
+ include ActionCableContext
202
+
203
+ def subscribe_to_channel
204
+ with_posthog_cable_context('subscribe', connection: connection, channel: self) { super }
205
+ end
206
+
207
+ def unsubscribe_from_channel
208
+ with_posthog_cable_context('unsubscribe', connection: connection, channel: self) { super }
209
+ end
210
+
211
+ def perform_action(data)
212
+ with_posthog_cable_context('action', connection: connection, channel: self, data: data) { super }
213
+ end
214
+ end
215
+
216
+ private_constant :ActionCableCapture, :ActionCableContext,
217
+ :ActionCableConnectionExtensions, :ActionCableChannelExtensions,
218
+ :ActionCableDispatch, :ActionCableSubscriptionsExtensions
219
+ end
220
+ end
@@ -21,6 +21,11 @@ module PostHog
21
21
  return unless PostHog::Rails.config&.auto_capture_exceptions
22
22
  return unless PostHog::Rails.config&.should_capture_exception?(error)
23
23
 
24
+ if defined?(::Sidekiq::JobRetry::Handled) && error.is_a?(::Sidekiq::JobRetry::Handled)
25
+ error = error.cause || error
26
+ return unless PostHog::Rails.config&.should_capture_exception?(error)
27
+ end
28
+
24
29
  distinct_id = context[:user_id] || context[:distinct_id]
25
30
 
26
31
  properties = {
@@ -33,6 +33,30 @@ module PostHog
33
33
  )
34
34
  end
35
35
 
36
+ initializer 'posthog.action_cable' do
37
+ require 'posthog/rails/action_cable'
38
+
39
+ ActiveSupport.on_load(:action_cable_connection) do
40
+ prepend ActionCableConnectionExtensions unless self < ActionCableConnectionExtensions
41
+ subscriptions = ::ActionCable::Connection::Subscriptions
42
+ unless subscriptions < ActionCableSubscriptionsExtensions
43
+ subscriptions.prepend(ActionCableSubscriptionsExtensions)
44
+ end
45
+ end
46
+ ActiveSupport.on_load(:action_cable_channel) do
47
+ prepend ActionCableChannelExtensions unless self < ActionCableChannelExtensions
48
+ end
49
+
50
+ # Rails 5.2 has only the server configuration hook.
51
+ if ::Rails.version.to_f < 6.0
52
+ ActiveSupport.on_load(:action_cable) do
53
+ ::ActionCable::Connection::Base.prepend(ActionCableConnectionExtensions)
54
+ ::ActionCable::Channel::Base.prepend(ActionCableChannelExtensions)
55
+ ::ActionCable::Connection::Subscriptions.prepend(ActionCableSubscriptionsExtensions)
56
+ end
57
+ end
58
+ end
59
+
36
60
  initializer 'posthog.streaming_errors' do
37
61
  StreamingExceptions.install
38
62
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module PostHog
4
4
  module Rails
5
- VERSION = '3.21.0'
5
+ VERSION = '3.22.0'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: posthog-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.21.0
4
+ version: 3.22.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - PostHog
@@ -48,6 +48,7 @@ files:
48
48
  - lib/generators/posthog/templates/posthog.rb
49
49
  - lib/posthog-rails.rb
50
50
  - lib/posthog/rails.rb
51
+ - lib/posthog/rails/action_cable.rb
51
52
  - lib/posthog/rails/active_job.rb
52
53
  - lib/posthog/rails/capture_exceptions.rb
53
54
  - lib/posthog/rails/configuration.rb