From cd3a1df20eb357498f91bf4217c1667e4d4839aa Mon Sep 17 00:00:00 2001 From: Frederik Erbs Spang Thomsen Date: Tue, 22 Oct 2024 10:31:26 +0200 Subject: [PATCH] Set root op instead of nested spans --- .../sidekiq/sentry_context_middleware.rb | 24 +++++++++---------- .../sidekiq/sentry_context_middleware_spec.rb | 16 ++++++------- sentry-sidekiq/spec/sentry/sidekiq_spec.rb | 2 +- 3 files changed, 20 insertions(+), 22 deletions(-) diff --git a/sentry-sidekiq/lib/sentry/sidekiq/sentry_context_middleware.rb b/sentry-sidekiq/lib/sentry/sidekiq/sentry_context_middleware.rb index e7a7afdd3..740b2082b 100644 --- a/sentry-sidekiq/lib/sentry/sidekiq/sentry_context_middleware.rb +++ b/sentry-sidekiq/lib/sentry/sidekiq/sentry_context_middleware.rb @@ -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) @@ -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 diff --git a/sentry-sidekiq/spec/sentry/sidekiq/sentry_context_middleware_spec.rb b/sentry-sidekiq/spec/sentry/sidekiq/sentry_context_middleware_spec.rb index fcdedd2d8..ffb24e443 100644 --- a/sentry-sidekiq/spec/sentry/sidekiq/sentry_context_middleware_spec.rb +++ b/sentry-sidekiq/spec/sentry/sidekiq/sentry_context_middleware_spec.rb @@ -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 diff --git a/sentry-sidekiq/spec/sentry/sidekiq_spec.rb b/sentry-sidekiq/spec/sentry/sidekiq_spec.rb index 64fdec5c1..4c788c9ce 100644 --- a/sentry-sidekiq/spec/sentry/sidekiq_spec.rb +++ b/sentry-sidekiq/spec/sentry/sidekiq_spec.rb @@ -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