posthog-rails 3.24.0 → 3.25.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:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 94abc2b8a286465010b2f0ab2581933e9d2fa68abcdb145e7076193a21cfe854
|
|
4
|
+
data.tar.gz: 8d220b357b4a739bd50b791bce91ccbe053978866875deb8ad69a9d247336d6d
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 9a50c5a41a58e760e75458379d55f3054768b81c413929dfa3177bc33311d139fa61807f69678a6f9ad3ee6b20c5592fea0007f9b480c0a6607ecc6fc0887db6
|
|
7
|
+
data.tar.gz: f140b196bb5487e4e33fb4eda36f6581fb44acc62a7a27ee322da653585cdbe6dd9be4df86b53e03c9d8bc2b6deb5d1fbffc756f31999598b3cbc4fe63fbd561
|
|
@@ -190,6 +190,15 @@ PostHog.init do |config|
|
|
|
190
190
|
# config.test_mode = true if Rails.env.test? || Rails.env.development?
|
|
191
191
|
end
|
|
192
192
|
|
|
193
|
+
# ============================================================================
|
|
194
|
+
# OPTIONAL SIDEKIQ INTEGRATION (Rails 7.1+, Sidekiq 7.0.3+)
|
|
195
|
+
# ============================================================================
|
|
196
|
+
# Install explicitly to report only terminal job failures. Other policies: :all, :dead.
|
|
197
|
+
# require 'posthog/rails/sidekiq'
|
|
198
|
+
# Sidekiq.configure_server do |config|
|
|
199
|
+
# PostHog::Rails::Sidekiq.install(config, error_reporting: :exhausted)
|
|
200
|
+
# end
|
|
201
|
+
|
|
193
202
|
# ============================================================================
|
|
194
203
|
# DEFAULT EXCLUDED EXCEPTIONS
|
|
195
204
|
# ============================================================================
|
|
@@ -225,6 +225,8 @@ module PostHog
|
|
|
225
225
|
# Add serialized job arguments (be careful with sensitive data)
|
|
226
226
|
properties['$job_arguments'] = sanitize_job_arguments(arguments) if arguments.present?
|
|
227
227
|
|
|
228
|
+
return if defined?(PostHog::Rails::Sidekiq) && Sidekiq.defer_capture?(exception, distinct_id, properties)
|
|
229
|
+
|
|
228
230
|
PostHog.capture_exception(
|
|
229
231
|
exception,
|
|
230
232
|
distinct_id,
|
|
@@ -22,6 +22,7 @@ module PostHog
|
|
|
22
22
|
|
|
23
23
|
runner_report = !handled && source == 'application.runner.railties'
|
|
24
24
|
return unless runner_report || PostHog::Rails.config&.should_capture_exception?(error)
|
|
25
|
+
return if defined?(PostHog::Rails::Sidekiq) && Sidekiq.suppress_rails_report?(error, source)
|
|
25
26
|
|
|
26
27
|
if defined?(::Sidekiq::JobRetry::Handled) && error.is_a?(::Sidekiq::JobRetry::Handled)
|
|
27
28
|
error = error.cause || error
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'posthog-rails'
|
|
4
|
+
require 'sidekiq'
|
|
5
|
+
require 'posthog/internal/context'
|
|
6
|
+
|
|
7
|
+
module PostHog
|
|
8
|
+
module Rails
|
|
9
|
+
module Sidekiq
|
|
10
|
+
JOB_SCOPE_KEY = :posthog_sidekiq_job
|
|
11
|
+
INSTALLATION_IVAR = :@posthog_sidekiq_integration
|
|
12
|
+
DEFERRED_REPORT_IVAR = :@posthog_sidekiq_report
|
|
13
|
+
Scope = Struct.new(:installation, :previous)
|
|
14
|
+
|
|
15
|
+
# Install job error reporting on a Sidekiq server configuration.
|
|
16
|
+
# @param config [::Sidekiq::Config]
|
|
17
|
+
# @param error_reporting [Symbol] :all, :exhausted, or :dead.
|
|
18
|
+
# @return [void]
|
|
19
|
+
def self.install(config, error_reporting:)
|
|
20
|
+
unless %i[all exhausted dead].include?(error_reporting)
|
|
21
|
+
raise ArgumentError, 'error_reporting must be :all, :exhausted, or :dead'
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
if Gem::Version.new(::Rails.version) < Gem::Version.new('7.1') ||
|
|
25
|
+
Gem::Version.new(::Sidekiq::VERSION) < Gem::Version.new('7.0.3')
|
|
26
|
+
PostHog::Logging.logger.warn(
|
|
27
|
+
'Sidekiq retry reporting requires Rails 7.1+ and Sidekiq 7.0.3+; leaving existing reporting unchanged.'
|
|
28
|
+
)
|
|
29
|
+
return
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
if (installation = config.instance_variable_get(INSTALLATION_IVAR))
|
|
33
|
+
installation.error_reporting = error_reporting
|
|
34
|
+
return
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
installation = Installation.new(error_reporting)
|
|
38
|
+
config.server_middleware { |chain| chain.prepend(ServerMiddleware, installation) }
|
|
39
|
+
config.capsules.each_value do |capsule|
|
|
40
|
+
# Leave uncached chains lazy so they inherit later global middleware additions.
|
|
41
|
+
capsule.instance_variable_get(:@server_chain)&.prepend(ServerMiddleware, installation)
|
|
42
|
+
end
|
|
43
|
+
config[:error_handlers] = config.error_handlers + [installation.method(:report_error)]
|
|
44
|
+
config[:death_handlers] = config.death_handlers + [installation.method(:report_death)]
|
|
45
|
+
config.instance_variable_set(INSTALLATION_IVAR, installation)
|
|
46
|
+
nil
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# @api private
|
|
50
|
+
def self.in_job?
|
|
51
|
+
scope = Thread.current[JOB_SCOPE_KEY]
|
|
52
|
+
scope&.installation&.enabled?
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# @api private
|
|
56
|
+
def self.defer_capture?(error, distinct_id, properties)
|
|
57
|
+
return false unless in_job?
|
|
58
|
+
return true if error.instance_variable_defined?(DEFERRED_REPORT_IVAR)
|
|
59
|
+
|
|
60
|
+
context = Internal::Context.current
|
|
61
|
+
# Sidekiq invokes its handlers after the job's tracing middleware unwinds.
|
|
62
|
+
properties = PostHog.client.send(:active_trace_properties).merge(properties)
|
|
63
|
+
context_data = { distinct_id: context&.distinct_id, session_id: context&.session_id,
|
|
64
|
+
properties: context&.properties }
|
|
65
|
+
error.instance_variable_set(DEFERRED_REPORT_IVAR, [distinct_id, properties, context_data]) unless error.frozen?
|
|
66
|
+
true
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# @api private
|
|
70
|
+
def self.suppress_rails_report?(error, source)
|
|
71
|
+
scope = Thread.current[JOB_SCOPE_KEY]
|
|
72
|
+
installation = scope&.installation
|
|
73
|
+
# Sidekiq sets its capsule before dispatch, including failures before middleware runs.
|
|
74
|
+
installation ||= Thread.current[:sidekiq_capsule]&.config&.instance_variable_get(INSTALLATION_IVAR)
|
|
75
|
+
return false unless installation&.enabled?
|
|
76
|
+
|
|
77
|
+
source == 'job.sidekiq' || (source == 'application.active_job' && scope) ||
|
|
78
|
+
(defined?(::Sidekiq::JobRetry::Handled) && error.is_a?(::Sidekiq::JobRetry::Handled))
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
class Installation
|
|
82
|
+
attr_accessor :error_reporting
|
|
83
|
+
|
|
84
|
+
def initialize(error_reporting)
|
|
85
|
+
@error_reporting = error_reporting
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def enabled?
|
|
89
|
+
PostHog.initialized? && PostHog.client.enabled?
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def with_job
|
|
93
|
+
scope = Scope.new(self, Thread.current[JOB_SCOPE_KEY])
|
|
94
|
+
Thread.current[JOB_SCOPE_KEY] = scope
|
|
95
|
+
yield
|
|
96
|
+
ensure
|
|
97
|
+
Thread.current[JOB_SCOPE_KEY] = scope.previous
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def report_error(error, context, _config = nil)
|
|
101
|
+
return unless enabled? && error_reporting == :all
|
|
102
|
+
return unless context[:job].is_a?(Hash)
|
|
103
|
+
|
|
104
|
+
capture(error, 'sidekiq.error_handler')
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
def report_death(job, error)
|
|
108
|
+
return unless enabled? && job.is_a?(Hash)
|
|
109
|
+
return unless error_reporting == :exhausted ||
|
|
110
|
+
(error_reporting == :dead && job['retry'] && job['dead'] != false && !job['discarded_at'])
|
|
111
|
+
|
|
112
|
+
capture(error, 'sidekiq.death_handler')
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
private
|
|
116
|
+
|
|
117
|
+
def capture(error, source)
|
|
118
|
+
return if excluded?(error)
|
|
119
|
+
|
|
120
|
+
distinct_id, properties, context = error.instance_variable_get(DEFERRED_REPORT_IVAR)
|
|
121
|
+
properties = (properties || {}).merge('$exception_source' => source, '$exception_level' => 'error')
|
|
122
|
+
Internal::Context.with_context(context, fresh: true, owner: PostHog.client, enrich_current_span: false) do
|
|
123
|
+
PostHog.capture_exception(
|
|
124
|
+
error, distinct_id, properties, mechanism: { 'type' => 'task', 'handled' => false }
|
|
125
|
+
)
|
|
126
|
+
end
|
|
127
|
+
rescue StandardError => e
|
|
128
|
+
PostHog::Logging.logger.error("Failed to capture Sidekiq job error: #{e.class}")
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
def excluded?(error)
|
|
132
|
+
seen = {}.compare_by_identity
|
|
133
|
+
current = error
|
|
134
|
+
while current && seen.size < PostHog::ExceptionCapture::MAX_CHAINED_EXCEPTIONS && !seen.key?(current)
|
|
135
|
+
return true unless PostHog::Rails.config.should_capture_exception?(current)
|
|
136
|
+
|
|
137
|
+
seen[current] = true
|
|
138
|
+
current = current.cause
|
|
139
|
+
end
|
|
140
|
+
false
|
|
141
|
+
end
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
class ServerMiddleware
|
|
145
|
+
def initialize(installation)
|
|
146
|
+
@installation = installation
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
def call(_worker, _job, _queue, &block)
|
|
150
|
+
return yield unless @installation.enabled?
|
|
151
|
+
|
|
152
|
+
@installation.with_job(&block)
|
|
153
|
+
end
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
private_constant :JOB_SCOPE_KEY, :INSTALLATION_IVAR, :DEFERRED_REPORT_IVAR,
|
|
157
|
+
:Scope, :Installation, :ServerMiddleware
|
|
158
|
+
end
|
|
159
|
+
end
|
|
160
|
+
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.
|
|
4
|
+
version: 3.25.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- PostHog
|
|
@@ -66,6 +66,7 @@ files:
|
|
|
66
66
|
- lib/posthog/rails/request_context.rb
|
|
67
67
|
- lib/posthog/rails/request_metadata.rb
|
|
68
68
|
- lib/posthog/rails/rescued_exception_interceptor.rb
|
|
69
|
+
- lib/posthog/rails/sidekiq.rb
|
|
69
70
|
- lib/posthog/rails/streaming_exceptions.rb
|
|
70
71
|
- lib/posthog/rails/tracing_headers.rb
|
|
71
72
|
- lib/posthog/rails/version.rb
|