diff --git a/sentry-rails/lib/sentry/rails/active_job.rb b/sentry-rails/lib/sentry/rails/active_job.rb index cf0c7ca1a..52a6f8064 100644 --- a/sentry-rails/lib/sentry/rails/active_job.rb +++ b/sentry-rails/lib/sentry/rails/active_job.rb @@ -38,19 +38,41 @@ def record(job, &block) rescue Exception => e # rubocop:disable Lint/RescueException finish_sentry_transaction(transaction, 500) - Sentry::Rails.capture_exception( - e, - extra: sentry_context(job), - tags: { - job_id: job.job_id, - provider_job_id: job.provider_job_id - } - ) + unless Sentry.configuration.active_job.report_after_job_retries + capture_exception(job, e) + end + raise end end end + def capture_exception(job, e) + Sentry::Rails.capture_exception( + e, + extra: sentry_context(job), + tags: { + job_id: job.job_id, + provider_job_id: job.provider_job_id + } + ) + end + + def register_retry_stopped_subscriber + ActiveSupport::Notifications.subscribe("retry_stopped.active_job") do |*args| + retry_stopped_handler(*args) + end + end + + def retry_stopped_handler(*args) + return unless Sentry.configuration.active_job.report_after_job_retries + + event = ActiveSupport::Notifications::Event.new(*args) + job = event.payload[:job] + error = event.payload[:error] + capture_exception(job, error) + end + def finish_sentry_transaction(transaction, status) return unless transaction diff --git a/sentry-rails/lib/sentry/rails/active_job/configuration.rb b/sentry-rails/lib/sentry/rails/active_job/configuration.rb new file mode 100644 index 000000000..05362485a --- /dev/null +++ b/sentry-rails/lib/sentry/rails/active_job/configuration.rb @@ -0,0 +1,23 @@ +module Sentry + class Configuration + attr_reader :active_job + + add_post_initialization_callback do + @active_job = Sentry::Rails::ActiveJob::Configuration.new + end + end + + module Rails + module ActiveJob + class Configuration + # Set this option to true if you want Sentry to only capture the last job + # retry if it fails. + attr_accessor :report_after_job_retries + + def initialize + @report_after_job_retries = false + end + end + end + end +end diff --git a/sentry-rails/lib/sentry/rails/railtie.rb b/sentry-rails/lib/sentry/rails/railtie.rb index ef09851c5..747094494 100644 --- a/sentry-rails/lib/sentry/rails/railtie.rb +++ b/sentry-rails/lib/sentry/rails/railtie.rb @@ -49,6 +49,8 @@ class Railtie < ::Rails::Railtie activate_tracing register_error_subscriber(app) if ::Rails.version.to_f >= 7.0 && Sentry.configuration.rails.register_error_subscriber + + register_retry_stopped_subscriber if defined?(ActiveJob) end runner do @@ -135,5 +137,9 @@ def register_error_subscriber(app) require "sentry/rails/error_subscriber" app.executor.error_reporter.subscribe(Sentry::Rails::ErrorSubscriber.new) end + + def register_retry_stopped_subscriber + Sentry::Rails::ActiveJobExtensions::SentryReporter.register_retry_stopped_subscriber + end end end