Skip to content

Commit dea9b82

Browse files
committed
Add config.sidekiq.report_after_job_retries
When set to true, `sentry-sidekiq` only reports exceptions when the job has been fully retried.
1 parent 878b33b commit dea9b82

File tree

6 files changed

+83
-0
lines changed

6 files changed

+83
-0
lines changed

sentry-sidekiq/lib/sentry-sidekiq.rb

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
require "sentry-ruby"
33
require "sentry/integrable"
44
require "sentry/sidekiq/version"
5+
require "sentry/sidekiq/configuration"
56
require "sentry/sidekiq/error_handler"
67
require "sentry/sidekiq/sentry_context_middleware"
78

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

sentry-sidekiq/lib/sentry/sidekiq/error_handler.rb

+9
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@ def call(ex, context)
1111
scope = Sentry.get_current_scope
1212
scope.set_transaction_name(context_filter.transaction_name) unless scope.transaction_name
1313

14+
retry_option = context.dig(:job, "retry")
15+
16+
if Sentry.configuration.sidekiq.report_after_job_retries && retry_option.is_a?(Integer)
17+
retry_count = context.dig(:job, "retry_count")
18+
if retry_count.nil? || retry_count < retry_option - 1
19+
return
20+
end
21+
end
22+
1423
Sentry::Sidekiq.capture_exception(
1524
ex,
1625
contexts: { sidekiq: context_filter.filtered },
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
require "spec_helper"
2+
3+
RSpec.describe Sentry::Sidekiq::Configuration do
4+
it "adds #delayed_job option to Sentry::Configuration" do
5+
config = Sentry::Configuration.new
6+
7+
expect(config.sidekiq).to be_a(described_class)
8+
end
9+
10+
describe "#report_after_job_retries" do
11+
it "has correct default value" do
12+
expect(subject.report_after_job_retries).to eq(false)
13+
end
14+
end
15+
end

sentry-sidekiq/spec/sentry/sidekiq_spec.rb

+27
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,33 @@
8989
expect(retry_set.count).to eq(1)
9090
end
9191

92+
context "with config.report_after_job_retries = true" do
93+
before do
94+
Sentry.configuration.sidekiq.report_after_job_retries = true
95+
end
96+
97+
it "doesn't report the error until retries are exhuasted" do
98+
execute_worker(processor, RetryWorker)
99+
100+
expect(transport.events.count).to eq(0)
101+
102+
expect(retry_set.count).to eq(1)
103+
104+
retry_set.first.add_to_queue
105+
job = queue.first
106+
work = Sidekiq::BasicFetch::UnitOfWork.new('queue:default', job.value)
107+
process_work(processor, work)
108+
expect(transport.events.count).to eq(1)
109+
end
110+
111+
it "doesn't affect no-retry jobs" do
112+
execute_worker(processor, SadWorker)
113+
114+
expect(transport.events.count).to eq(1)
115+
expect(retry_set.count).to eq(1)
116+
end
117+
end
118+
92119
context "when tracing is enabled" do
93120
before do
94121
perform_basic_setup do |config|

sentry-sidekiq/spec/spec_helper.rb

+10
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,16 @@ def perform
133133
end
134134
end
135135

136+
class RetryWorker
137+
include Sidekiq::Worker
138+
139+
sidekiq_options retry: 1
140+
141+
def perform
142+
1/0
143+
end
144+
end
145+
136146
def execute_worker(processor, klass)
137147
klass_options = klass.sidekiq_options_hash || {}
138148
options = {}

0 commit comments

Comments
 (0)