Skip to content

Commit

Permalink
Add option to notify Sentry only after the last retry on resque (#2087)
Browse files Browse the repository at this point in the history
  • Loading branch information
GlauberrBatista authored Oct 10, 2023
1 parent 65ef04c commit 71ec4f4
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Record client reports for profiles [#2107](https://github.com/getsentry/sentry-ruby/pull/2107)
- Adopt Rails 7.1's new BroadcastLogger [#2120](https://github.com/getsentry/sentry-ruby/pull/2120)
- Support sending events after all retries were performed (sentry-resque) [#2087](https://github.com/getsentry/sentry-ruby/pull/2087)
- Add [Cron Monitoring](https://docs.sentry.io/product/crons/) support
- Add `Sentry.capture_check_in` API for Cron Monitoring [#2117](https://github.com/getsentry/sentry-ruby/pull/2117)

Expand Down
2 changes: 2 additions & 0 deletions sentry-resque/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ gem 'simplecov'
gem "simplecov-cobertura", "~> 1.4"
gem "rexml"

gem "resque-retry", "~> 1.8"

if RUBY_VERSION.to_f >= 2.6
gem "debug", github: "ruby/debug", platform: :ruby
gem "irb"
Expand Down
7 changes: 7 additions & 0 deletions sentry-resque/lib/sentry/resque.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ def record(queue, worker, payload, &block)

finish_transaction(transaction, 200)
rescue Exception => exception
klass = payload['class'].constantize

raise if Sentry.configuration.resque.report_after_job_retries &&
defined?(::Resque::Plugins::Retry) == 'constant' &&
klass.is_a?(::Resque::Plugins::Retry) &&
!klass.retry_limit_reached?

::Sentry::Resque.capture_exception(exception, hint: { background: false })
finish_transaction(transaction, 500)
raise
Expand Down
83 changes: 83 additions & 0 deletions sentry-resque/spec/sentry/resque_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,21 @@ def self.perform
end
end

class FailedRetriableJob
extend Resque::Plugins::Retry

@queue = :default
@retry_limit = 3

def self.perform
1/0
end
end

class FailedZeroRetriesJob < FailedRetriableJob
@retry_limit = 0
end

class TaggedFailedJob
def self.perform
Sentry.set_tags(number: 1)
Expand Down Expand Up @@ -125,6 +140,74 @@ def self.perform(msg)
end
end

context "with ResqueRetry" do
context "when report_after_job_retries is true" do
before do
Sentry.configuration.resque.report_after_job_retries = true
end

it "reports exception only on the last run" do
expect do
Resque::Job.create(:default, FailedRetriableJob)
process_job(worker)
end.to change { Resque::Stat.get("failed") }.by(1)
.and change { transport.events.count }.by(0)

expect do
3.times do
process_job(worker)
end
end.to change { transport.events.count }.by(1)

event = transport.events.last.to_hash

expect(event[:sdk]).to eq({ name: "sentry.ruby.resque", version: described_class::VERSION })
expect(event.dig(:exception, :values, 0, :type)).to eq("ZeroDivisionError")
expect(event[:tags]).to eq({ "resque.queue" => "default" })
end

it "reports exception on first run when retry_count is 0" do
expect do
Resque::Job.create(:default, FailedZeroRetriesJob)
process_job(worker)
end.to change { Resque::Stat.get("failed") }.by(1)
.and change { transport.events.count }.by(1)

event = transport.events.last.to_hash

expect(event[:sdk]).to eq({ name: "sentry.ruby.resque", version: described_class::VERSION })
expect(event.dig(:exception, :values, 0, :type)).to eq("ZeroDivisionError")
expect(event[:tags]).to eq({ "resque.queue" => "default" })
end
end

context "when report_after_job_retries is false" do
before do
Sentry.configuration.resque.report_after_job_retries = false
end

it "reports exeception all the runs" do
expect do
Resque::Job.create(:default, FailedRetriableJob)
process_job(worker)
end.to change { Resque::Stat.get("failed") }.by(1)
.and change { transport.events.count }.by(1)

expect do
3.times do
process_job(worker)
end
end.to change { transport.events.count }.by(3)

event = transport.events.last.to_hash

expect(event[:sdk]).to eq({ name: "sentry.ruby.resque", version: described_class::VERSION })
expect(event.dig(:exception, :values, 0, :type)).to eq("ZeroDivisionError")
expect(event[:tags]).to eq({ "resque.queue" => "default" })
end
end
end

context "with ActiveJob" do
require "rails"
require "active_job"
Expand Down
1 change: 1 addition & 0 deletions sentry-resque/spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require "debug" if RUBY_VERSION.to_f >= 2.6 && RUBY_ENGINE == "ruby"

require "resque"
require "resque-retry"

# To workaround https://github.com/steveklabnik/mono_logger/issues/13
# Note: mono_logger is resque's default logger
Expand Down

0 comments on commit 71ec4f4

Please sign in to comment.