Repository navigation
fix(otel): Propagate the continued trace from SentryPropagator - #1442
sentry-junior[bot] wants to merge 2 commits into
Conversation
After `SentryPropagator` extracts an incoming `sentry-trace` header, `SentrySpanProcessor` continues that trace in Sentry, but the OTEL span gets a new trace ID. `inject_context` built the outgoing `sentry-trace` header from the OTEL span context, so services downstream of a Rust service started a new trace. `inject_context` now writes the headers of the Sentry span that `SentrySpanProcessor` created for the OTEL span, and falls back to the OTEL span context when there is none. The outgoing sampled flag now also carries Sentry's sampling decision instead of OTEL's. The OTEL propagators of the Python, Ruby, Go, and Java SDKs inject from the Sentry span in the same way. Reproduction: getsentry/repro#96 Fixes #1441 Co-Authored-By: Daniel Szoke <[email protected]>
Co-Authored-By: Daniel Szoke <[email protected]>
| type SpanMap = Arc<Mutex<HashMap<sentry_core::protocol::SpanId, TransactionOrSpan>>>; | ||
|
|
||
| static SPAN_MAP: LazyLock<SpanMap> = LazyLock::new(|| Arc::new(Mutex::new(HashMap::new()))); | ||
| pub(crate) static SPAN_MAP: LazyLock<SpanMap> = |
There was a problem hiding this comment.
I'm concerned about making this SPAN_MAP public. Tbh, I did not myself develop this integration, so I am not totally sure what this does. But if it is conceptually part of the processor, I would worry to make it part of the propagator.
Walk me through this decision and the existing architecture
There was a problem hiding this comment.
It's pub(crate), not pub: it stays out of the crate's public API and is only visible inside sentry-opentelemetry.
What SPAN_MAP is
It's a process-wide map from span ID to the live Sentry TransactionOrSpan (L28-L34). Each Sentry span is created with the same span ID as its OTEL span, so the map is the only link from an OTEL span to its Sentry span. It's used in three places:
on_startlooks up the parent's Sentry span to nest the new one, and inserts the new one.on_endremoves the Sentry span and finishes it.- The event processor that
SentrySpanProcessor::newregisters on the scope (L43-L60) looks up the Sentry span of the active OTEL span, to put its trace and span IDs on events.
So it's already shared beyond the SpanProcessor trait methods. It acts as the module's registry of live Sentry spans.
Why the propagator needs it
The outgoing sentry-trace header has to carry Sentry's trace ID and sampling decision. Those live only on the Sentry span. The Context that inject_context receives holds only the OTEL span context. The processor can't put the Sentry span into that Context either: on_start gets a &Context, not the context the caller keeps using afterwards. That leaves the map as the only path.
The OTEL propagators of the other SDKs with this architecture do the same thing. They read the processor's span map in inject: Python, Ruby, Go, and Java.
Narrower option
If the concern is the propagator locking and reading the map directly, SPAN_MAP can stay private. processor.rs would expose one pub(crate) function instead, such as fn sentry_trace_headers(span_id: &opentelemetry::SpanId) -> Option<TraceHeadersIter>. The processor would then still be the only code that touches the map, and the propagator would only ask it for the headers of a span. Behavior is the same. I can switch to that if you prefer it.
#1443 alone does not remove the need for this. It makes the OTEL trace ID match, but the sampled flag would still come from OTEL's decision rather than Sentry's.
After
SentryPropagatorextracts an incomingsentry-traceheader,SentrySpanProcessorcontinues that trace in Sentry, but the OTEL span gets a new trace ID.inject_contextbuilt the outgoingsentry-traceheader from the OTEL span context, so services downstream of a Rust service started a new trace.inject_contextnow writes the headers of the Sentry span thatSentrySpanProcessorcreated for the OTEL span, and falls back to the OTEL span context when there is none. The outgoing sampled flag now also carries Sentry's sampling decision instead of OTEL's. The OTEL propagators of the Python, Ruby, Go, and Java SDKs inject from the Sentry span in the same way.A follow-up PR stacked on this one makes the OTEL span adopt the incoming trace ID on extract, as those SDKs also do.
Reproduction: getsentry/repro#96
Fixes #1441
Fixes RUST-346
via @szokeasaurusrex.
--
View Session [Sentry]