Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use attempt_threshold to skip reporting on first N attempts #2503

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions sentry-sidekiq/lib/sentry/sidekiq/error_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,19 @@ def call(ex, context, sidekiq_config = nil)
end
end

# Check if the retry count is below the attempt_threshold
attempt_threshold = context.dig(:job, "attempt_threshold")
if attempt_threshold && retryable?(context)
attempt_threshold = attempt_threshold.to_i
retry_count = context.dig(:job, "retry_count")
# attempt 1 - retry_count is nil
# attempt 2 - this is your first retry so retry_count is 0
# attempt 3 - you have retried once, retry_count is 1
attempt = retry_count.nil? ? 1 : retry_count.to_i + 2

return if attempt < attempt_threshold
end

Sentry::Sidekiq.capture_exception(
ex,
contexts: { sidekiq: context_filter.filtered },
Expand Down
41 changes: 34 additions & 7 deletions sentry-sidekiq/spec/sentry/sidekiq_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,18 +109,45 @@
expect(retry_set.count).to eq(1)
end

def retry_last_failed_job
retry_set.first.add_to_queue
job = queue.first
work = Sidekiq::BasicFetch::UnitOfWork.new('queue:default', job.value)
process_work(processor, work)
end

context "with attempt_threshold" do
it "doesn't report the error until attempts equal the threshold" do
worker = Class.new(SadWorker)
worker.sidekiq_options attempt_threshold: 3

execute_worker(processor, worker)
expect(transport.events.count).to eq(0)

retry_last_failed_job
expect(transport.events.count).to eq(0)

retry_last_failed_job
expect(transport.events.count).to eq(1)
end

it "doesn't report the error when threshold is not reached" do
worker = Class.new(SadWorker)
worker.sidekiq_options attempt_threshold: 3

execute_worker(processor, worker)
expect(transport.events.count).to eq(0)

retry_last_failed_job
expect(transport.events.count).to eq(0)
end
end

context "with config.report_after_job_retries = true" do
before do
Sentry.configuration.sidekiq.report_after_job_retries = true
end

def retry_last_failed_job
retry_set.first.add_to_queue
job = queue.first
work = Sidekiq::BasicFetch::UnitOfWork.new('queue:default', job.value)
process_work(processor, work)
end

context "when retry: is specified" do
it "doesn't report the error until retries are exhuasted" do
worker = Class.new(SadWorker)
Expand Down
Loading