Skip to content

feat(sampling): Support for OpenTelemetry consistent tracestate sampling - #12397

Open
MilanGarnier wants to merge 9 commits into
masterfrom
milan.garnier/ot.th
Open

MilanGarnier wants to merge 9 commits into
masterfrom
milan.garnier/ot.th

Conversation

@MilanGarnier

@MilanGarnier MilanGarnier commented Sep 3, 2026

Copy link
Copy Markdown
Contributor

What Does This Do

Adds OpenTelemetry consistent probability-sampling behavior on top of the
ot tracestate parsing introduced by #12405.

  • Validates and normalizes rv and th.
  • Generates probability state for loaded agent rates and sampling rules.
  • Preserves inherited randomness and unknown fields across overrides.
  • Keeps limiter outcomes distinct from raw probability decisions.
  • Adds focused parsing, propagation, continuation, and sampling tests.

Motivation

Make Java tracer sampling decisions interoperable with OpenTelemetry
consistent probability sampling and provide the state needed for downstream
tracestate propagation.

Additional Notes

Contributor Checklist

  • Format the title according to the contribution guidelines.
  • Assign the required type: and comp: labels.

Jira ticket: APMAPI-2171

@MilanGarnier MilanGarnier added type: feature Enhancements and improvements comp: context propagation Trace context propagation tag: ai generated Largely based on code generated by an AI or LLM labels Sep 3, 2026
@MilanGarnier MilanGarnier changed the title Support OpenTelemetry tracestate sampling feat(sampling): Support for OpenTelemetry consistent tracestate sampling Sep 3, 2026
@datadog-datadog-prod-us1-2

This comment has been minimized.

@dd-octo-sts

dd-octo-sts Bot commented Sep 3, 2026

Copy link
Copy Markdown
Contributor

🟢 Java Benchmark SLOs — All performance SLOs passed

Suite Status
Startup 🟢 pass

SLO thresholds are defined here based on automatically generated metrics. A warning is raised when results are within 5% of the threshold.

PR vs. master results
Scenario Candidate master Δ (95% CI of mean)
startup:insecure-bank:iast:Agent 14.74 s 14.66 s [-0.5%; +1.5%] (no difference)
startup:insecure-bank:tracing:Agent 13.69 s 13.71 s [-1.1%; +0.9%] (no difference)
startup:petclinic:appsec:Agent 17.65 s 17.51 s [-0.2%; +1.8%] (no difference)
startup:petclinic:iast:Agent 17.41 s 17.65 s [-2.1%; -0.6%] (maybe better)
startup:petclinic:profiling:Agent 17.34 s 17.40 s [-1.6%; +1.0%] (no difference)
startup:petclinic:sca:Agent 17.50 s 17.36 s [-0.0%; +1.7%] (no difference)
startup:petclinic:tracing:Agent 16.51 s 16.66 s [-2.1%; +0.2%] (no difference)

Commit: 34ef4a24 · CI Pipeline · Benchmarking Platform UI


Load and DaCapo benchmarks can be triggered manually in the GitLab pipeline. Results will appear in the Benchmarking Platform UI after completion.

@MilanGarnier
MilanGarnier changed the base branch from master to milan.garnier/preserve-deterministic-sampler-rate September 3, 2026 13:59
Base automatically changed from milan.garnier/preserve-deterministic-sampler-rate to master September 3, 2026 18:45
@MilanGarnier
MilanGarnier changed the base branch from master to milan.garnier/otel-tracestate-refactor September 4, 2026 09:30
@MilanGarnier
MilanGarnier force-pushed the milan.garnier/ot.th branch 5 times, most recently from 7a1ddea to a0176c9 Compare September 8, 2026 14:26
@MilanGarnier

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a0176c99c0

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread dd-trace-core/src/main/java/datadog/trace/core/DDSpanContext.java Outdated
@MilanGarnier
MilanGarnier marked this pull request as ready for review September 10, 2026 09:02
@MilanGarnier
MilanGarnier requested review from a team as code owners September 10, 2026 09:02
@MilanGarnier
MilanGarnier requested review from mhlidd and removed request for a team September 10, 2026 09:02
@MilanGarnier
MilanGarnier force-pushed the milan.garnier/otel-tracestate-refactor branch from db321c0 to 1c02400 Compare September 10, 2026 09:02

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 629892d492

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread dd-trace-core/src/main/java/datadog/trace/core/propagation/HttpCodec.java Outdated
Comment thread dd-trace-core/src/main/java/datadog/trace/core/DDSpan.java Outdated

@datadog-datadog-prod-us1-2 datadog-datadog-prod-us1-2 Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Datadog Autotest: FAIL

Compound extraction can emit a W3C sampled flag that conflicts with the propagated OTel probability threshold when propagation styles have different sampling decisions.

Open Bits AI session

🤖 Datadog Autotest · Commit 3c81345 · What is Autotest? · @DataDog review to ask questions · Any feedback? Reach out in #autotest

boolean sampled = sampler.sample(span);
int samplingPriority = sampled ? PrioritySampling.SAMPLER_KEEP : PrioritySampling.SAMPLER_DROP;

Boolean probabilitySamplingResult = rates.hasAgentRates() ? sampled : null;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

probabilitySamplingResult is gated on rates.hasAgentRates() — a flag for the whole RateSamplersByEnvAndService snapshot — rather than whether this span's sampling decision actually came from an agent-provided rate.

hasAgentRates is set true as soon as any entry in the agent's rate update is non-null (line ~121), for the entire snapshot across all env/service combinations. But rates.getSampler(env, serviceName) can still fall through to fallbackSampler (the hardcoded DEFAULT_SAMPLER, not agent-derived) for any env/service combination the agent didn't send a rate for. In that case sampled comes from the default sampler, yet probabilitySamplingResult is still computed as non-null (since hasAgentRates is true snapshot-wide) — so DDSpan.setSamplingPriority(..., probabilitySamplingResult) injects an OTel consistent-sampling probability decision (rv/th) into the span's tracestate that misrepresents an unrelated default-sampler decision as agent-rate-based probability sampling.

Compare with RuleBasedTraceSampler elsewhere in this PR, which correctly ties probabilitySamplingResult to the matched rule for that specific span rather than a class-wide flag. This one should probably check whether sampler (the one actually used for this span) came from an agent rate vs. the fallback, not whether the snapshot contains any agent rate at all.

original.startsWith(DATADOG_MEMBER_KEY, memberStart)
|| original.startsWith(OTEL_MEMBER_KEY, memberStart);
if (!managedMember) {
boolean datadogMember = original.startsWith(DATADOG_MEMBER_KEY, memberStart);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

appendOtelAndVendorMembers, when called with preserveDatadogMember=true (the new OTLP-export path via getW3CTracestate(int)), copies every member matching original.startsWith(DATADOG_MEMBER_KEY, memberStart) verbatim into the output. Contrast with fromHeaderValue's extraction parser (line 73), which guards the same check with ddMemberIndex == -1 so only the first dd= member is ever recognized/kept.

If an inbound W3C tracestate somehow contains more than one dd= member (malformed, but stored verbatim as tracestate at extraction since nothing here validates uniqueness on the way in), this loop will re-emit all of them into the OTLP-exported traceState, producing a tracestate with duplicate list-member keys — invalid per the W3C tracestate spec (https://www.w3.org/TR/trace-context/#tracestate-header-field-values, "duplicated with the same key MUST be discarded"). Worth applying the same first-occurrence guard here that extraction already uses.


String tracestate = propagationTags.getW3CTracestate();
int samplingPriority = span.samplingPriority();
// TODO Cache the effective tracestate once per trace.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

getW3CTracestate(samplingPriority) bypasses headerCache entirely and unconditionally calls W3CPTagsCodec.updateOtelTraceState(this, resolved), which fully re-parses the original tracestate and rebuilds it with a fresh StringBuilder — even though OtelSamplingDecision.resolve() already caches the resolved decision per trace. For an N-span trace exported via OTLP (this call runs once per span, same TODO duplicated in OtlpTraceProto.java:95), that's O(N) redundant identical string rebuilds instead of O(1).

Given this is on the OTLP export hot path and scales with span count, this is worth fixing rather than leaving as a TODO — every tracer feature that adds a per-span cost here compounds for every other product exporting via OTLP. A per-trace cache keyed on the resolved OtelTraceState/OtelSamplingDecision (mirroring how headerCache already works for headerValue()) would turn this back into O(1) per trace.

@MilanGarnier
MilanGarnier marked this pull request as draft September 11, 2026 15:59
@MilanGarnier

Copy link
Copy Markdown
Contributor Author

I got more ideas today on how to have a cleaner design so I'll refactor this before re-asking for review

@MilanGarnier
MilanGarnier removed this pull request from stack #12406 September 14, 2026 13:01
Base automatically changed from milan.garnier/otel-tracestate-refactor to master September 14, 2026 14:41
@MilanGarnier
MilanGarnier added this pull request to stack #12487 September 14, 2026 16:17
@MilanGarnier

Copy link
Copy Markdown
Contributor Author

Since the PR is a bit big (~1100 lines of feature, 1000 of tests), I've rewritten the history into a set of commits that should each be independently reviewable and testable.

@MilanGarnier

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: e9d42f767a

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@MilanGarnier
MilanGarnier marked this pull request as ready for review September 15, 2026 09:47
@MilanGarnier
MilanGarnier removed this pull request from stack #12487 September 15, 2026 09:47

@datadog-datadog-prod-us1-2 datadog-datadog-prod-us1-2 Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Datadog Autotest: FAIL

A matching traceparent and Datadog priority can keep an OTel threshold that gives the opposite sampling decision. Reinjection then sends conflicting sampling data.

Open Bits AI session

🤖 Datadog Autotest · Commit d324e16 · What is Autotest? · @DataDog review to ask questions · Any feedback? Reach out in #autotest

Comment on lines +790 to +795
SamplingState currentState = samplingState;
this.samplingState =
newSamplingState(
currentState.getSamplingPriority(),
tracestate,
otelTraceState,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 Remove a threshold that conflicts with the sampled flag

A downstream OTel sampler can drop a trace that traceparent marks as sampled.

Assertion details
  • Input: Use a sampled traceparent with tracestate: dd=s:1,ot=rv:00000000000000;th:8.
  • Expected: Remove the threshold when the parsed OTel state conflicts with the final sampling priority.
  • Actual: Extraction keeps th:8. Injection then sends a sampled traceparent with an ot state that means drop.
Suggested change
SamplingState currentState = samplingState;
this.samplingState =
newSamplingState(
currentState.getSamplingPriority(),
tracestate,
otelTraceState,
SamplingState currentState = samplingState;
int samplingPriority = currentState.getSamplingPriority();
if (otelTraceState != null
&& samplingPriority != PrioritySampling.UNSET
&& !otelTraceState.isConsistentWith(samplingPriority > 0)) {
otelTraceState = otelTraceState.withoutThreshold();
}
this.samplingState =
newSamplingState(
samplingPriority,
tracestate,
otelTraceState,

Was this helpful? React 👍 or 👎
🤖 Datadog Autotest · What is Autotest? · @DataDog review to ask questions · Any feedback? Reach out in #autotest · Open Bits AI session

@mcculls
mcculls self-requested a review September 15, 2026 10:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp: context propagation Trace context propagation tag: ai generated Largely based on code generated by an AI or LLM type: feature Enhancements and improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants