Skip to content

Commit

Permalink
add report_after_job_retries support for activejob
Browse files Browse the repository at this point in the history
  • Loading branch information
modosc committed Dec 18, 2024
1 parent bd4f58a commit 5ecd340
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 8 deletions.
38 changes: 30 additions & 8 deletions sentry-rails/lib/sentry/rails/active_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
23 changes: 23 additions & 0 deletions sentry-rails/lib/sentry/rails/active_job/configuration.rb
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions sentry-rails/lib/sentry/rails/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.register_retry_stopped_subscriber
end
end
end

0 comments on commit 5ecd340

Please sign in to comment.