Skip to content

Commit

Permalink
Add childspan for Sidekiq Queue instrumentation
Browse files Browse the repository at this point in the history
  • Loading branch information
frederikspang committed Sep 17, 2024
1 parent c3bcfa0 commit ac46c1b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
29 changes: 24 additions & 5 deletions sentry-sidekiq/lib/sentry/sidekiq/sentry_context_middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class SentryContextServerMiddleware
OP_NAME = "queue.sidekiq"
SPAN_ORIGIN = "auto.queue.sidekiq"

def call(_worker, job, queue)
def call(worker, job, queue)
return yield unless Sentry.initialized?

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

begin
yield
rescue
Sentry.with_child_span(op: "queue.process", description: "Process #{worker.class.name}") do |span|
# Set span data
if span
span.set_data("messaging.message.id", job["jid"])
span.set_data("messaging.destination.name", queue)
span.set_data("messaging.message.receive.latency", ((Time.now.to_f - job["enqueued_at"]) * 1000).to_i)
span.set_data("messaging.message.retry.count", job["retry_count"] || 0)
end

yield
end
rescue => ex
finish_transaction(transaction, 500)
raise
end
Expand Down Expand Up @@ -63,13 +73,22 @@ def finish_transaction(transaction, status)
end

class SentryContextClientMiddleware
def call(_worker_class, job, _queue, _redis_pool)
def call(worker_class, job, queue, _redis_pool)
return yield unless Sentry.initialized?

user = Sentry.get_current_scope.user
job["sentry_user"] = user unless user.empty?
job["trace_propagation_headers"] ||= Sentry.get_trace_propagation_headers
yield

Sentry.with_child_span(op: "queue.publish", description: "Enqueue #{worker_class}") do |span|
# Set span data
if span
span.set_data("messaging.message.id", job["jid"])
span.set_data("messaging.destination.name", queue)
end

yield
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
execute_worker(processor, HappyWorker, trace_propagation_headers: trace_propagation_headers)

expect(transport.events.count).to eq(1)

transaction = transport.events[0]
expect(transaction).not_to be_nil
expect(transaction.contexts.dig(:trace, :trace_id)).to eq(parent_transaction.trace_id)
Expand Down
3 changes: 1 addition & 2 deletions sentry-sidekiq/spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -227,13 +227,12 @@ def sidekiq_config(opts)

def execute_worker(processor, klass, **options)
klass_options = klass.sidekiq_options_hash || {}

# for Ruby < 2.6
klass_options.each do |k, v|
options[k.to_sym] = v
end

msg = Sidekiq.dump_json(jid: "123123", class: klass, args: [], **options)
msg = Sidekiq.dump_json(created_at: Time.now.to_f, jid: "123123", class: klass, args: [], **options)
work = Sidekiq::BasicFetch::UnitOfWork.new('queue:default', msg)
process_work(processor, work)
end
Expand Down

0 comments on commit ac46c1b

Please sign in to comment.