Skip to content

Commit 4a0f70e

Browse files
authoredJan 2, 2025
Use attempt_threshold to skip reporting on first N attempts (#2503)
Co-authored-by: Peter Solnica <peter@solnica.online> Closes #1632
1 parent 7fe7f88 commit 4a0f70e

File tree

3 files changed

+48
-8
lines changed

3 files changed

+48
-8
lines changed
 

‎CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
### Features
44

55
- Improve the accuracy of duration calculations in cron jobs monitoring ([#2471](https://github.com/getsentry/sentry-ruby/pull/2471))
6+
- Use attempt_threshold to skip reporting on first N attempts ([#2503](https://github.com/getsentry/sentry-ruby/pull/2503))
67
- Support `code.namespace` for Ruby 3.4+ stacktraces ([#2506](https://github.com/getsentry/sentry-ruby/pull/2506))
78

8-
99
### Bug fixes
1010

1111
- Default to `internal_error` error type for OpenTelemetry spans [#2473](https://github.com/getsentry/sentry-ruby/pull/2473)

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

+13
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,19 @@ def call(ex, context, sidekiq_config = nil)
3131
end
3232
end
3333

34+
# Check if the retry count is below the attempt_threshold
35+
attempt_threshold = context.dig(:job, "attempt_threshold")
36+
if attempt_threshold && retryable?(context)
37+
attempt_threshold = attempt_threshold.to_i
38+
retry_count = context.dig(:job, "retry_count")
39+
# attempt 1 - retry_count is nil
40+
# attempt 2 - this is your first retry so retry_count is 0
41+
# attempt 3 - you have retried once, retry_count is 1
42+
attempt = retry_count.nil? ? 1 : retry_count.to_i + 2
43+
44+
return if attempt < attempt_threshold
45+
end
46+
3447
Sentry::Sidekiq.capture_exception(
3548
ex,
3649
contexts: { sidekiq: context_filter.filtered },

‎sentry-sidekiq/spec/sentry/sidekiq_spec.rb

+34-7
Original file line numberDiff line numberDiff line change
@@ -109,18 +109,45 @@
109109
expect(retry_set.count).to eq(1)
110110
end
111111

112+
def retry_last_failed_job
113+
retry_set.first.add_to_queue
114+
job = queue.first
115+
work = Sidekiq::BasicFetch::UnitOfWork.new('queue:default', job.value)
116+
process_work(processor, work)
117+
end
118+
119+
context "with attempt_threshold" do
120+
it "doesn't report the error until attempts equal the threshold" do
121+
worker = Class.new(SadWorker)
122+
worker.sidekiq_options attempt_threshold: 3
123+
124+
execute_worker(processor, worker)
125+
expect(transport.events.count).to eq(0)
126+
127+
retry_last_failed_job
128+
expect(transport.events.count).to eq(0)
129+
130+
retry_last_failed_job
131+
expect(transport.events.count).to eq(1)
132+
end
133+
134+
it "doesn't report the error when threshold is not reached" do
135+
worker = Class.new(SadWorker)
136+
worker.sidekiq_options attempt_threshold: 3
137+
138+
execute_worker(processor, worker)
139+
expect(transport.events.count).to eq(0)
140+
141+
retry_last_failed_job
142+
expect(transport.events.count).to eq(0)
143+
end
144+
end
145+
112146
context "with config.report_after_job_retries = true" do
113147
before do
114148
Sentry.configuration.sidekiq.report_after_job_retries = true
115149
end
116150

117-
def retry_last_failed_job
118-
retry_set.first.add_to_queue
119-
job = queue.first
120-
work = Sidekiq::BasicFetch::UnitOfWork.new('queue:default', job.value)
121-
process_work(processor, work)
122-
end
123-
124151
context "when retry: is specified" do
125152
it "doesn't report the error until retries are exhuasted" do
126153
worker = Class.new(SadWorker)

0 commit comments

Comments
 (0)