Skip to content

fix(otel): Propagate the continued trace from SentryPropagator - #1442

Draft
sentry-junior[bot] wants to merge 2 commits into
masterfrom
fix/1441-otel-inject-sentry-trace
Draft

sentry-junior[bot] wants to merge 2 commits into
masterfrom
fix/1441-otel-inject-sentry-trace

Conversation

@sentry-junior

@sentry-junior sentry-junior Bot commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

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.

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]

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]>
@sentry-junior
sentry-junior Bot requested a review from a team as a code owner December 1, 2026 15:13
Co-Authored-By: Daniel Szoke <[email protected]>
@linear-code

linear-code Bot commented Dec 2, 2026

Copy link
Copy Markdown

RUST-346

@szokeasaurusrex
szokeasaurusrex marked this pull request as draft December 2, 2026 15:40
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> =

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_start looks up the parent's Sentry span to nest the new one, and inserts the new one.
  • on_end removes the Sentry span and finishes it.
  • The event processor that SentrySpanProcessor::new registers 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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SentryPropagator injects a new trace ID after continuing an incoming sentry-trace

1 participant