Skip to content

Commit 2be327a

Browse files
committed
add report_after_job_retries support for activejob
1 parent bd4f58a commit 2be327a

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
@@ -38,19 +38,41 @@ def record(job, &block)
3838
rescue Exception => e # rubocop:disable Lint/RescueException
3939
finish_sentry_transaction(transaction, 500)
4040

41-
Sentry::Rails.capture_exception(
42-
e,
43-
extra: sentry_context(job),
44-
tags: {
45-
job_id: job.job_id,
46-
provider_job_id: job.provider_job_id
47-
}
48-
)
41+
unless Sentry.configuration.active_job.report_after_job_retries
42+
capture_exception(job, e)
43+
end
44+
4945
raise
5046
end
5147
end
5248
end
5349

50+
def capture_exception(job, e)
51+
Sentry::Rails.capture_exception(
52+
e,
53+
extra: sentry_context(job),
54+
tags: {
55+
job_id: job.job_id,
56+
provider_job_id: job.provider_job_id
57+
}
58+
)
59+
end
60+
61+
def register_retry_stopped_subscriber
62+
ActiveSupport::Notifications.subscribe("retry_stopped.active_job") do |*args|
63+
retry_stopped_handler(*args)
64+
end
65+
end
66+
67+
def retry_stopped_handler(*args)
68+
return unless Sentry.configuration.active_job.report_after_job_retries
69+
70+
event = ActiveSupport::Notifications::Event.new(*args)
71+
job = event.payload[:job]
72+
error = event.payload[:error]
73+
capture_exception(job, error)
74+
end
75+
5476
def finish_sentry_transaction(transaction, status)
5577
return unless transaction
5678

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
@@ -49,6 +49,8 @@ class Railtie < ::Rails::Railtie
4949
activate_tracing
5050

5151
register_error_subscriber(app) if ::Rails.version.to_f >= 7.0 && Sentry.configuration.rails.register_error_subscriber
52+
53+
register_retry_stopped_subscriber if defined?(ActiveJob)
5254
end
5355

5456
runner do
@@ -135,5 +137,9 @@ def register_error_subscriber(app)
135137
require "sentry/rails/error_subscriber"
136138
app.executor.error_reporter.subscribe(Sentry::Rails::ErrorSubscriber.new)
137139
end
140+
141+
def register_retry_stopped_subscriber
142+
Sentry::Rails::ActiveJobExtensions::SentryReporter.register_retry_stopped_subscriber
143+
end
138144
end
139145
end

0 commit comments

Comments
 (0)