sentry-rails 7.1.0 → 7.3.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.
Files changed (49) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +5 -0
  3. data/README.md +5 -1
  4. data/gemfiles/ruby-2.7_rails-5.2.0.gemfile.lock +17 -17
  5. data/gemfiles/ruby-2.7_rails-6.0.0.gemfile.lock +17 -17
  6. data/gemfiles/ruby-2.7_rails-6.1.0.gemfile.lock +17 -17
  7. data/gemfiles/ruby-2.7_rails-7.0.0.gemfile.lock +17 -17
  8. data/gemfiles/ruby-2.7_rails-7.1.0.gemfile.lock +18 -18
  9. data/gemfiles/ruby-3.0_rails-6.1.0.gemfile.lock +34 -20
  10. data/gemfiles/ruby-3.0_rails-7.0.0.gemfile.lock +34 -20
  11. data/gemfiles/ruby-3.0_rails-7.1.0.gemfile.lock +34 -20
  12. data/gemfiles/ruby-3.1_rails-6.1.0.gemfile.lock +62 -38
  13. data/gemfiles/ruby-3.1_rails-7.0.0.gemfile.lock +62 -38
  14. data/gemfiles/ruby-3.1_rails-7.1.0.gemfile.lock +62 -38
  15. data/gemfiles/ruby-3.1_rails-7.2.0.gemfile.lock +129 -105
  16. data/gemfiles/ruby-3.2_rails-6.1.0.gemfile.lock +96 -72
  17. data/gemfiles/ruby-3.2_rails-7.0.0.gemfile.lock +96 -72
  18. data/gemfiles/ruby-3.2_rails-7.1.0.gemfile.lock +98 -74
  19. data/gemfiles/ruby-3.2_rails-7.2.0.gemfile.lock +95 -71
  20. data/gemfiles/ruby-3.2_rails-8.0.0.gemfile.lock +169 -145
  21. data/gemfiles/ruby-3.3_rails-6.1.0.gemfile.lock +96 -72
  22. data/gemfiles/ruby-3.3_rails-7.0.0.gemfile.lock +96 -72
  23. data/gemfiles/ruby-3.3_rails-7.1.0.gemfile.lock +98 -74
  24. data/gemfiles/ruby-3.3_rails-7.2.0.gemfile.lock +95 -71
  25. data/gemfiles/ruby-3.3_rails-8.0.0.gemfile.lock +169 -145
  26. data/gemfiles/ruby-3.4_rails-7.1.0.gemfile.lock +80 -64
  27. data/gemfiles/ruby-3.4_rails-7.2.0.gemfile.lock +77 -61
  28. data/gemfiles/ruby-3.4_rails-8.0.0.gemfile.lock +151 -135
  29. data/gemfiles/ruby-3.4_rails-8.1.3.gemfile.lock +151 -135
  30. data/gemfiles/ruby-4.0_rails-6.1.0.gemfile.lock +78 -62
  31. data/gemfiles/ruby-4.0_rails-7.0.0.gemfile.lock +78 -62
  32. data/gemfiles/ruby-4.0_rails-7.1.0.gemfile.lock +80 -64
  33. data/gemfiles/ruby-4.0_rails-8.0.0.gemfile.lock +151 -135
  34. data/gemfiles/ruby-4.0_rails-8.1.3.gemfile.lock +151 -135
  35. data/gemfiles/ruby-jruby-9.4.14.0_rails-6.1.0.gemfile.lock +35 -35
  36. data/gemfiles/ruby-jruby-9.4.14.0_rails-7.0.0.gemfile.lock +35 -35
  37. data/gemfiles/ruby-jruby-9.4.14.0_rails-7.1.0.gemfile.lock +35 -35
  38. data/lib/sentry/rails/active_job.rb +46 -186
  39. data/lib/sentry/rails/active_job_extension.rb +196 -0
  40. data/lib/sentry/rails/configuration.rb +12 -0
  41. data/lib/sentry/rails/good_job_cron.rb +152 -0
  42. data/lib/sentry/rails/overrides/streaming_reporter.rb +2 -0
  43. data/lib/sentry/rails/railtie.rb +15 -1
  44. data/lib/sentry/rails/solid_queue_recurring.rb +119 -0
  45. data/lib/sentry/rails/version.rb +1 -1
  46. data/lib/sentry/rails.rb +47 -0
  47. data/sentry-rails.gemspec +1 -1
  48. metadata +11 -9
  49. data/bin/test_old +0 -389
@@ -3,6 +3,7 @@
3
3
  require "set"
4
4
  require "sentry/rails/serializer"
5
5
  require "sentry/rails/error_reporter_context"
6
+ require "sentry/rails/active_job_extension"
6
7
 
7
8
  module Sentry
8
9
  module Rails
@@ -13,44 +14,21 @@ module Sentry
13
14
 
14
15
  def self.prepended(base)
15
16
  base.attr_accessor :_sentry
17
+ base.attr_reader :_sentry_job_data
16
18
  end
17
19
 
18
20
  def perform_now
19
21
  if !Sentry.initialized? || already_supported_by_sentry_integration?
20
22
  super
21
23
  else
22
- data = _sentry || {}
23
- SentryReporter.record(
24
- self,
25
- trace_headers: data["trace_propagation_headers"],
26
- user: data["user"]
27
- ) { super }
24
+ SentryReporter.record(self) { super }
28
25
  end
29
26
  end
30
27
 
31
28
  def serialize
32
29
  payload = super
33
30
  return payload if !Sentry.initialized? || already_supported_by_sentry_integration?
34
-
35
- begin
36
- sentry_data = {}
37
- if Sentry.configuration.rails.active_job_propagate_traces
38
- headers = Sentry.get_trace_propagation_headers
39
- sentry_data["trace_propagation_headers"] = headers if headers && !headers.empty?
40
- end
41
-
42
- if Sentry.configuration.data_collection.user_info
43
- user = Sentry.get_current_scope.user
44
- allowed = user.transform_keys(&:to_s).slice(*USER_FIELDS_ALLOWLIST)
45
- sentry_data["user"] = allowed unless allowed.empty?
46
- end
47
-
48
- payload[SENTRY_PAYLOAD_KEY] = sentry_data unless sentry_data.empty?
49
- rescue StandardError => e
50
- Sentry.sdk_logger&.error("sentry-rails: failed to inject _sentry payload: #{e.class}: #{e.message}\n #{Array(e.backtrace).first(5).join("\n ")}")
51
- end
52
-
53
- payload
31
+ SentryReporter.prepare_payload(self, payload)
54
32
  end
55
33
 
56
34
  def deserialize(job_data)
@@ -58,6 +36,7 @@ module Sentry
58
36
  return if !Sentry.initialized? || already_supported_by_sentry_integration?
59
37
 
60
38
  begin
39
+ @_sentry_job_data = job_data
61
40
  self._sentry = job_data[SENTRY_PAYLOAD_KEY]
62
41
  rescue StandardError => e
63
42
  Sentry.sdk_logger&.error("sentry-rails: failed to extract _sentry payload: #{e.class}: #{e.message}\n #{Array(e.backtrace).first(5).join("\n ")}")
@@ -65,17 +44,12 @@ module Sentry
65
44
  end
66
45
 
67
46
  def already_supported_by_sentry_integration?
68
- Sentry.configuration.rails.skippable_job_adapters.include?(self.class.queue_adapter.class.to_s)
47
+ !Sentry::Rails.active_job_owns_adapter?(self.class.queue_adapter.class)
69
48
  end
70
49
 
71
50
  class SentryReporter
72
- OP_NAME = "queue.process"
73
- SPAN_ORIGIN = "auto.queue.active_job"
74
-
75
- # Emitted as messaging.system when the configured queue adapter's
76
- # identity cannot be resolved from the job. ActiveJob is adapter-agnostic
77
- # and supports arbitrary third-party backends, so the concrete value is
78
- # normally derived from the adapter itself; this is only the last resort.
51
+ OP_NAME = Sentry::JobExtensions::Extension::PROCESS_OP
52
+ SPAN_ORIGIN = ActiveJobExtension::SPAN_ORIGIN
79
53
  MESSAGING_SYSTEM_FALLBACK = "activejob"
80
54
 
81
55
  EVENT_HANDLERS = {
@@ -83,7 +57,9 @@ module Sentry
83
57
  }
84
58
 
85
59
  class << self
86
- include ErrorReporterContext
60
+ def prepare_payload(job, payload)
61
+ Sentry::Rails.active_job_extension_for(job.class).prepare_payload(job: job, payload: payload)
62
+ end
87
63
 
88
64
  def producer_callback_registered?
89
65
  @producer_callback_registered ||= false
@@ -96,143 +72,38 @@ module Sentry
96
72
  def record_producer_span(job, &enqueue)
97
73
  return yield if !Sentry.initialized? || job.already_supported_by_sentry_integration?
98
74
 
99
- enqueued = false
100
-
101
- run_enqueue = lambda do
102
- enqueued = true
103
- enqueue.call
104
- end
105
-
106
- begin
107
- Sentry.with_child_span(op: "queue.publish", description: job.class.name) do |span|
108
- if span
109
- span.set_origin(SPAN_ORIGIN)
110
- span.set_data(Sentry::Span::DataConventions::MESSAGING_SYSTEM, messaging_system(job))
111
- span.set_data(Sentry::Span::DataConventions::MESSAGING_MESSAGE_ID, job.job_id)
112
- span.set_data(Sentry::Span::DataConventions::MESSAGING_DESTINATION_NAME, job.queue_name)
113
-
114
- if (count = retry_count(job))
115
- span.set_data(Sentry::Span::DataConventions::MESSAGING_MESSAGE_RETRY_COUNT, count)
116
- end
117
- end
118
-
119
- run_enqueue.call
120
- end
121
- rescue StandardError => e
122
- raise if enqueued
123
-
124
- log_producer_span_error(e)
75
+ Sentry::Rails.active_job_extension_for(job.class).instrument_enqueue(job: job, &enqueue)
76
+ end
125
77
 
126
- run_enqueue.call
78
+ def record(job, &block)
79
+ with_isolated_scope do |scope|
80
+ record_in_scope(scope, job, &block)
127
81
  end
128
82
  end
129
83
 
130
- def log_producer_span_error(e)
131
- Sentry.sdk_logger&.error("sentry-rails: failed to record producer span: #{e.class}: #{e.message}\n #{Array(e.backtrace).first(5).join("\n ")}")
132
- end
84
+ def record_in_scope(scope, job)
85
+ extension = Sentry::Rails.active_job_extension_for(job.class)
86
+ transaction = nil
133
87
 
134
- def record(job, trace_headers: nil, user: nil, &block)
135
- original_hub = Sentry.get_current_hub_internal
136
- Sentry.clone_hub_to_current_thread
88
+ begin
89
+ transaction = extension.start_job(scope: scope, job: job)
137
90
 
138
- Sentry.with_scope do |scope|
139
- begin
140
- scope.set_user(user.transform_keys(&:to_sym)) if user && !user.empty?
141
-
142
- scope.set_transaction_name(job.class.name, source: :task)
143
- scope.set_tags(queue: job.queue_name)
144
-
145
- scope.set_contexts(active_job: {
146
- job_class: job.class.name,
147
- job_id: job.job_id,
148
- queue: job.queue_name,
149
- provider_job_id: job.provider_job_id
150
- })
151
-
152
- transaction_options = {
153
- name: scope.transaction_name,
154
- source: scope.transaction_source,
155
- op: OP_NAME,
156
- origin: SPAN_ORIGIN
157
- }
158
-
159
- transaction = if trace_headers && !trace_headers.empty?
160
- continued = Sentry.continue_trace(trace_headers, **transaction_options)
161
- Sentry.start_transaction(transaction: continued, **transaction_options)
162
- else
163
- Sentry.start_transaction(**transaction_options)
164
- end
165
-
166
- if transaction
167
- set_messaging_data(transaction, job)
168
- scope.set_span(transaction)
169
- end
170
-
171
- yield.tap do
172
- finish_sentry_transaction(transaction, 200)
173
- end
174
- rescue Exception => e # rubocop:disable Lint/RescueException
175
- finish_sentry_transaction(transaction, 500)
176
-
177
- capture_exception(job, e)
178
-
179
- raise
91
+ yield.tap do
92
+ extension.finish_job(transaction: transaction, status: 200)
180
93
  end
181
- end
182
- ensure
183
- Sentry.set_current_hub_internal(original_hub)
184
- end
94
+ rescue Exception => e # rubocop:disable Lint/RescueException
95
+ extension.finish_job(transaction: transaction, status: 500)
185
96
 
186
- def set_messaging_data(transaction, job)
187
- transaction.set_data(Sentry::Span::DataConventions::MESSAGING_SYSTEM, messaging_system(job))
188
- transaction.set_data(Sentry::Span::DataConventions::MESSAGING_MESSAGE_ID, job.job_id)
189
- transaction.set_data(Sentry::Span::DataConventions::MESSAGING_DESTINATION_NAME, job.queue_name)
190
- if (count = retry_count(job))
191
- transaction.set_data(Sentry::Span::DataConventions::MESSAGING_MESSAGE_RETRY_COUNT, count)
192
- end
97
+ if extension.safely("decide whether to report the job exception", fallback: true) { extension.report_exception?(job: job, exception: e) }
98
+ extension.capture_exception(job: job, exception: e)
99
+ end
193
100
 
194
- if (latency = compute_latency(job))
195
- transaction.set_data(Sentry::Span::DataConventions::MESSAGING_MESSAGE_RECEIVE_LATENCY, latency)
101
+ raise
196
102
  end
197
103
  end
198
104
 
199
- def messaging_system(job)
200
- name = job.class.queue_adapter_name if job.class.respond_to?(:queue_adapter_name)
201
- name = name.to_s
202
- name.empty? ? MESSAGING_SYSTEM_FALLBACK : name
203
- end
204
-
205
- # Number of retries the job has already gone through, as observed at
206
- # the moment the span is opened. On the consumer the span opens before
207
- # ActiveJob increments +executions+, so a job's first attempt reads 0,
208
- # its first retry reads 1, and so on. On the producer the retry enqueue
209
- # runs after the failed attempt bumped +executions+, so it observes the
210
- # same progression.
211
- def retry_count(job)
212
- job.executions.to_i if job.respond_to?(:executions)
213
- end
214
-
215
- def compute_latency(job)
216
- return unless job.respond_to?(:enqueued_at) && job.enqueued_at
217
-
218
- enqueued_time = job.enqueued_at.is_a?(String) ? Time.parse(job.enqueued_at) : job.enqueued_at
219
- (Time.now.to_f - enqueued_time.to_f) * 1000.0
220
- end
221
-
222
105
  def capture_exception(job, e)
223
- options = {
224
- extra: sentry_context(job),
225
- tags: {
226
- job_id: job.job_id,
227
- provider_job_id: job.provider_job_id
228
- },
229
- # Send synchronously: a worker process may exit before the async
230
- # background worker flushes its queue, which would drop the event.
231
- hint: { background: false }
232
- }
233
- options[:contexts] = execution_context if Sentry.configuration.data_collection.queues
234
-
235
- Sentry::Rails.capture_exception(e, **options)
106
+ Sentry::Rails.active_job_extension_for(job.class).capture_exception(job: job, exception: e)
236
107
  end
237
108
 
238
109
  def register_event_handlers
@@ -253,10 +124,16 @@ module Sentry
253
124
  # This handler does not capture error unless `active_job_report_on_retry_error` is true
254
125
  def retry_handler(*args)
255
126
  handle_error_event(*args) do |job, error|
127
+ return if error.nil?
256
128
  return if !Sentry.initialized? || job.already_supported_by_sentry_integration?
257
- return unless Sentry.configuration.rails.active_job_report_on_retry_error
258
129
 
259
- capture_exception(job, error)
130
+ extension = Sentry::Rails.active_job_extension_for(job.class)
131
+ report = extension.safely("decide whether to report the retried exception", fallback: Sentry.configuration.rails.active_job_report_on_retry_error) do
132
+ extension.report_retried_exception?(job: job, exception: error)
133
+ end
134
+ return unless report
135
+
136
+ extension.capture_exception(job: job, exception: error)
260
137
  end
261
138
  end
262
139
 
@@ -265,35 +142,18 @@ module Sentry
265
142
  yield(event.payload[:job], event.payload[:error])
266
143
  end
267
144
 
268
- def finish_sentry_transaction(transaction, status)
269
- return unless transaction
145
+ private :record_in_scope
270
146
 
271
- transaction.set_http_status(status)
272
- transaction.finish
273
- end
274
-
275
- def sentry_context(job)
276
- context = {
277
- active_job: job.class.name,
278
- scheduled_at: job.scheduled_at,
279
- job_id: job.job_id,
280
- provider_job_id: job.provider_job_id,
281
- locale: job.locale
282
- }
283
-
284
- if Sentry.configuration.data_collection.queues
285
- context[:arguments] = sentry_serialize_arguments(job.arguments)
286
- end
287
-
288
- context
289
- end
147
+ private
290
148
 
291
- def sentry_serialize_arguments(argument)
292
- Sentry::Rails::Serializer.serialize(argument)
149
+ def with_isolated_scope(&block)
150
+ original_hub = Sentry.get_current_hub_internal
151
+ Sentry.clone_hub_to_current_thread
152
+ Sentry.with_scope(&block)
153
+ ensure
154
+ Sentry.set_current_hub_internal(original_hub)
293
155
  end
294
156
 
295
- private
296
-
297
157
  def subscribers
298
158
  @__subscribers__ ||= Set.new
299
159
  end
@@ -0,0 +1,196 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "sentry/rails/serializer"
4
+ require "sentry/rails/error_reporter_context"
5
+
6
+ module Sentry
7
+ module Rails
8
+ # The job extension sentry-rails uses for ActiveJob jobs. Backend gems
9
+ # subclass it to enrich ActiveJob jobs run through their queue adapter, and
10
+ # register it with {Sentry::Rails.register_active_job_extension}.
11
+ class ActiveJobExtension < Sentry::JobExtensions::Extension
12
+ include ErrorReporterContext
13
+
14
+ SPAN_ORIGIN = "auto.queue.active_job"
15
+
16
+ # @param serializer [#serialize] serializes job arguments for error events
17
+ # @see Sentry::JobExtensions::Extension#initialize
18
+ def initialize(name: :active_job, integration: Sentry::Rails, origin: SPAN_ORIGIN, serializer: Sentry::Rails::Serializer)
19
+ super(name: name, integration: integration, origin: origin)
20
+ @serializer = serializer
21
+ end
22
+
23
+ # Returns the name of the job class' queue adapter.
24
+ #
25
+ # @param job_class [Class]
26
+ # @return [Symbol]
27
+ def self.adapter_name(job_class)
28
+ name = job_class.queue_adapter_name if job_class.respond_to?(:queue_adapter_name)
29
+ (name || job_class.queue_adapter.class.name.demodulize.delete_suffix("Adapter").underscore).to_sym
30
+ end
31
+
32
+ # Returns the job's serialized payload carrying Sentry's trace propagation
33
+ # headers and user data.
34
+ #
35
+ # @param job [ActiveJob::Base]
36
+ # @param payload [Hash]
37
+ # @return [Hash]
38
+ def prepare_payload(job:, payload:)
39
+ prepared = payload
40
+ sentry_data = {}
41
+ if Sentry.configuration.rails.active_job_propagate_traces
42
+ headers = Sentry.get_trace_propagation_headers
43
+ if headers && !headers.empty?
44
+ sentry_data["trace_propagation_headers"] = headers
45
+ injected = safely("inject trace headers", fallback: payload) { inject_trace_headers(job: job, payload: payload, headers: headers) }
46
+ prepared = injected if injected.is_a?(Hash)
47
+ end
48
+ end
49
+
50
+ if Sentry.configuration.data_collection.user_info
51
+ user = Sentry.get_current_scope.user.transform_keys(&:to_s).slice(*ActiveJobExtensions::USER_FIELDS_ALLOWLIST)
52
+ sentry_data["user"] = user unless user.empty?
53
+ end
54
+
55
+ sentry_data.empty? ? prepared : prepared.merge(ActiveJobExtensions::SENTRY_PAYLOAD_KEY => sentry_data)
56
+ rescue StandardError => e
57
+ Sentry.sdk_logger&.error("sentry-rails: failed to inject _sentry payload: #{e.class}: #{e.message}\n #{Array(e.backtrace).first(5).join("\n ")}")
58
+ payload
59
+ end
60
+
61
+ # Enqueues the job even when recording its `queue.publish` span fails.
62
+ #
63
+ # @see Sentry::JobExtensions::Extension#instrument_enqueue
64
+ def instrument_enqueue(job:, **context, &enqueue)
65
+ enqueued = false
66
+ super(job: job, **context) do
67
+ enqueued = true
68
+ enqueue.call
69
+ end
70
+ rescue StandardError => e
71
+ raise if enqueued
72
+
73
+ log_failure("record producer span", e)
74
+ enqueue.call
75
+ end
76
+
77
+ # Also applies the user and ActiveJob context the job carries.
78
+ #
79
+ # @see Sentry::JobExtensions::Extension#start_job
80
+ def start_job(scope:, job:, **context)
81
+ user = job._sentry&.[]("user")
82
+ scope.set_user(user.transform_keys(&:to_sym)) if user && !user.empty?
83
+ scope.set_transaction_name(job.class.name, source: :task)
84
+
85
+ super.tap { apply_active_job_context(scope, job) }
86
+ end
87
+
88
+ # Also adds the job's ActiveJob data to the event.
89
+ #
90
+ # @see Sentry::JobExtensions::Extension#capture_exception
91
+ def capture_exception(job:, exception:, contexts: {}, tags: {}, extra: {}, **context)
92
+ data = active_job_error_data(job)
93
+
94
+ super(
95
+ job: job,
96
+ exception: exception,
97
+ contexts: data[:contexts].merge(contexts),
98
+ tags: data[:tags].merge(tags),
99
+ extra: data[:extra].merge(extra),
100
+ **context
101
+ )
102
+ end
103
+
104
+ # Reads the trace propagation headers stored in the job's Sentry payload.
105
+ #
106
+ # @see Sentry::JobExtensions::Extension#extract_trace_headers
107
+ def extract_trace_headers(job:, **)
108
+ job._sentry&.[]("trace_propagation_headers") || {}
109
+ end
110
+
111
+ # Describes the job with its ActiveJob messaging attributes, its receive
112
+ # latency and body size.
113
+ #
114
+ # @see Sentry::JobExtensions::Extension#messaging_data
115
+ def messaging_data(job:, **)
116
+ data = active_job_messaging_data(job)
117
+ data[:receive_latency] = Sentry::Utils::Latency.milliseconds_since(job.enqueued_at) if job.respond_to?(:enqueued_at)
118
+ data[:body_size] = body_size(job._sentry_job_data) if Sentry.get_current_scope.get_span&.sampled
119
+ data.compact
120
+ end
121
+
122
+ # Describes the job with its ActiveJob messaging attributes.
123
+ #
124
+ # @see Sentry::JobExtensions::Extension#publish_messaging_data
125
+ def publish_messaging_data(job:, **)
126
+ active_job_messaging_data(job)
127
+ end
128
+
129
+ # Follows `config.rails.active_job_report_on_retry_error`.
130
+ #
131
+ # @see Sentry::JobExtensions::Extension#report_retried_exception?
132
+ def report_retried_exception?(job:, exception:, **)
133
+ Sentry.configuration.rails.active_job_report_on_retry_error
134
+ end
135
+
136
+ private
137
+
138
+ def log_failure(context, error)
139
+ Sentry.sdk_logger&.error("sentry-rails: job extension #{name.inspect} failed to #{context}: #{error.class}: #{error.message}\n #{Array(error.backtrace).first(5).join("\n ")}")
140
+ end
141
+
142
+ def active_job_messaging_data(job)
143
+ name = job.class.queue_adapter_name if job.class.respond_to?(:queue_adapter_name)
144
+ {
145
+ system: name.to_s.empty? ? "activejob" : name.to_s,
146
+ message_id: job.job_id,
147
+ destination: job.queue_name,
148
+ retry_count: (job.executions.to_i if job.respond_to?(:executions))
149
+ }.compact
150
+ end
151
+
152
+ def body_size(payload)
153
+ JSON.generate(payload).bytesize if payload
154
+ rescue StandardError
155
+ nil
156
+ end
157
+
158
+ def apply_active_job_context(scope, job)
159
+ required = {
160
+ job_class: job.class.name,
161
+ job_id: job.job_id,
162
+ queue: job.queue_name,
163
+ provider_job_id: job.provider_job_id
164
+ }
165
+ scope.set_contexts(active_job: scope.contexts.fetch(:active_job, {}).merge(required))
166
+ scope.set_tags(queue: job.queue_name)
167
+ end
168
+
169
+ def active_job_error_data(job)
170
+ queues = Sentry.configuration.data_collection.queues
171
+ extra = {
172
+ active_job: job.class.name,
173
+ scheduled_at: job.scheduled_at,
174
+ job_id: job.job_id,
175
+ provider_job_id: job.provider_job_id,
176
+ locale: job.locale
177
+ }
178
+ arguments = serialize_arguments(job.arguments) if queues
179
+ extra[:arguments] = arguments unless arguments.nil?
180
+
181
+ {
182
+ contexts: queues ? execution_context : {},
183
+ tags: { job_id: job.job_id, provider_job_id: job.provider_job_id },
184
+ extra: extra
185
+ }
186
+ end
187
+
188
+ def serialize_arguments(arguments)
189
+ @serializer.serialize(arguments)
190
+ rescue StandardError => e
191
+ Sentry.sdk_logger&.error("sentry-rails: failed to serialize job arguments: #{e.class}: #{e.message}")
192
+ nil
193
+ end
194
+ end
195
+ end
196
+ end
@@ -183,6 +183,16 @@ module Sentry
183
183
  # Defaults to true. Set to false to opt out.
184
184
  attr_accessor :active_job_propagate_traces
185
185
 
186
+ # GoodJob cron keys to skip when the :good_job_cron patch is enabled.
187
+ # Strings and Symbols match the key exactly; Regexps are matched against it.
188
+ # @return [Array<String, Symbol, Regexp>]
189
+ attr_accessor :good_job_exclude_cron_jobs
190
+
191
+ # Solid Queue recurring task keys to skip when the :solid_queue_recurring patch is enabled.
192
+ # Strings match the key exactly; Regexps are matched against it.
193
+ # @return [Array<String, Regexp>]
194
+ attr_accessor :solid_queue_exclude_recurring_tasks
195
+
186
196
  # Configuration for structured logging feature
187
197
  # @return [StructuredLoggingConfiguration]
188
198
  attr_reader :structured_logging
@@ -205,6 +215,8 @@ module Sentry
205
215
  @active_support_logger_subscription_items = Sentry::Rails::ACTIVE_SUPPORT_LOGGER_SUBSCRIPTION_ITEMS_DEFAULT.dup
206
216
  @active_job_report_on_retry_error = false
207
217
  @active_job_propagate_traces = true
218
+ @good_job_exclude_cron_jobs = []
219
+ @solid_queue_exclude_recurring_tasks = []
208
220
  @structured_logging = StructuredLoggingConfiguration.new
209
221
  end
210
222
  end
@@ -0,0 +1,152 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sentry
4
+ module Rails
5
+ # Sends Crons check-ins for GoodJob cron entries.
6
+ # Enable with `config.enabled_patches << :good_job_cron`.
7
+ module GoodJobCron
8
+ EVENT_STATE = :sentry_good_job_cron
9
+ MINIMUM_GOOD_JOB_VERSION = Gem::Version.new("4.0")
10
+
11
+ class << self
12
+ def excluded?(key)
13
+ return false unless Sentry.initialized?
14
+
15
+ key = key.to_s
16
+ Array(Sentry.configuration.rails.good_job_exclude_cron_jobs).any? do |pattern|
17
+ (pattern.is_a?(Symbol) ? pattern.to_s : pattern) === key
18
+ end
19
+ rescue StandardError => e
20
+ Sentry.sdk_logger.error(Sentry::LOGGER_PROGNAME) { "Could not check good_job_exclude_cron_jobs for #{key}: #{e.class}: #{e.message}" }
21
+ false
22
+ end
23
+
24
+ # The persisted record identifies the cron run, including retries and callable classes.
25
+ def monitor_for(job)
26
+ key = job.cron_key
27
+ return unless key && Sentry.initialized?
28
+ return if excluded?(key)
29
+
30
+ entry = ::GoodJob::CronEntry.find(key)
31
+ job_class = job.job_class.safe_constantize
32
+ return if job_class && job_class < Sentry::Cron::MonitorCheckIns::Patch
33
+
34
+ slug = Sentry::Cron::Slug.from(entry.key)
35
+ return unless slug
36
+
37
+ config = schedule_for(entry)
38
+ unless config
39
+ Sentry.sdk_logger.debug(Sentry::LOGGER_PROGNAME) { "Not monitoring GoodJob cron entry #{entry.key}: its schedule can't be expressed as a Crons schedule." }
40
+ return
41
+ end
42
+ return if slug_shared?(entry.key, slug)
43
+
44
+ [slug, config]
45
+ rescue StandardError => e
46
+ Sentry.sdk_logger.error(Sentry::LOGGER_PROGNAME) { "Not monitoring GoodJob cron entry #{key}: #{e.class}: #{e.message}" }
47
+ nil
48
+ end
49
+
50
+ # Evented subscribers receive start before execution and finish afterwards.
51
+ # State belongs to the notification payload, so nested and concurrent runs are independent.
52
+ def start(_name, _id, payload)
53
+ return unless Sentry.initialized? && Sentry.configuration.enabled_patches.include?(:good_job_cron)
54
+
55
+ monitor = monitor_for(payload[:job])
56
+ return unless monitor
57
+
58
+ slug, schedule = monitor
59
+ cron_config = Sentry.configuration.cron
60
+ config = Sentry::Cron::MonitorConfig.from_crontab(
61
+ schedule.schedule.value,
62
+ timezone: schedule.timezone,
63
+ checkin_margin: cron_config.default_checkin_margin,
64
+ max_runtime: cron_config.default_max_runtime
65
+ )
66
+ check_in_id = Sentry.capture_check_in(slug, :in_progress, monitor_config: config)
67
+ payload[EVENT_STATE] = { slug: slug, check_in_id: check_in_id, started_at: monotonic_now }
68
+ rescue StandardError => e
69
+ log_error(e)
70
+ end
71
+
72
+ def finish(_name, _id, payload)
73
+ state = payload.delete(EVENT_STATE)
74
+ return unless state && Sentry.initialized?
75
+
76
+ error = payload[:handled_error] || payload[:unhandled_error] || payload[:error] || payload[:exception]
77
+ Sentry.capture_check_in(
78
+ state[:slug], error ? :error : :ok,
79
+ check_in_id: state[:check_in_id], duration: monotonic_now - state[:started_at]
80
+ )
81
+ rescue StandardError => e
82
+ log_error(e)
83
+ end
84
+
85
+ def patch
86
+ return if @subscriber
87
+
88
+ @subscriber = ActiveSupport::Notifications.subscribe("perform_job.good_job", self)
89
+ end
90
+
91
+ private
92
+
93
+ def log_error(error)
94
+ Sentry.sdk_logger.error(Sentry::LOGGER_PROGNAME) { "Could not send GoodJob cron check-in: #{error.class}: #{error.message}" }
95
+ end
96
+
97
+ def monotonic_now
98
+ Process.clock_gettime(Process::CLOCK_MONOTONIC)
99
+ end
100
+
101
+ # Proc schedules can change between runs. Match the scheduler's local-zone
102
+ # resolution: EtOrbi prefers ENV['TZ'], then the Rails app's default zone.
103
+ # A request's temporary Time.use_zone must not change the monitor's zone.
104
+ def schedule_for(entry)
105
+ cron = entry.params[:cron]
106
+ return if cron.respond_to?(:call)
107
+
108
+ Time.use_zone(Time.zone_default) { Sentry::Cron::FugitSchedule.from_cron(::Fugit.parse(cron)) }
109
+ end
110
+
111
+ # Different keys can share a slug, like long keys cut at the same length.
112
+ # One monitor can't follow two schedules, so none of them are monitored.
113
+ def slug_shared?(key, slug)
114
+ colliding = ::GoodJob::CronEntry.all.select do |other|
115
+ other.key.to_s != key.to_s && Sentry::Cron::Slug.from(other.key) == slug && monitored?(other)
116
+ end.map(&:key)
117
+ return false if colliding.empty?
118
+
119
+ @slug_warnings ||= Concurrent::Map.new
120
+ if @slug_warnings.put_if_absent(key.to_s, true).nil?
121
+ Sentry.sdk_logger.warn(Sentry::LOGGER_PROGNAME) do
122
+ "Not monitoring GoodJob cron entry #{key}: its monitor slug #{slug} is also used by #{colliding.join(", ")}. Rename or exclude one of them."
123
+ end
124
+ end
125
+ true
126
+ end
127
+
128
+ def monitored?(entry)
129
+ return false if excluded?(entry.key) || !schedule_for(entry)
130
+
131
+ job_class = entry.job_class
132
+ job_class = job_class.safe_constantize if job_class.is_a?(String)
133
+ !(job_class.is_a?(Class) && job_class < Sentry::Cron::MonitorCheckIns::Patch)
134
+ end
135
+ end
136
+ end
137
+ end
138
+ end
139
+
140
+ Sentry.register_patch(:good_job_cron) do |config|
141
+ if defined?(::GoodJob) && defined?(::Rails.application) && ::Rails.application
142
+ if Gem::Version.new(::GoodJob::VERSION) < Sentry::Rails::GoodJobCron::MINIMUM_GOOD_JOB_VERSION
143
+ config.sdk_logger.warn(Sentry::LOGGER_PROGNAME) do
144
+ "The GoodJob cron integration requires good_job #{Sentry::Rails::GoodJobCron::MINIMUM_GOOD_JOB_VERSION} or later, but #{::GoodJob::VERSION} is installed."
145
+ end
146
+ else
147
+ Sentry::Rails::GoodJobCron.patch
148
+ end
149
+ else
150
+ config.sdk_logger.warn(Sentry::LOGGER_PROGNAME) { "You tried to enable the GoodJob cron integration but the `good_job` gem was not detected." }
151
+ end
152
+ end