Repository navigation
feat(tracing): Send the dynamic sampling context as the envelope's trace header - #1275
Open
dirkholzapfel wants to merge 1 commit into
Open
dirkholzapfel wants to merge 1 commit into
dirkholzapfel wants to merge 1 commit into
Conversation
…ace header
Transaction envelopes carried no trace header, so Sentry saw no client
sample rate: throughput and count charts showed the raw kept share, and
server-side sampling had no client decision to be consistent with.
Sentry.OpenTelemetry.DynamicSamplingContext builds the context. At the head
of a trace it takes the sampler's rate, random value and decision from the
root span's tracestate, plus the DSN's public key, release, environment,
transaction name and org id. A trace continued from upstream keeps the
context it arrived with in the baggage header, as is: the span processor
keeps the incoming baggage on the stored span record from the span's start,
and the sentry- members are parsed with keys trimmed, properties dropped,
values percent-decoded and members that are no UTF-8 dropped; a baggage
without a trace ID is no context. A span that ends after its transaction
was sent carries the context that transaction went out with, kept in the
sent-span marker. The envelope sends the context as its trace header and
keeps it out of the transaction item.
An envelope without an event ID starts with {} instead of {{}}, which was
not JSON.
dirkholzapfel
requested review from
a team,
solnic and
whatyouhide
as code owners
November 25, 2026 13:47
Contributor
PR risk: mediumFor commit Main concerns: data sent to Sentry, host performance, public contract. This label is a hint for reviewers, not an approval. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Transaction envelopes carry no
traceheader today, so Sentry receives no dynamic sampling context from this SDK. On a production app on 14.0.1 withtraces_sample_rate: 0.01, every span showsserver_sample_rate 1.0and noclient_sample_rate: the throughput and count charts show the raw kept share, extrapolation has nothing to work with, and server-side sampling has no client decision to stay consistent with.This PR builds the dynamic sampling context from what the SDK already knows and sends it as the envelope's
traceheader (spec), in a module of its own,Sentry.OpenTelemetry.DynamicSamplingContext:build/2:sample_rate,sample_randandsampledfrom the root span's tracestate, whereSentry.OpenTelemetry.Samplerwrites them at the sampling decision (it exposes a reader for them now);trace_id;public_keyfrom the DSN;releaseandenvironmentfrom the config;transaction, the transaction's name; andorg_idwhen known, configured or taken from the DSN host as the propagator already does.from_baggage/1: the context it arrived with, as is ("frozen", per the spec). The propagator keeps the incomingbaggageheader in the OpenTelemetry context; the span processor copies it onto the stored span record at the span's start, the only point where that context is at hand. Thesentry-members are parsed with one comprehension: keys trimmed, properties after;dropped, values percent-decoded, and a member whose key or value is no UTF-8 after decoding dropped, so a malformed header cannot fail the envelope's encoding. A baggage withoutsentry-trace_idis no context and the service builds its own: this SDK's propagator itself sends a baggage of onlysentry-org_idwhen it heads a trace and knows an org id.SpanStorage.The context lives on
Sentry.Transactionasdynamic_sampling_context(listed int/0) and stays out of the transaction item (to_payload/1drops it).Sentry.Envelope.from_transaction/1puts it on the envelope, andto_binary/1encodes the envelope headers as JSON.While there: an envelope without an event ID started with
{{}}, which is not JSON; it starts with{}now, and the test that pinned{{}}is updated.This mirrors what the other SDKs did when they added the dynamic sampling context, sentry-ruby#1869 and sentry-javascript#5292, minus the outgoing baggage.
Not in this PR, happy to follow up on either:
baggageheaders built from the same context. The propagator'sinject/4builds no context of its own today: it forwards an incoming baggage and addssentry-org_idto it, or sends onlysentry-org_idwhen there is none, so a trace headed by this SDK gives a downstream service no rate and no decision to continue with.DynamicSamplingContextis where that baggage would come from.sentry-tracesampled flag of a continued trace. Today the propagator creates the remote parent as always-sampled and leaves the decision to the sampler, which decides again at the local rate, while the frozen context reports the upstream's rate and decision, as the spec asks. Both agree once the sampler inherits.Issues
None open for this; I can file one if you want the discussion there.