From 5c329fe7bce8bfc6c1a399c20405477e99fede4d Mon Sep 17 00:00:00 2001 From: Daniel Szoke Date: Tue, 22 Sep 2026 14:51:13 +0200 Subject: [PATCH] fix: Use strict trace continuation for `continue_from_span` The function `continue_from_span` allows continuing a distributed trace from a span that is spawned on another thread. Typically, both spans in this case are from the same Client, but if they are attached to different Clients it is also possible that they are associated with different org IDs. Anyways, to keep the handling of distributed traces consistent, it makes sense to populate `incoming_trace` in the `continue_from_span` method. As we will be adding more things to `IncomingTrace`, we should be using it consistently. --- CHANGELOG.md | 4 ++ sentry-core/src/performance/mod.rs | 68 +++++++++++++++++++++++++----- 2 files changed, 61 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 99716fdd..17c3d6e9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ - Deprecated [`Client::sample_should_send`](https://docs.rs/sentry-core/0.49.4/sentry_core/struct.Client.html#method.sample_should_send). We will remove this method from the public API in the next breaking release ([#1328](https://github.com/getsentry/sentry-rust/pull/1328)). +### Fixes + +- [`TransactionContext::continue_from_span`](https://docs.rs/sentry-core/0.49.4/sentry_core/struct.TransactionContext.html#method.continue_from_span) now respects trace continuation rules: spans being recorded onto a client that reports to a different Sentry organization are no longer continued by this function. We also respect the `strict_trace_continuation` option ([#1329](https://github.com/getsentry/sentry-rust/pull/1329)). + ## 0.49.3 ### Features diff --git a/sentry-core/src/performance/mod.rs b/sentry-core/src/performance/mod.rs index 5cf12f5e..23f2d79b 100644 --- a/sentry-core/src/performance/mod.rs +++ b/sentry-core/src/performance/mod.rs @@ -8,7 +8,7 @@ use std::time::SystemTime; use sentry_types::protocol::v7::client_report::Reason as ClientReportReason; #[cfg(feature = "client")] use sentry_types::protocol::v7::OrganizationId; -use sentry_types::protocol::v7::SpanId; +use sentry_types::protocol::v7::{SpanId, TraceId}; #[cfg(feature = "client")] use self::sampling::FinishAction; @@ -271,24 +271,53 @@ impl TransactionContext { /// thread and should be connected to the calling thread via a distributed /// tracing transaction. pub fn continue_from_span(name: &str, op: &str, span: Option) -> Self { + /// Helper struct for the match statement; this allows us to have `incoming_trace` be + /// guarded by the client feature. + struct IncomingSpanData { + trace_id: TraceId, + span_id: SpanId, + #[cfg(feature = "client")] + incoming_trace: IncomingTrace, + sampled: Option, + } + let span = match span { Some(span) => span, None => return Self::new(name, op), }; - let (trace_id, parent_span_id, sampled) = match span { + let IncomingSpanData { + trace_id, + span_id: parent_span_id, + #[cfg(feature = "client")] + incoming_trace, + sampled, + } = match span { TransactionOrSpan::Transaction(transaction) => { let inner = transaction.inner.lock().unwrap(); - ( - inner.context.trace_id, - inner.context.span_id, - inner.tracing_state.trace_sampled(), - ) + IncomingSpanData { + trace_id: inner.context.trace_id, + span_id: inner.context.span_id, + #[cfg(feature = "client")] + incoming_trace: IncomingTrace { + org_id: inner.client.as_ref().and_then(|c| c.org_id()), + }, + sampled: inner.tracing_state.trace_sampled(), + } } TransactionOrSpan::Span(span) => { - let trace_sampled = span.tracing_state.trace_sampled(); - let span = span.span.lock().unwrap(); - (span.trace_id, span.span_id, trace_sampled) + let sampled = span.tracing_state.trace_sampled(); + let protocol_span = span.span.lock().unwrap(); + + IncomingSpanData { + trace_id: protocol_span.trace_id, + span_id: protocol_span.span_id, + #[cfg(feature = "client")] + incoming_trace: IncomingTrace { + org_id: span.org_id(), + }, + sampled, + } } }; @@ -300,7 +329,7 @@ impl TransactionContext { span_id: protocol::SpanId::default(), sampled, #[cfg(feature = "client")] - incoming_trace: None, + incoming_trace: incoming_trace.into(), custom: None, } } @@ -1084,6 +1113,17 @@ impl Transaction { span: Arc::new(Mutex::new(span)), } } + + /// Obtains the org_id of this transaction's [`Client`]. + #[cfg(feature = "client")] + pub(crate) fn org_id(&self) -> Option { + self.inner + .lock() + .unwrap_or_else(|e| e.into_inner()) + .client + .as_ref() + .and_then(|c| c.org_id()) + } } impl PartialEq for Transaction { @@ -1334,6 +1374,12 @@ impl Span { } } + /// Return the organization ID of the client this span is being recorded onto. + #[cfg(feature = "client")] + pub(crate) fn org_id(&self) -> Option { + self.transaction.org_id() + } + /// Starts a new child Span with the given `op` and `description`. /// /// The span must be explicitly finished via [`Span::finish`].