From daee3d87e5f4eb78631035b695ea77019157ea23 Mon Sep 17 00:00:00 2001 From: jonathan schatz Date: Wed, 18 Dec 2024 15:21:32 -0800 Subject: [PATCH] add tests --- sentry-rails/lib/sentry/rails/active_job.rb | 9 ++-- .../spec/sentry/rails/activejob_spec.rb | 42 ++++++++++++++++++- 2 files changed, 46 insertions(+), 5 deletions(-) diff --git a/sentry-rails/lib/sentry/rails/active_job.rb b/sentry-rails/lib/sentry/rails/active_job.rb index dcaf89d52..0280d5c54 100644 --- a/sentry-rails/lib/sentry/rails/active_job.rb +++ b/sentry-rails/lib/sentry/rails/active_job.rb @@ -20,7 +20,7 @@ def already_supported_by_sentry_integration? class SentryReporter OP_NAME = "queue.active_job" SPAN_ORIGIN = "auto.queue.active_job" - + NOTIFICATION_NAME = "retry_stopped.active_job" class << self def record(job, &block) Sentry.with_scope do |scope| @@ -67,17 +67,18 @@ def capture_exception(job, e) end def register_retry_stopped_subscriber - ActiveSupport::Notifications.subscribe("retry_stopped.active_job") do |*args| + ActiveSupport::Notifications.subscribe(NOTIFICATION_NAME) do |*args| retry_stopped_handler(*args) end end def retry_stopped_handler(*args) - return if !Sentry.initialized? || already_supported_by_sentry_integration? - return unless Sentry.configuration.rails.active_job_report_after_job_retries event = ActiveSupport::Notifications::Event.new(*args) job = event.payload[:job] error = event.payload[:error] + + return if !Sentry.initialized? || job.already_supported_by_sentry_integration? + return unless Sentry.configuration.rails.active_job_report_after_job_retries capture_exception(job, error) end diff --git a/sentry-rails/spec/sentry/rails/activejob_spec.rb b/sentry-rails/spec/sentry/rails/activejob_spec.rb index ffcb4d44c..e3a500fe4 100644 --- a/sentry-rails/spec/sentry/rails/activejob_spec.rb +++ b/sentry-rails/spec/sentry/rails/activejob_spec.rb @@ -67,6 +67,9 @@ class FailedJobWithCron < FailedJob sentry_monitor_check_ins slug: "failed_job", monitor_config: Sentry::Cron::MonitorConfig.from_crontab("5 * * * *") end +class FailedJobWithRetryOn < FailedJob + retry_on StandardError, attempts: 3, wait: 0 +end RSpec.describe "without Sentry initialized", type: :job do it "runs job" do @@ -318,7 +321,6 @@ def perform(event, hint) it "does not trigger sentry and re-raises" do expect { FailedJob.perform_now }.to raise_error(FailedJob::TestError) - expect(transport.events.size).to eq(0) end end @@ -386,4 +388,42 @@ def perform(event, hint) end end end + + describe "active_job_report_after_job_retries" do + before do + allow(Sentry::Rails::ActiveJobExtensions::SentryReporter) + .to receive(:capture_exception) + .and_call_original + end + context "when active_job_report_after_job_retries is false" do + it "reports 3 exceptions" do + assert_performed_jobs 3 do + FailedJobWithRetryOn.perform_later rescue nil + end + + expect(Sentry::Rails::ActiveJobExtensions::SentryReporter) + .to have_received(:capture_exception) + .exactly(3).times + end + end + context "when active_job_report_after_job_retries is true" do + before do + Sentry.configuration.rails.active_job_report_after_job_retries = true + end + + after do + Sentry.configuration.rails.active_job_report_after_job_retries = false + end + + it "reports 1 exception" do + assert_performed_jobs 3 do + FailedJobWithRetryOn.perform_later rescue nil + end + + expect(Sentry::Rails::ActiveJobExtensions::SentryReporter) + .to have_received(:capture_exception) + .exactly(1).times + end + end + end end