posthog-rails 3.20.7 → 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 +4 -4
- data/lib/generators/posthog/templates/posthog.rb +1 -1
- data/lib/posthog/rails/action_cable.rb +220 -0
- data/lib/posthog/rails/error_subscriber.rb +5 -0
- data/lib/posthog/rails/railtie.rb +28 -0
- data/lib/posthog/rails/request_context.rb +5 -2
- data/lib/posthog/rails/streaming_exceptions.rb +96 -0
- data/lib/posthog/rails/version.rb +1 -1
- data/lib/posthog/rails.rb +1 -0
- metadata +5 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 1cb18a06ebc035ab3aadc995985ce0dffca4423d7a3f4354f35505d1728e9838
|
|
4
|
+
data.tar.gz: f2f7532ff5015d52fe4df57bf3d4a18ce1ae823d0373ce92b514774735cf5e53
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
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
|
-
#
|
|
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,34 @@ 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
|
+
|
|
60
|
+
initializer 'posthog.streaming_errors' do
|
|
61
|
+
StreamingExceptions.install
|
|
62
|
+
end
|
|
63
|
+
|
|
36
64
|
# After initialization, set up remaining integrations
|
|
37
65
|
config.after_initialize do |_app|
|
|
38
66
|
# Hook into ActiveJob only if enabled
|
|
@@ -11,8 +11,9 @@ module PostHog
|
|
|
11
11
|
# @api private
|
|
12
12
|
class RequestContext
|
|
13
13
|
# @param app [#call] Rack application.
|
|
14
|
-
def initialize(app)
|
|
14
|
+
def initialize(app, enrich_current_span: true)
|
|
15
15
|
@app = app
|
|
16
|
+
@enrich_current_span = enrich_current_span
|
|
16
17
|
end
|
|
17
18
|
|
|
18
19
|
# @param env [Hash] Rack environment.
|
|
@@ -20,7 +21,9 @@ module PostHog
|
|
|
20
21
|
def call(env)
|
|
21
22
|
request = build_request(env)
|
|
22
23
|
|
|
23
|
-
Internal::Context.with_context(
|
|
24
|
+
Internal::Context.with_context(
|
|
25
|
+
context_data(request), fresh: true, owner: PostHog.client, enrich_current_span: @enrich_current_span
|
|
26
|
+
) do
|
|
24
27
|
@app.call(env)
|
|
25
28
|
end
|
|
26
29
|
end
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module PostHog
|
|
4
|
+
module Rails
|
|
5
|
+
# @api private
|
|
6
|
+
class StreamingExceptions < CaptureExceptions
|
|
7
|
+
module LiveReporter
|
|
8
|
+
private
|
|
9
|
+
|
|
10
|
+
def log_error(exception)
|
|
11
|
+
StreamingExceptions.capture(exception, source: 'rails.live', controller: self)
|
|
12
|
+
super
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
module TemplateReporter
|
|
17
|
+
private
|
|
18
|
+
|
|
19
|
+
def log_error(exception)
|
|
20
|
+
StreamingExceptions.capture(exception, source: 'rails.streaming_template')
|
|
21
|
+
super
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def self.install
|
|
26
|
+
return if @installed
|
|
27
|
+
|
|
28
|
+
ActiveSupport.on_load(:action_controller_live) do
|
|
29
|
+
StreamingExceptions.install_live(self)
|
|
30
|
+
end
|
|
31
|
+
# Rails 8.1.4 added the Live hook to avoid resolving Response too early.
|
|
32
|
+
if Gem::Version.new(::Rails.version) < Gem::Version.new('8.1.4')
|
|
33
|
+
ActiveSupport.on_load(:action_controller) do
|
|
34
|
+
StreamingExceptions.install_live(ActionController::Live)
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
ActiveSupport.on_load(:action_view) do
|
|
38
|
+
body = ActionView::StreamingTemplateRenderer::Body
|
|
39
|
+
body.prepend(TemplateReporter) unless body < TemplateReporter
|
|
40
|
+
end
|
|
41
|
+
@installed = true
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def self.install_live(live)
|
|
45
|
+
return if live < LiveReporter
|
|
46
|
+
|
|
47
|
+
# Older Rails exposes log_error publicly. Keep visibility local to the
|
|
48
|
+
# target instead of changing the shared reporter module.
|
|
49
|
+
reporter = Module.new { include LiveReporter }
|
|
50
|
+
visibility = %i[public protected private].find { |value| live.send("#{value}_method_defined?", :log_error) }
|
|
51
|
+
reporter.send(visibility, :log_error)
|
|
52
|
+
live.prepend(reporter)
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def self.capture(exception, source:, controller: nil)
|
|
56
|
+
return unless PostHog.initialized? && PostHog.client.enabled?
|
|
57
|
+
|
|
58
|
+
new(nil).report(exception, source: source, controller: controller)
|
|
59
|
+
rescue StandardError
|
|
60
|
+
# Reporting must never replace Rails' logging result or exception.
|
|
61
|
+
nil
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def report(exception, source:, controller:)
|
|
65
|
+
return unless should_capture?(exception)
|
|
66
|
+
|
|
67
|
+
env = controller&.request&.env
|
|
68
|
+
if env
|
|
69
|
+
# Live may log after middleware has returned or in another scope.
|
|
70
|
+
# Use its own request, never a retained or unrelated ambient context.
|
|
71
|
+
RequestContext.new(lambda do |_env|
|
|
72
|
+
capture_streaming_exception(exception, source, env)
|
|
73
|
+
end, enrich_current_span: false).call(env)
|
|
74
|
+
else
|
|
75
|
+
capture_streaming_exception(exception, source, env)
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
private
|
|
80
|
+
|
|
81
|
+
def capture_streaming_exception(exception, source, env = nil)
|
|
82
|
+
properties = env ? build_properties(ActionDispatch::Request.new(env), env) : {}
|
|
83
|
+
properties['$exception_source'] = source
|
|
84
|
+
properties['$exception_level'] = 'error'
|
|
85
|
+
PostHog.capture_exception(
|
|
86
|
+
exception,
|
|
87
|
+
env ? extract_distinct_id(env) : nil,
|
|
88
|
+
properties,
|
|
89
|
+
mechanism: { 'type' => 'middleware', 'handled' => false }
|
|
90
|
+
)
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
private_constant :StreamingExceptions
|
|
95
|
+
end
|
|
96
|
+
end
|
data/lib/posthog/rails.rb
CHANGED
|
@@ -6,6 +6,7 @@ require 'posthog/rails/tracing_headers'
|
|
|
6
6
|
require 'posthog/rails/request_metadata'
|
|
7
7
|
require 'posthog/rails/request_context'
|
|
8
8
|
require 'posthog/rails/capture_exceptions'
|
|
9
|
+
require 'posthog/rails/streaming_exceptions'
|
|
9
10
|
require 'posthog/rails/rescued_exception_interceptor'
|
|
10
11
|
require 'posthog/rails/active_job'
|
|
11
12
|
require 'posthog/rails/error_subscriber'
|
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.
|
|
4
|
+
version: 3.22.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- PostHog
|
|
@@ -29,14 +29,14 @@ dependencies:
|
|
|
29
29
|
requirements:
|
|
30
30
|
- - '='
|
|
31
31
|
- !ruby/object:Gem::Version
|
|
32
|
-
version: 3.32.
|
|
32
|
+
version: 3.32.1
|
|
33
33
|
type: :runtime
|
|
34
34
|
prerelease: false
|
|
35
35
|
version_requirements: !ruby/object:Gem::Requirement
|
|
36
36
|
requirements:
|
|
37
37
|
- - '='
|
|
38
38
|
- !ruby/object:Gem::Version
|
|
39
|
-
version: 3.32.
|
|
39
|
+
version: 3.32.1
|
|
40
40
|
description: Automatic exception tracking and instrumentation for Ruby on Rails applications
|
|
41
41
|
using PostHog
|
|
42
42
|
email: engineering@posthog.com
|
|
@@ -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
|
|
@@ -62,6 +63,7 @@ files:
|
|
|
62
63
|
- lib/posthog/rails/request_context.rb
|
|
63
64
|
- lib/posthog/rails/request_metadata.rb
|
|
64
65
|
- lib/posthog/rails/rescued_exception_interceptor.rb
|
|
66
|
+
- lib/posthog/rails/streaming_exceptions.rb
|
|
65
67
|
- lib/posthog/rails/tracing_headers.rb
|
|
66
68
|
- lib/posthog/rails/version.rb
|
|
67
69
|
homepage: https://github.com/PostHog/posthog-ruby
|