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

Tag job_id and provider_job_id on ActiveJob events #1259

Merged
merged 3 commits into from
Jan 31, 2021
Merged
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
1 change: 1 addition & 0 deletions sentry-rails/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- Fixes [#1071](https://github.com/getsentry/sentry-ruby/issues/1071)
- Make sentry-rails a Rails engine and provide default job class for async [#1181](https://github.com/getsentry/sentry-ruby/pull/1181)
- Allow users to configure ActiveJob adapters to ignore [#1256](https://github.com/getsentry/sentry-ruby/pull/1256)
- Tag `job_id` and `provider_job_id` on ActiveJob events [#1259](https://github.com/getsentry/sentry-ruby/pull/1259)

## Unreleased

Expand Down
2 changes: 2 additions & 0 deletions sentry-rails/examples/rails-6.0/app/jobs/error_job.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class ErrorJob < ApplicationJob
self.queue_adapter = :async

def perform
raise "Job failed"
end
Expand Down
28 changes: 15 additions & 13 deletions sentry-rails/lib/sentry/rails/active_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,14 @@ def capture_and_reraise_with_sentry(job, block)
rescue_handler_result = rescue_with_handler(e)
return rescue_handler_result if rescue_handler_result

Sentry::Rails.capture_exception(e, extra: sentry_context(job))
Sentry::Rails.capture_exception(
e,
extra: sentry_context(job),
tags: {
job_id: job.job_id,
provider_job_id: job.provider_job_id
}
)
raise e
end

Expand All @@ -34,19 +41,14 @@ def already_supported_by_specific_integration?(job)
end

def sentry_context(job)
ctx = {
:active_job => job.class.name,
:arguments => job.arguments,
:scheduled_at => job.scheduled_at,
:job_id => job.job_id,
:locale => job.locale
{
active_job: job.class.name,
arguments: job.arguments,
scheduled_at: job.scheduled_at,
job_id: job.job_id,
provider_job_id: job.provider_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
Expand Down
15 changes: 15 additions & 0 deletions sentry-rails/spec/sentry/rails/activejob_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,21 @@ def rescue_callback(error); end
MyActiveJob.queue_adapter = :inline
end

it "adds useful context to extra" do
job = FailedJob.new

expect { job.perform_now }.to raise_error(FailedJob::TestError)

event = transport.events.last.to_json_compatible
expect(event.dig("extra", "active_job")).to eq("FailedJob")
expect(event.dig("extra", "job_id")).to be_a(String)
expect(event.dig("extra", "provider_job_id")).to be_nil
expect(event.dig("extra", "arguments")).to eq([])

expect(event.dig("tags", "job_id")).to eq(event.dig("extra", "job_id"))
expect(event.dig("tags", "provider_job_id")).to eq(event.dig("extra", "provider_job_id"))
end

it "clears context" do
job = MyActiveJob.new

Expand Down