Skip to content

feat(otel): otel sampling support in distributed tracing - #10117

Open
vpellan wants to merge 19 commits into
masterfrom
vpellan/otel-threshold-random-value
Open

feat(otel): otel sampling support in distributed tracing#10117
vpellan wants to merge 19 commits into
masterfrom
vpellan/otel-threshold-random-value

Conversation

@vpellan

@vpellan vpellan commented Sep 2, 2026

Copy link
Copy Markdown

What does this PR do?

Add support for OTel sampling in distributed tracing, by reading and emitting ot.th and ot.rv tags. These tags will be forwarded to services using OTel SDKs, that should forward them to the backend.

Motivation

Distributed tracing will work between DD-instrumented services and OTel-instrumented services, downstream OTel services will be able to forward the th field it to the collector and backend, which will calculate metrics correctly

Additional Notes

Locally passes DataDog/system-tests#7518

@vpellan
vpellan requested review from a team as code owners September 2, 2026 14:21
@vpellan
vpellan requested a review from BridgeAR September 2, 2026 14:21
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 2, 2026

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review Completed 2026-09-07T12:22:28.127430Z c0614ce New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@dd-octo-sts

dd-octo-sts Bot commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

Overall package size

Self size: 8.84 MB
Deduped: 9.51 MB
No deduping: 9.51 MB

Dependency sizes | name | version | self size | total size | |------|---------|-----------|------------| | import-in-the-middle | 3.4.0 | 127.33 kB | 447.04 kB | | opentracing | 0.14.7 | 194.81 kB | 194.81 kB | | dc-polyfill | 0.1.11 | 25.74 kB | 25.74 kB |

🤖 This report was automatically generated by heaviest-objects-in-the-universe

@datadog-prod-us1-5

datadog-prod-us1-5 Bot commented Sep 2, 2026

Copy link
Copy Markdown

Tests

⚠️ Warnings

⚠️ Your PR has warnings. Tests failed — unable to determine if these are from required jobs.

🧪 3 Tests failed in 1 job

LLMObs | claude-agent-sdk — ❌ 3 tests failed

View more details · View in GitHub Actions

❌ Plugin claude-agent-sdk with @anthropic-ai/claude-agent-sdk >=0.2.113 (0.3.259) instruments a full agentic call with subagents from with @anthropic-ai/claude-agent-sdk >=0.2.113 (0.3.259)   View in Datadog
Claude Code returned an error result: Failed to authenticate. API Error: 401 �@������tu���zup����n
f�n&*j�6cA$&��	��e��&���{ua�j3G��LR�dR.���+�e:��Ҽx�T.d���ڮ;s>���;��T����z�n

Error: Claude Code returned an error result: Failed to authenticate. API Error: 401 �@������tu���zup����n
f�n&*j�6cA$&��	��e��&���{ua�j3G��LR�dR.���+�e:��Ҽx�T.d���ڮ;s>���;��T����z�n
    at Gy.readMessages (file:///home/runner/work/dd-trace-js/dd-trace-js/versions/@anthropic-ai/claude-agent-sdk@0/node_modules/@anthropic-ai/claude-agent-sdk/sdk.mjs:41643:20)
    at process.processTicksAndRejections (node:internal/process/task_queues:104:5)
❌ esm with @anthropic-ai/claude-agent-sdk >=0.2.113 (0.3.259) is instrumented named from with @anthropic-ai/claude-agent-sdk >=0.2.113 (0.3.259)   View in Datadog
Process exited with status code 1.

Error: Process exited with status code 1.
    at ChildProcess.<anonymous> (integration-tests/helpers/index.js:266:25)
    at Object.onceWrapper (node:events:631:26)
    at ChildProcess.emit (node:events:509:28)
    at ChildProcess._handle.onexit (node:internal/child_process:295:12)
❌ esm with @anthropic-ai/claude-agent-sdk >=0.2.113 (0.3.259) is instrumented named-from-namespace from with @anthropic-ai/claude-agent-sdk >=0.2.113 (0.3.259)   View in Datadog
Process exited with status code 1.

Error: Process exited with status code 1.
    at ChildProcess.<anonymous> (integration-tests/helpers/index.js:266:25)
    at Object.onceWrapper (node:events:631:26)
    at ChildProcess.emit (node:events:509:28)
    at ChildProcess._handle.onexit (node:internal/child_process:295:12)

ℹ️ Info

No other issues found (see more)

❄️ No new flaky tests detected

🎯 Code Coverage (details)
Patch Coverage: 100.00%
Overall Coverage: 98.04% (-0.19%)

Useful? React with 👍 / 👎

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: c0614ce | Docs | View more details | Give us feedback!

@vpellan
vpellan requested a review from a team as a code owner September 2, 2026 14:25

@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: 49eb6259f6

ℹ️ About Codex in GitHub

Your team has set up Codex to 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 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread packages/dd-trace/src/opentracing/propagation/text_map.js Outdated
Comment thread packages/dd-trace/src/priority_sampler.js Outdated
Comment thread packages/dd-trace/src/opentracing/propagation/text_map.js Outdated
Comment thread packages/dd-trace/src/opentracing/propagation/text_map.js Outdated
@pr-commenter

pr-commenter Bot commented Sep 2, 2026

Copy link
Copy Markdown

Benchmarks

Benchmark execution time: 2026-09-07 12:21:41

Comparing candidate commit c0614ce in PR branch vpellan/otel-threshold-random-value with baseline commit ca391e1 in branch master.

📊 Benchmarking dashboard

Found 0 performance improvements and 6 performance regressions! Performance is the same for 2294 metrics, 13 unstable metrics.

Explanation

This is an A/B test comparing a candidate commit's performance against that of a baseline commit. Performance changes are noted in the tables below as:

  • 🟩 = significantly better candidate vs. baseline
  • 🟥 = significantly worse candidate vs. baseline

We compute a confidence interval (CI) over the relative difference of means between metrics from the candidate and baseline commits, considering the baseline as the reference.

If the CI is entirely outside the configured SIGNIFICANT_IMPACT_THRESHOLD (or the deprecated UNCONFIDENCE_THRESHOLD), the change is considered significant.

Feel free to reach out to #apm-benchmarking-platform on Slack if you have any questions.

More details about the CI and significant changes

You can imagine this CI as a range of values that is likely to contain the true difference of means between the candidate and baseline commits.

CIs of the difference of means are often centered around 0%, because often changes are not that big:

---------------------------------(------|---^--------)-------------------------------->
                              -0.6%    0%  0.3%     +1.2%
                                 |          |        |
         lower bound of the CI --'          |        |
sample mean (center of the CI) -------------'        |
         upper bound of the CI ----------------------'

As described above, a change is considered significant if the CI is entirely outside the configured SIGNIFICANT_IMPACT_THRESHOLD (or the deprecated UNCONFIDENCE_THRESHOLD).

For instance, for an execution time metric, this confidence interval indicates a significantly worse performance:

----------------------------------------|---------|---(---------^---------)---------->
                                       0%        1%  1.3%      2.2%      3.1%
                                                  |   |         |         |
       significant impact threshold --------------'   |         |         |
                      lower bound of CI --------------'         |         |
       sample mean (center of the CI) --------------------------'         |
                      upper bound of CI ----------------------------------'

scenario:sampling-agent-24

  • 🟥 cpu_user_time [+65.418ms; +167.840ms] or [+2.571%; +6.597%]
  • 🟥 execution_time [+62.655ms; +164.327ms] or [+2.456%; +6.441%]
  • 🟥 throughput [-305573.477op/s; -117285.292op/s] or [-6.463%; -2.481%]

scenario:sampling-rate-limited-24

  • 🟥 cpu_user_time [+61.003ms; +142.031ms] or [+2.479%; +5.772%]
  • 🟥 execution_time [+60.344ms; +142.705ms] or [+2.445%; +5.782%]
  • 🟥 throughput [-212416.101op/s; -90841.961op/s] or [-5.784%; -2.473%]

Unstable benchmarks

These benchmarks have a confidence interval too wide to call a change; treat them as noise rather than signal.

scenario:appsec-appsec-enabled-24

  • unstable max_rss_usage [-25.995MB; +8.428MB] or [-9.185%; +2.978%]

scenario:appsec-iast-no-vulnerability-iast-enabled-default-config-20

  • unstable max_rss_usage [-16652.960KB; +15828.960KB] or [-7.214%; +6.857%]

scenario:appsec-iast-with-vulnerability-iast-enabled-default-config-20

  • unstable max_rss_usage [-16.310MB; +22.749MB] or [-4.729%; +6.595%]

scenario:encoders-0.4-immediate-flush-20

  • unstable max_rss_usage [-2.992MB; +13.256MB] or [-3.913%; +17.338%]

scenario:llmobs-encode-unicode-mixed-20

  • unstable max_rss_usage [-6.295MB; +9.622MB] or [-8.067%; +12.330%]

scenario:llmobs-span-processor-retrieval-20

  • unstable execution_time [-135.336ms; +195.163ms] or [-4.586%; +6.613%]

scenario:plugin-graphql-long-with-depth-and-collapse-off-24

  • unstable cpu_user_time [-554.276ms; +603.041ms] or [-5.058%; +5.503%]
  • unstable execution_time [-569.226ms; +604.238ms] or [-5.122%; +5.437%]
  • unstable throughput [-2.185op/s; +2.019op/s] or [-5.359%; +4.952%]

scenario:plugin-graphql-long-with-depth-on-max-26

  • unstable max_rss_usage [-13.989MB; +21.468MB] or [-7.589%; +11.647%]

scenario:plugin-pg-service-24

  • unstable cpu_user_time [-116.943ms; +185.680ms] or [-3.878%; +6.158%]
  • unstable execution_time [-118.234ms; +185.203ms] or [-3.911%; +6.127%]

scenario:plugin-pg-service-26

  • unstable execution_time [-94.396ms; +127.005ms] or [-5.818%; +7.828%]

@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: 42005a60a5

ℹ️ About Codex in GitHub

Your team has set up Codex to 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 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread packages/dd-trace/src/opentelemetry/span_context.js
Comment thread packages/dd-trace/src/priority_sampler.js
Comment thread packages/dd-trace/src/opentracing/propagation/text_map.js
Comment thread packages/dd-trace/src/knuth-hash.js
# Conflicts:
#	.github/CODEOWNERS
#	packages/dd-trace/src/opentracing/propagation/text_map.js
#	packages/dd-trace/test/opentracing/propagation/text_map.spec.js

@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: 660cb44f39

ℹ️ About Codex in GitHub

Your team has set up Codex to 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 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread packages/dd-trace/src/otel-sampling.js Outdated
@codecov

codecov Bot commented Sep 3, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.65%. Comparing base (ca391e1) to head (c0614ce).
⚠️ Report is 5 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff            @@
##           master   #10117    +/-   ##
========================================
  Coverage   98.65%   98.65%            
========================================
  Files        1005     1007     +2     
  Lines      157435   157824   +389     
  Branches       74       74            
========================================
+ Hits       155312   155701   +389     
  Misses       2123     2123            
Flag Coverage Δ
ai-guard 65.01% <65.36%> (-0.02%) ⬇️
apm-capabilities 63.23% <100.00%> (+0.13%) ⬆️
apm-integrations 81.48% <89.85%> (+0.05%) ⬆️
appsec 58.82% <88.76%> (+0.10%) ⬆️
debugger 69.76% <62.55%> (-0.08%) ⬇️
instrumentation 53.84% <84.74%> (+0.11%) ⬆️
llmobs 79.02% <81.36%> (+<0.01%) ⬆️
master-coverage 98.65% <100.00%> (?)
openfeature 66.36% <61.01%> (-0.06%) ⬇️
platform 69.37% <71.69%> (+0.05%) ⬆️
profiling 66.68% <60.45%> (-0.09%) ⬇️
serverless 65.82% <83.05%> (+0.09%) ⬆️
test-optimization 82.69% <79.66%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@vpellan vpellan added the AI Generated Largely based on code generated by an AI or LLM. This label is the same across all dd-trace-* repos label Sep 4, 2026

@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: cf02963117

ℹ️ About Codex in GitHub

Your team has set up Codex to 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 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +710 to +712
const selectedPriority = firstSpanContext._sampling.priority
if (selectedPriority !== undefined &&
(selectedPriority >= AUTO_KEEP) !== (w3cSpanContext._sampling.priority >= AUTO_KEEP)) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Reconcile missing selected priorities before copying OTel state

When extraction prefers B3 or Datadog and that header omits its sampling decision, selectedPriority is undefined, so this guard does nothing while line 717 still copies the matching W3C ot member. A child span then makes a local probability decision, but buildOtelMember() preserves the already-valid inbound rv/th; for example, an unsampled W3C rv:00000000000000;th:8 can be reinjected with a locally sampled traceparent. The fresh case not covered by the prior conflict fix is this missing, rather than disagreeing, selected priority; either inherit the W3C decision or discard/rebuild its sampling fields before local sampling.

Useful? React with 👍 / 👎.

Comment on lines +216 to +217
if (isSamplingPriorityTag(key)) {
this._prioritySampler.setPriorityFromTag(this, key, value)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Validate sampling-priority values before overriding

After sampling has already been materialized—for example by injection or reading OTel traceFlags—calling setTag('sampling.priority', 1n) or passing a Symbol now reaches Math.trunc() through this unconditional override path and throws a TypeError. Previously the defined priority made this path a no-op, so the same late tag could not crash the application; validate the value's type before attempting the override.

AGENTS.md reference: AGENTS.md:L109-L109

Useful? React with 👍 / 👎.

Comment on lines 248 to 249
samplingTags = tags
mayChangeSamplingPriority = true

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Avoid rescanning stale sampling tags in the v5 fallback

With DD_MAJOR < 6, every string or array passed to addTags() now marks sampling as changed and passes the entire live tag map to setPriorityFromTags(), even when the new input contains no sampling tag. For example, after addTags('manual.keep:true') and a later setTag('manual.drop', true) establish a drop, an unrelated addTags('foo:bar') rescans both stored tags, gives manual.keep precedence, and silently flips the trace back to keep. Detect sampling keys in the newly parsed input, or avoid reapplying old tags for unrelated additions.

AGENTS.md reference: AGENTS.md:L119-L122

Useful? React with 👍 / 👎.

@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: c0614ce61b

ℹ️ About Codex in GitHub

Your team has set up Codex to 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 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +141 to +143
const context = this._getContext(span)
const priority = this._getPriorityFromTag(key, value, context)
if (this.validate(priority)) this.setPriority(span, priority)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Preserve product force-keeps from late manual tags

When AppSec or AI Guard has already called keepTrace(..., product), a subsequent span.setTag('manual.drop', true) now reaches this unconditional setPriority call, replaces the product's USER_KEEP with USER_REJECT, and clears its decision-maker tag. Previously, sampling tags were ignored once any priority was established, so security or evaluation traces could not be dropped merely because the tag was applied after the product decision; the final code fixes refreshing automatic decisions but should restrict that override to automatic decisions rather than product force-keeps (and apply the same restriction to the batch-tag helper).

Useful? React with 👍 / 👎.

Comment on lines +713 to +715
} else if ((selectedPriority >= AUTO_KEEP) !== (w3cSpanContext._sampling.priority >= AUTO_KEEP)) {
// The W3C threshold describes its sampled bit, not the conflicting decision selected from another style.
firstSpanContext._sampling.isProbabilityDecision = false

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Clear thresholds for matching B3 debug decisions

When B3 is the selected extraction style and carries its debug flag (d or X-B3-Flags: 1), getB3Priority() produces the non-probabilistic USER_KEEP, but a matching W3C header with its sampled bit set also satisfies this equality check. The newly copied W3C tracestate therefore retains ot.th, so reinjection represents the B3 force-keep as a probability decision and downstream metrics can apply an incorrect sampling weight. The final condition handles sampled-bit disagreement, but this same-bit non-probability case is fresh evidence that debug contexts also need isProbabilityDecision = false before the tracestate is merged.

Useful? React with 👍 / 👎.

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

Labels

AI Generated Largely based on code generated by an AI or LLM. This label is the same across all dd-trace-* repos semver-minor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant