Skip to content

Commit 3bc2cfa

Browse files
solnicgithub-copilot
andcommitted
feat(active_job): set scope tags and context on consumer like Sidekiq
Mirror Sidekiq's scope enrichment: set a 'queue' scope tag and an 'active_job' context block (job_class, job_id, queue, provider_job_id) on every event captured within the consumer scope, including the transaction and any captured errors. Co-Authored-By: github-copilot <noreply@example.com>
1 parent a4e2010 commit 3bc2cfa

2 files changed

Lines changed: 44 additions & 0 deletions

File tree

sentry-rails/lib/sentry/rails/active_job.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,13 @@ def record(job, trace_headers: nil, user: nil, &block)
104104
begin
105105
scope.set_user(user) if user && !user.empty?
106106
scope.set_transaction_name(job.class.name, source: :task)
107+
scope.set_tags(queue: job.queue_name)
108+
scope.set_contexts(active_job: {
109+
job_class: job.class.name,
110+
job_id: job.job_id,
111+
queue: job.queue_name,
112+
provider_job_id: job.provider_job_id
113+
})
107114

108115
transaction_options = {
109116
name: scope.transaction_name,

sentry-rails/spec/active_job/shared_examples/tracing/consumer_transaction.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,43 @@ def perform
3232
expect(transaction.contexts.dig(:trace, :status)).to eq("ok")
3333
end
3434

35+
it "sets queue scope tag on the consumer transaction" do
36+
successful_job.set(queue: "important").perform_later
37+
drain
38+
39+
transaction = sentry_events.find { |e| e.is_a?(Sentry::TransactionEvent) }
40+
expect(transaction).not_to be_nil
41+
expect(transaction.tags[:queue]).to eq("important")
42+
end
43+
44+
it "sets active_job context on the consumer transaction" do
45+
successful_job.perform_later
46+
drain
47+
48+
transaction = sentry_events.find { |e| e.is_a?(Sentry::TransactionEvent) }
49+
expect(transaction).not_to be_nil
50+
51+
ctx = transaction.contexts[:active_job]
52+
expect(ctx).not_to be_nil
53+
expect(ctx[:job_class]).to eq(successful_job.name)
54+
expect(ctx[:job_id]).to be_a(String).and(satisfy { |v| !v.empty? })
55+
expect(ctx[:queue]).to eq("default")
56+
end
57+
58+
it "sets active_job context on the error event" do
59+
expect do
60+
failing_job.perform_later
61+
drain
62+
end.to raise_error(RuntimeError, /boom from tracing spec/)
63+
64+
error_event = sentry_events.find { |e| e.is_a?(Sentry::ErrorEvent) }
65+
expect(error_event).not_to be_nil
66+
67+
ctx = error_event.contexts[:active_job]
68+
expect(ctx).not_to be_nil
69+
expect(ctx[:job_class]).to eq(failing_job.name)
70+
end
71+
3572
it "records a db.sql.active_record child span when the job performs a query" do
3673
query_job = job_fixture do
3774
def perform

0 commit comments

Comments
 (0)