Skip to content

Commit ac46c1b

Browse files
committed
Add childspan for Sidekiq Queue instrumentation
1 parent c3bcfa0 commit ac46c1b

3 files changed

Lines changed: 26 additions & 7 deletions

File tree

sentry-sidekiq/lib/sentry/sidekiq/sentry_context_middleware.rb

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class SentryContextServerMiddleware
88
OP_NAME = "queue.sidekiq"
99
SPAN_ORIGIN = "auto.queue.sidekiq"
1010

11-
def call(_worker, job, queue)
11+
def call(worker, job, queue)
1212
return yield unless Sentry.initialized?
1313

1414
context_filter = Sentry::Sidekiq::ContextFilter.new(job)
@@ -26,8 +26,18 @@ def call(_worker, job, queue)
2626
scope.set_span(transaction) if transaction
2727

2828
begin
29-
yield
30-
rescue
29+
Sentry.with_child_span(op: "queue.process", description: "Process #{worker.class.name}") do |span|
30+
# Set span data
31+
if span
32+
span.set_data("messaging.message.id", job["jid"])
33+
span.set_data("messaging.destination.name", queue)
34+
span.set_data("messaging.message.receive.latency", ((Time.now.to_f - job["enqueued_at"]) * 1000).to_i)
35+
span.set_data("messaging.message.retry.count", job["retry_count"] || 0)
36+
end
37+
38+
yield
39+
end
40+
rescue => ex
3141
finish_transaction(transaction, 500)
3242
raise
3343
end
@@ -63,13 +73,22 @@ def finish_transaction(transaction, status)
6373
end
6474

6575
class SentryContextClientMiddleware
66-
def call(_worker_class, job, _queue, _redis_pool)
76+
def call(worker_class, job, queue, _redis_pool)
6777
return yield unless Sentry.initialized?
6878

6979
user = Sentry.get_current_scope.user
7080
job["sentry_user"] = user unless user.empty?
7181
job["trace_propagation_headers"] ||= Sentry.get_trace_propagation_headers
72-
yield
82+
83+
Sentry.with_child_span(op: "queue.publish", description: "Enqueue #{worker_class}") do |span|
84+
# Set span data
85+
if span
86+
span.set_data("messaging.message.id", job["jid"])
87+
span.set_data("messaging.destination.name", queue)
88+
end
89+
90+
yield
91+
end
7392
end
7493
end
7594
end

sentry-sidekiq/spec/sentry/sidekiq/sentry_context_middleware_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
execute_worker(processor, HappyWorker, trace_propagation_headers: trace_propagation_headers)
7272

7373
expect(transport.events.count).to eq(1)
74+
7475
transaction = transport.events[0]
7576
expect(transaction).not_to be_nil
7677
expect(transaction.contexts.dig(:trace, :trace_id)).to eq(parent_transaction.trace_id)

sentry-sidekiq/spec/spec_helper.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,13 +227,12 @@ def sidekiq_config(opts)
227227

228228
def execute_worker(processor, klass, **options)
229229
klass_options = klass.sidekiq_options_hash || {}
230-
231230
# for Ruby < 2.6
232231
klass_options.each do |k, v|
233232
options[k.to_sym] = v
234233
end
235234

236-
msg = Sidekiq.dump_json(jid: "123123", class: klass, args: [], **options)
235+
msg = Sidekiq.dump_json(created_at: Time.now.to_f, jid: "123123", class: klass, args: [], **options)
237236
work = Sidekiq::BasicFetch::UnitOfWork.new('queue:default', msg)
238237
process_work(processor, work)
239238
end

0 commit comments

Comments
 (0)