diff --git a/sentry-rails/spec/sentry/rails/capture_context_spec.rb b/sentry-rails/spec/sentry/rails/capture_context_spec.rb index 46fb0162e..a0c2fea4e 100644 --- a/sentry-rails/spec/sentry/rails/capture_context_spec.rb +++ b/sentry-rails/spec/sentry/rails/capture_context_spec.rb @@ -8,12 +8,18 @@ def self.captured_trace_ids @captured_trace_ids ||= [] end + def self.captured_span_ids + @captured_span_ids ||= [] + end + def initialize(app) @app = app end def call(env) - self.class.captured_trace_ids << Sentry.get_current_scope.get_trace_context[:trace_id] + trace_context = Sentry.get_current_scope.get_trace_context + self.class.captured_trace_ids << trace_context[:trace_id] + self.class.captured_span_ids << trace_context[:span_id] @app.call(env) end end @@ -47,6 +53,7 @@ def propagation_context_in_app(env) context "when composed with CaptureExceptions", type: :request do before do CaptureContextSpecProbe.captured_trace_ids.clear + CaptureContextSpecProbe.captured_span_ids.clear end context "without tracing enabled" do @@ -78,6 +85,15 @@ def propagation_context_in_app(env) end end + it "points a span_id captured before CaptureExceptions at the started transaction" do + get "/world" + + transaction = transport.events.last + early_span_id = CaptureContextSpecProbe.captured_span_ids.first + + expect(early_span_id).to eq(transaction.contexts.dig(:trace, :span_id)) + end + it "keeps the same trace_id from before CaptureExceptions through the started transaction" do get "/world" diff --git a/sentry-ruby/lib/sentry/propagation_context.rb b/sentry-ruby/lib/sentry/propagation_context.rb index ff7ec5092..a62bd5d52 100644 --- a/sentry-ruby/lib/sentry/propagation_context.rb +++ b/sentry-ruby/lib/sentry/propagation_context.rb @@ -163,6 +163,7 @@ def initialize(scope, env = nil) def transaction_options { trace_id: trace_id, + span_id: span_id, parent_span_id: parent_span_id, parent_sampled: parent_sampled, baggage: baggage, diff --git a/sentry-ruby/spec/sentry/rack/capture_exceptions_spec.rb b/sentry-ruby/spec/sentry/rack/capture_exceptions_spec.rb index 7b1111de7..09b4e396e 100644 --- a/sentry-ruby/spec/sentry/rack/capture_exceptions_spec.rb +++ b/sentry-ruby/spec/sentry/rack/capture_exceptions_spec.rb @@ -128,6 +128,7 @@ def propagation_context_in_app(stack_env) expect(transaction.type).to eq("transaction") expect(transaction.contexts.dig(:trace, :trace_id)).to eq(context_in_app.trace_id) + expect(transaction.contexts.dig(:trace, :span_id)).to eq(context_in_app.span_id) expect(transaction.contexts.dig(:trace, :parent_span_id)).to be_nil end end diff --git a/sentry-ruby/spec/sentry_spec.rb b/sentry-ruby/spec/sentry_spec.rb index 77430216b..bc3abe434 100644 --- a/sentry-ruby/spec/sentry_spec.rb +++ b/sentry-ruby/spec/sentry_spec.rb @@ -1196,6 +1196,14 @@ expect(transaction.baggage.mutable).to eq(false) end + it "gives the Transaction the propagation context's span_id" do + Sentry.configuration.traces_sample_rate = 1.0 + + transaction = described_class.continue_trace(env, name: "foobar") + + expect(transaction.span_id).to eq(Sentry.get_current_scope.propagation_context.span_id) + end + describe "sample_rand propagation" do before do Sentry.configuration.traces_sample_rate = 1.0 diff --git a/spec/features/trace_context_spec.rb b/spec/features/trace_context_spec.rb index fb8138e3c..b6e0afb4b 100644 --- a/spec/features/trace_context_spec.rb +++ b/spec/features/trace_context_spec.rb @@ -18,6 +18,13 @@ def request_transaction .to eq(request_transaction.dig("contexts", "trace", "trace_id")) end + it "points a log emitted before CaptureExceptions at the transaction's span" do + without_trace_propagation { make_request("/trace_context") } + + expect(early_middleware_logs.first["span_id"]) + .to eq(request_transaction.dig("contexts", "trace", "span_id")) + end + it "continues an incoming distributed trace in a log emitted before CaptureExceptions" do incoming_trace_id = propagated_trace_id