Skip to content

feat(tracing): Send the dynamic sampling context as the envelope's trace header - #1275

Open
dirkholzapfel wants to merge 1 commit into
getsentry:masterfrom
dirkholzapfel:feat/dynamic-sampling-context-envelope-header
Open

dirkholzapfel wants to merge 1 commit into
getsentry:masterfrom
dirkholzapfel:feat/dynamic-sampling-context-envelope-header

Conversation

@dirkholzapfel

Copy link
Copy Markdown

Description

Transaction envelopes carry no trace header today, so Sentry receives no dynamic sampling context from this SDK. On a production app on 14.0.1 with traces_sample_rate: 0.01, every span shows server_sample_rate 1.0 and no client_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 trace header (spec), in a module of its own, Sentry.OpenTelemetry.DynamicSamplingContext:

  • At the head of a trace, build/2: sample_rate, sample_rand and sampled from the root span's tracestate, where Sentry.OpenTelemetry.Sampler writes them at the sampling decision (it exposes a reader for them now); trace_id; public_key from the DSN; release and environment from the config; transaction, the transaction's name; and org_id when known, configured or taken from the DSN host as the propagator already does.
  • For a trace continued from an upstream service, from_baggage/1: the context it arrived with, as is ("frozen", per the spec). The propagator keeps the incoming baggage header 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. The sentry- 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 without sentry-trace_id is no context and the service builds its own: this SDK's propagator itself sends a baggage of only sentry-org_id when it heads a trace and knows an org id.
  • Once sent, the context stays. A span that ends after its transaction was sent is reported as a follow-up transaction of the same trace and carries the context that transaction went out with, kept in the sent-span marker of SpanStorage.

The context lives on Sentry.Transaction as dynamic_sampling_context (listed in t/0) and stays out of the transaction item (to_payload/1 drops it). Sentry.Envelope.from_transaction/1 puts it on the envelope, and to_binary/1 encodes 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:

  • Outgoing baggage headers built from the same context. The propagator's inject/4 builds no context of its own today: it forwards an incoming baggage and adds sentry-org_id to it, or sends only sentry-org_id when there is none, so a trace headed by this SDK gives a downstream service no rate and no decision to continue with. DynamicSamplingContext is where that baggage would come from.
  • The sampler inheriting the sentry-trace sampled 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.

…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
dirkholzapfel requested review from a team, solnic and whatyouhide as code owners November 25, 2026 13:47
@github-actions github-actions Bot added the risk: medium PR risk score: medium label Nov 25, 2026
@github-actions

Copy link
Copy Markdown
Contributor

PR risk: medium

For commit 2908313.

Main concerns: data sent to Sentry, host performance, public contract.

This label is a hint for reviewers, not an approval.

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

Labels

risk: medium PR risk score: medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant