Skip to content

Commit

Permalink
Set root op instead of nested spans
Browse files Browse the repository at this point in the history
  • Loading branch information
frederikspang committed Oct 22, 2024
1 parent af385ba commit cd3a1df
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 22 deletions.
24 changes: 11 additions & 13 deletions sentry-sidekiq/lib/sentry/sidekiq/sentry_context_middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
module Sentry
module Sidekiq
class SentryContextServerMiddleware
OP_NAME = "queue.sidekiq"
OP_NAME = "queue.process"
SPAN_ORIGIN = "auto.queue.sidekiq"

def call(worker, job, queue)
Expand All @@ -23,20 +23,18 @@ def call(worker, job, queue)
scope.set_contexts(sidekiq: job.merge("queue" => queue))
scope.set_transaction_name(context_filter.transaction_name, source: :task)
transaction = start_transaction(scope, job["trace_propagation_headers"])
scope.set_span(transaction) if transaction

if transaction
scope.set_span(transaction)

transaction.set_data("messaging.message.id", job["jid"])
transaction.set_data("messaging.destination.name", queue)
transaction.set_data("messaging.message.receive.latency", ((Time.now.to_f - job["enqueued_at"]) * 1000).to_i)
transaction.set_data("messaging.message.retry.count", job["retry_count"] || 0)
end

begin
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
yield
rescue
finish_transaction(transaction, 500)
raise
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,17 @@

transaction = transport.events[0]
expect(transaction).not_to be_nil
expect(transaction.spans.count).to eq(1)
expect(transaction.spans[0][:data]['messaging.message.id']).to eq('123123') # Default defined in #execute_worker
expect(transaction.spans[0][:data]['messaging.destination.name']).to eq('default')
expect(transaction.spans[0][:data]['messaging.message.retry.count']).to eq(0)
expect(transaction.spans.count).to eq(0)
expect(transaction.contexts[:trace][:data]['messaging.message.id']).to eq('123123') # Default defined in #execute_worker
expect(transaction.contexts[:trace][:data]['messaging.destination.name']).to eq('default')
expect(transaction.contexts[:trace][:data]['messaging.message.retry.count']).to eq(0)

transaction = transport.events[1]
expect(transaction).not_to be_nil
expect(transaction.spans.count).to eq(1)
expect(transaction.spans[0][:data]['messaging.message.id']).to eq('123456') # Explicitly set above.
expect(transaction.spans[0][:data]['messaging.destination.name']).to eq('default')
expect(transaction.spans[0][:data]['messaging.message.retry.count']).to eq(0)
expect(transaction.spans.count).to eq(0)
expect(transaction.contexts[:trace][:data]['messaging.message.id']).to eq('123456') # Explicitly set above.
expect(transaction.contexts[:trace][:data]['messaging.destination.name']).to eq('default')
expect(transaction.contexts[:trace][:data]['messaging.message.retry.count']).to eq(0)
end

context "with trace_propagation_headers" do
Expand Down
2 changes: 1 addition & 1 deletion sentry-sidekiq/spec/sentry/sidekiq_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ def retry_last_failed_job
expect(transaction.contexts.dig(:trace, :trace_id)).to be_a(String)
expect(transaction.contexts.dig(:trace, :span_id)).to be_a(String)
expect(transaction.contexts.dig(:trace, :status)).to eq("ok")
expect(transaction.contexts.dig(:trace, :op)).to eq("queue.sidekiq")
expect(transaction.contexts.dig(:trace, :op)).to eq("queue.process")
end

it "records transaction with exception" do
Expand Down

0 comments on commit cd3a1df

Please sign in to comment.