Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
913f397
commit 2ab5755
Showing
4 changed files
with
97 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,53 @@ | ||
module Raven | ||
class Rails | ||
module ActiveJob | ||
module ActiveJobExtensions | ||
ALREADY_SUPPORTED_SENTRY_ADAPTERS = %w( | ||
ActiveJob::QueueAdapters::SidekiqAdapter | ||
ActiveJob::QueueAdapters::DelayedJobAdapter | ||
).freeze | ||
|
||
def self.included(base) | ||
base.class_eval do | ||
rescue_from(Exception) do |exception| | ||
# Do not capture exceptions when using Sidekiq so we don't capture | ||
# The same exception twice. | ||
unless self.class.queue_adapter.to_s == 'ActiveJob::QueueAdapters::SidekiqAdapter' | ||
active_job_details = { | ||
:active_job => self.class.name, | ||
:arguments => arguments, | ||
:scheduled_at => scheduled_at, | ||
:job_id => job_id, | ||
:locale => locale | ||
} | ||
around_perform do |job, block| | ||
capture_and_reraise_with_sentry(job, block) | ||
end | ||
end | ||
end | ||
|
||
# Add provider_job_id details if Rails 5 | ||
if defined?(provider_job_id) | ||
active_job_details[:provider_job_id] = provider_job_id | ||
end | ||
def capture_and_reraise_with_sentry(job, block) | ||
block.call | ||
rescue Exception => exception # rubocop:disable Lint/RescueException | ||
return if already_supported_by_specific_integration?(job) | ||
Raven.capture_exception(exception, :extra => raven_context(job)) | ||
raise exception | ||
ensure | ||
Context.clear! | ||
BreadcrumbBuffer.clear! | ||
end | ||
|
||
Raven.capture_exception(exception, :extra => active_job_details) | ||
raise exception | ||
end | ||
end | ||
def already_supported_by_specific_integration?(job) | ||
ALREADY_SUPPORTED_SENTRY_ADAPTERS.include?(job.class.queue_adapter.to_s) | ||
end | ||
|
||
def raven_context(job) | ||
ctx = { | ||
:active_job => job.class.name, | ||
:arguments => job.arguments, | ||
:scheduled_at => job.scheduled_at, | ||
:job_id => job.job_id, | ||
:locale => job.locale | ||
} | ||
# Add provider_job_id details if Rails 5 | ||
if job.respond_to?(:provider_job_id) | ||
ctx[:provider_job_id] = job.provider_job_id | ||
end | ||
|
||
ctx | ||
end | ||
end | ||
end | ||
end | ||
|
||
class ActiveJob::Base | ||
include Raven::Rails::ActiveJobExtensions | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
require "spec_helper" | ||
require "rspec/rails" | ||
require "raven/transports/dummy" | ||
require "raven/integrations/rails" | ||
require 'raven/integrations/rails/active_job' | ||
|
||
class MyActiveJob < ActiveJob::Base | ||
self.queue_adapter = :inline | ||
self.logger = nil | ||
|
||
class TestError < RuntimeError | ||
end | ||
|
||
def perform | ||
raise TestError, "Boom!" | ||
end | ||
end | ||
|
||
describe MyActiveJob do | ||
before(:each) do | ||
Raven.client.transport.events = [] | ||
end | ||
|
||
it "captures exceptions" do | ||
job = MyActiveJob.new | ||
|
||
expect { job.perform_now }.to raise_error(MyActiveJob::TestError) | ||
|
||
expect(Raven.client.transport.events.size).to eq(1) | ||
end | ||
|
||
it "clears context" do | ||
Raven.extra_context(:foo => :bar) | ||
job = MyActiveJob.new | ||
|
||
expect { job.perform_now }.to raise_error(MyActiveJob::TestError) | ||
event = JSON.parse!(Raven.client.transport.events.first[1]) | ||
|
||
expect(event["extra"]["foo"]).to eq("bar") | ||
|
||
Raven.client.transport.events = [] | ||
expect { job.perform_now }.to raise_error(MyActiveJob::TestError) | ||
event = JSON.parse!(Raven.client.transport.events.first[1]) | ||
|
||
expect(event["extra"]["foo"]).to eq(nil) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters