Skip to content

Commit 2f24783

Browse files
committed
add report_after_job_retries support for activejob
1 parent d21c500 commit 2f24783

File tree

3 files changed

+59
-8
lines changed

3 files changed

+59
-8
lines changed

sentry-rails/lib/sentry/rails/active_job.rb

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,41 @@ def record(job, &block)
4646
rescue Exception => e # rubocop:disable Lint/RescueException
4747
finish_sentry_transaction(transaction, 500)
4848

49-
Sentry::Rails.capture_exception(
50-
e,
51-
extra: sentry_context(job),
52-
tags: {
53-
job_id: job.job_id,
54-
provider_job_id: job.provider_job_id
55-
}
56-
)
49+
unless Sentry.configuration.active_job.report_after_job_retries
50+
capture_exception(job, e)
51+
end
52+
5753
raise
5854
end
5955
end
6056
end
6157

58+
def capture_exception(job, e)
59+
Sentry::Rails.capture_exception(
60+
e,
61+
extra: sentry_context(job),
62+
tags: {
63+
job_id: job.job_id,
64+
provider_job_id: job.provider_job_id
65+
}
66+
)
67+
end
68+
69+
def register_retry_stopped_subscriber
70+
ActiveSupport::Notifications.subscribe("retry_stopped.active_job") do |*args|
71+
retry_stopped_handler(*args)
72+
end
73+
end
74+
75+
def retry_stopped_handler(*args)
76+
return unless Sentry.configuration.active_job.report_after_job_retries
77+
78+
event = ActiveSupport::Notifications::Event.new(*args)
79+
job = event.payload[:job]
80+
error = event.payload[:error]
81+
capture_exception(job, error)
82+
end
83+
6284
def finish_sentry_transaction(transaction, status)
6385
return unless transaction
6486

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
module Sentry
2+
class Configuration
3+
attr_reader :active_job
4+
5+
add_post_initialization_callback do
6+
@active_job = Sentry::Rails::ActiveJob::Configuration.new
7+
end
8+
end
9+
10+
module Rails
11+
module ActiveJob
12+
class Configuration
13+
# Set this option to true if you want Sentry to only capture the last job
14+
# retry if it fails.
15+
attr_accessor :report_after_job_retries
16+
17+
def initialize
18+
@report_after_job_retries = false
19+
end
20+
end
21+
end
22+
end
23+
end

sentry-rails/lib/sentry/rails/railtie.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ class Railtie < ::Rails::Railtie
5151
activate_tracing
5252

5353
register_error_subscriber(app) if ::Rails.version.to_f >= 7.0 && Sentry.configuration.rails.register_error_subscriber
54+
55+
register_retry_stopped_subscriber if defined?(ActiveJob)
5456
end
5557

5658
runner do
@@ -137,5 +139,9 @@ def register_error_subscriber(app)
137139
require "sentry/rails/error_subscriber"
138140
app.executor.error_reporter.subscribe(Sentry::Rails::ErrorSubscriber.new)
139141
end
142+
143+
def register_retry_stopped_subscriber
144+
Sentry::Rails::ActiveJobExtensions::SentryReporter.register_retry_stopped_subscriber
145+
end
140146
end
141147
end

0 commit comments

Comments
 (0)