posthog-rails 3.24.0 → 3.26.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: fe991353ced9e95b77ef50aaadbfd2425ccd0cde6c0b306101d1d68b132690cf
4
- data.tar.gz: 8f511758988ef33f46932212dbf70aadf4c637832a3963a71ad75f8cddb8ceec
3
+ metadata.gz: 586ceff3bdce7582a56dfbbe58ee4fd262a0a43896806ae77b1bfbbb2ed53c35
4
+ data.tar.gz: b29984205eaf4be92d8faab5d4d46be91783215dc32e64911453f991fe45f60f
5
5
  SHA512:
6
- metadata.gz: d33e6c620adbefe406a91f3573577b0c924d635ad5b962cf7a87272867027099874e1814f281c7e8dedaec13ce4131ff09241f734478aee0124a3af34c3d119c
7
- data.tar.gz: d29ebcec8d4df442380ef5feb1a670c0dd1d3c6af27412685c80790b6a7c34faeb7c23297f3b7386b9db12665301a5cb03c9f8ebbd2a2986897cc6b1cfe318cd
6
+ metadata.gz: 54176111e05e831c4a0783786f011d96f564edf6209827178470a356fb7fdec9ddd0e18a94687312a855cb9abeb2be1c147a04e8eba65ff37d70fce73be154a7
7
+ data.tar.gz: ef7ed8f03eb0fc2f98aea58c6445f7e5333797ca2f361513b581f0292ced45b40e00fe425fe624974a4d276844c1f74c07c8ef7a7e5bc3d649b64a4b420a580c
@@ -49,7 +49,8 @@ PostHog::Rails.configure do |config|
49
49
  # config.current_user_resolver = proc { |controller| controller.current_user }
50
50
 
51
51
  # Additional exception classes to exclude from reporting
52
- # These are added to the default excluded exceptions
52
+ # These are added to the default excluded exceptions. Subclasses of a listed
53
+ # class are excluded too, and so is any exception whose cause chain contains one.
53
54
  # config.excluded_exceptions = [
54
55
  # # 'MyCustom404Error',
55
56
  # # 'MyCustomValidationError'
@@ -190,6 +191,15 @@ PostHog.init do |config|
190
191
  # config.test_mode = true if Rails.env.test? || Rails.env.development?
191
192
  end
192
193
 
194
+ # ============================================================================
195
+ # OPTIONAL SIDEKIQ INTEGRATION (Rails 7.1+, Sidekiq 7.0.3+)
196
+ # ============================================================================
197
+ # Install explicitly to report only terminal job failures. Other policies: :all, :dead.
198
+ # require 'posthog/rails/sidekiq'
199
+ # Sidekiq.configure_server do |config|
200
+ # PostHog::Rails::Sidekiq.install(config, error_reporting: :exhausted)
201
+ # end
202
+
193
203
  # ============================================================================
194
204
  # DEFAULT EXCLUDED EXCEPTIONS
195
205
  # ============================================================================
@@ -178,7 +178,6 @@ module PostHog
178
178
  config = PostHog::Rails.config
179
179
  return unless config&.auto_instrument_active_job && config.report_active_job_retries && exception
180
180
  return unless PostHog.initialized? && PostHog.client.enabled?
181
- return if excluded_posthog_retry?(exception, config)
182
181
 
183
182
  Internal::Context.with_context(
184
183
  @posthog_execution_context || posthog_job_context, fresh: true, enrich_current_span: false
@@ -189,20 +188,9 @@ module PostHog
189
188
  PostHog::Logging.logger.error("Failed to capture job retry: #{e.class}")
190
189
  end
191
190
 
192
- def excluded_posthog_retry?(exception, config)
193
- seen = {}.compare_by_identity
194
- current = exception
195
- while current && seen.size < PostHog::ExceptionCapture::MAX_CHAINED_EXCEPTIONS && !seen.key?(current)
196
- return true unless config.should_capture_exception?(current)
197
-
198
- seen[current] = true
199
- current = current.respond_to?(:cause) ? current.cause : nil
200
- end
201
- false
202
- end
203
-
204
191
  def capture_job_exception(exception, retry_failure: false)
205
192
  return unless PostHog::Rails.config&.auto_instrument_active_job
193
+ return unless PostHog::Rails.config&.should_capture_exception?(exception)
206
194
 
207
195
  # Build distinct_id from job arguments if possible
208
196
  distinct_id = if @posthog_execution_context
@@ -225,6 +213,8 @@ module PostHog
225
213
  # Add serialized job arguments (be careful with sensitive data)
226
214
  properties['$job_arguments'] = sanitize_job_arguments(arguments) if arguments.present?
227
215
 
216
+ return if defined?(PostHog::Rails::Sidekiq) && Sidekiq.defer_capture?(exception, distinct_id, properties)
217
+
228
218
  PostHog.capture_exception(
229
219
  exception,
230
220
  distinct_id,
@@ -4,6 +4,8 @@
4
4
  # Copyright (c) 2020 Sentry
5
5
  # Licensed under the MIT License: https://github.com/getsentry/sentry-ruby/blob/master/LICENSE
6
6
 
7
+ require 'posthog/exception_capture'
8
+
7
9
  module PostHog
8
10
  module Rails
9
11
  class Configuration
@@ -23,7 +25,8 @@ module PostHog
23
25
  # Defaults to false.
24
26
  attr_accessor :report_active_job_retries
25
27
 
26
- # @return [Array<String>] Exception class names to ignore in addition to the defaults.
28
+ # @return [Array<String>] Exception class names to ignore in addition to the defaults. Subclasses are
29
+ # ignored too, and so is any exception whose cause chain contains a listed class.
27
30
  attr_accessor :excluded_exceptions
28
31
 
29
32
  # @return [Boolean] Whether to use PostHog tracing headers for request-scoped identity/session context.
@@ -117,8 +120,10 @@ module PostHog
117
120
  # @param exception [Exception] The exception to check.
118
121
  # @return [Boolean] Whether the exception should be captured.
119
122
  def should_capture_exception?(exception)
120
- exception_name = exception.class.name
121
- !all_excluded_exceptions.include?(exception_name)
123
+ excluded = all_excluded_exceptions
124
+ exception_chain(exception).none? do |error|
125
+ error.class.ancestors.any? { |ancestor| excluded.include?(ancestor.name) }
126
+ end
122
127
  end
123
128
 
124
129
  private
@@ -126,6 +131,15 @@ module PostHog
126
131
  def all_excluded_exceptions
127
132
  default_excluded_exceptions + excluded_exceptions
128
133
  end
134
+
135
+ def exception_chain(exception)
136
+ chain = []
137
+ while exception.is_a?(Exception) && chain.size < ExceptionCapture::MAX_CHAINED_EXCEPTIONS
138
+ chain << exception
139
+ exception = exception.cause
140
+ end
141
+ chain
142
+ end
129
143
  end
130
144
  end
131
145
  end
@@ -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
@@ -2,6 +2,6 @@
2
2
 
3
3
  module PostHog
4
4
  module Rails
5
- VERSION = '3.24.0'
5
+ VERSION = '3.26.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.24.0
4
+ version: 3.26.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