Skip to content

feat(sampling): OTel consistent-probability rv/th derivation (APMAPI-2181) - #2276

Merged
gh-worker-dd-mergequeue-cf854d[bot] merged 14 commits into
mainfrom
milan.garnier/otel-consistent-sampling
Aug 11, 2026
Merged

gh-worker-dd-mergequeue-cf854d[bot] merged 14 commits into
mainfrom
milan.garnier/otel-consistent-sampling

Conversation

@MilanGarnier

@MilanGarnier MilanGarnier commented Jul 27, 2026 •

Copy link
Copy Markdown
Contributor

Adds a public API to emit OpenTelemetry consistent-probability-sampling tracestate (ot.th/ot.rv). https://opentelemetry.io/docs/specs/otel/trace/tracestate-probability-sampling/

Part of APMAPI-2170 (OTel consistent probability sampling across dd-trace-*).

Changes

  • SamplingMechanism::is_probability() — classifies rate-driven mechanisms
  • OtelConsistentSampling { rv, th } (threshold, and rv as source of randomness derived from the trace id)
  • TraceRootSamplingInfo::otel_consistent_sampling(trace_id) returns Some(OtelConsistentSampling) for a probability decision

Tests

Tests from system-tests #7372
cargo test -p libdd-sampling

@datadog-datadog-prod-us1

datadog-datadog-prod-us1 Bot commented Jul 27, 2026 •

Copy link
Copy Markdown
Contributor

Tests

🎉 All green!

🧪 All tests passed
❄️ No new flaky tests detected

🎯 Code Coverage (details)
• Patch Coverage: 100.00%
• Overall Coverage: 75.81% (+0.04%)

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: fa97744 | Docs | Datadog PR Page | Give us feedback!

@pr-commenter

pr-commenter Bot commented Jul 27, 2026 •

Copy link
Copy Markdown

Benchmarks

Comparison

Benchmark execution time: 2026-08-11 15:48:59

Comparing candidate commit fa97744 in PR branch milan.garnier/otel-consistent-sampling with baseline commit 6ea3203 in branch main.

Found 0 performance improvements and 0 performance regressions! Performance is the same for 48 metrics, 10 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 ----------------------------------'

Candidate

Omitted due to size.

Baseline

Omitted due to size.

@dd-octo-sts

dd-octo-sts Bot commented Jul 27, 2026 •

Copy link
Copy Markdown
Contributor

Artifact Size Benchmark Report

aarch64-alpine-linux-musl
Artifact Baseline Commit Change
/aarch64-alpine-linux-musl/lib/libdatadog_profiling.a 89.18 MB 89.18 MB 0% (0 B) 👌
/aarch64-alpine-linux-musl/lib/libdatadog_profiling.so 8.20 MB 8.20 MB 0% (0 B) 👌
aarch64-unknown-linux-gnu
Artifact Baseline Commit Change
/aarch64-unknown-linux-gnu/lib/libdatadog_profiling.so 11.06 MB 11.06 MB 0% (0 B) 👌
/aarch64-unknown-linux-gnu/lib/libdatadog_profiling.a 100.36 MB 100.36 MB 0% (0 B) 👌
libdatadog-x64-windows
Artifact Baseline Commit Change
/libdatadog-x64-windows/debug/dynamic/datadog_profiling_ffi.dll 26.63 MB 26.63 MB 0% (0 B) 👌
/libdatadog-x64-windows/debug/dynamic/datadog_profiling_ffi.lib 94.56 KB 94.56 KB 0% (0 B) 👌
/libdatadog-x64-windows/debug/dynamic/datadog_profiling_ffi.pdb 191.41 MB 191.41 MB 0% (0 B) 👌
/libdatadog-x64-windows/debug/static/datadog_profiling_ffi.lib 1000.65 MB 1000.65 MB 0% (0 B) 👌
/libdatadog-x64-windows/release/dynamic/datadog_profiling_ffi.dll 8.71 MB 8.71 MB 0% (0 B) 👌
/libdatadog-x64-windows/release/dynamic/datadog_profiling_ffi.lib 94.56 KB 94.56 KB 0% (0 B) 👌
/libdatadog-x64-windows/release/dynamic/datadog_profiling_ffi.pdb 25.62 MB 25.62 MB 0% (0 B) 👌
/libdatadog-x64-windows/release/static/datadog_profiling_ffi.lib 51.01 MB 51.01 MB 0% (0 B) 👌
libdatadog-x86-windows
Artifact Baseline Commit Change
/libdatadog-x86-windows/debug/dynamic/datadog_profiling_ffi.dll 23.20 MB 23.20 MB 0% (0 B) 👌
/libdatadog-x86-windows/debug/dynamic/datadog_profiling_ffi.lib 96.04 KB 96.04 KB 0% (0 B) 👌
/libdatadog-x86-windows/debug/dynamic/datadog_profiling_ffi.pdb 196.23 MB 196.25 MB +0% (+16.00 KB) 👌
/libdatadog-x86-windows/debug/static/datadog_profiling_ffi.lib 990.10 MB 990.10 MB 0% (0 B) 👌
/libdatadog-x86-windows/release/dynamic/datadog_profiling_ffi.dll 6.73 MB 6.73 MB 0% (0 B) 👌
/libdatadog-x86-windows/release/dynamic/datadog_profiling_ffi.lib 96.04 KB 96.04 KB 0% (0 B) 👌
/libdatadog-x86-windows/release/dynamic/datadog_profiling_ffi.pdb 27.55 MB 27.55 MB 0% (0 B) 👌
/libdatadog-x86-windows/release/static/datadog_profiling_ffi.lib 48.56 MB 48.56 MB 0% (0 B) 👌
x86_64-alpine-linux-musl
Artifact Baseline Commit Change
/x86_64-alpine-linux-musl/lib/libdatadog_profiling.a 79.46 MB 79.46 MB 0% (0 B) 👌
/x86_64-alpine-linux-musl/lib/libdatadog_profiling.so 9.12 MB 9.12 MB 0% (0 B) 👌
x86_64-unknown-linux-gnu
Artifact Baseline Commit Change
/x86_64-unknown-linux-gnu/lib/libdatadog_profiling.a 95.11 MB 95.11 MB 0% (0 B) 👌
/x86_64-unknown-linux-gnu/lib/libdatadog_profiling.so 11.14 MB 11.14 MB 0% (0 B) 👌

MilanGarnier and others added 8 commits July 27, 2026 13:44
Compute th as round(2^56*(1-rate)) via 2^56 - round(2^56*rate) (exact,
avoids the single-shot IEEE-754 rounding the RFC warns against), and
return None from otel_consistent_sampling when a probability keep was
overturned by the trace rate limiter (rl_effective_rate set).
Use exact integer arithmetic over the canonical 6-decimal rate (the
form format_sampling_rate / dd= already use) so th reproduces the RFC
imprecision-appendix's decimal table (0.2 -> cccccccccccccd,
0.99 -> 028f5c28f5c28f), which an f64 pipeline cannot represent.
@MilanGarnier
MilanGarnier marked this pull request as ready for review July 28, 2026 14:57
@MilanGarnier
MilanGarnier requested a review from a team as a code owner July 28, 2026 14:57

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

ℹ️ 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 libdd-sampling/src/datadog_sampler.rs Outdated
Comment thread libdd-sampling/src/datadog_sampler.rs Outdated
@MilanGarnier
MilanGarnier changed the base branch from main to milan.garnier/otel-rate-limiter-effective-rate July 28, 2026 16:35
@MilanGarnier
MilanGarnier marked this pull request as draft July 30, 2026 16:02
Base automatically changed from milan.garnier/otel-rate-limiter-effective-rate to main July 30, 2026 17:22
@MilanGarnier
MilanGarnier marked this pull request as ready for review July 31, 2026 12:51

@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: 99ec5bae75

ℹ️ 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 libdd-sampling/src/datadog_sampler.rs
@MilanGarnier
MilanGarnier force-pushed the milan.garnier/otel-consistent-sampling branch from 99ec5ba to 984c9f7 Compare August 3, 2026 08:52
Comment thread libdd-sampling/src/datadog_sampler.rs
@MilanGarnier
MilanGarnier requested a review from bantonsson August 6, 2026 08:46

@bantonsson bantonsson 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.

The PR description is messy and confusing. It says:

th = round(2^56 * (1 - rate_micros / 1_000_000)) (56-bit), on the rate rounded to 6 decimals so it
matches the RFC appendix's imprecision table rather than a float
(1.0 - rate) * 2^56

But the code does exactly (1.0 - rate) * 2^56, or am I missing something?

Also, the test code in DataDog/system-tests#7372 does truncation, ie int(...) but this code does .round().

Please clean up the description and change the code.

fn derive_th(rate: f64) -> u64 {
const U56_MOD: u64 = 1u64 << 56;
const U56_MAX: u64 = U56_MOD - 1;
(((1.0 - rate) * U56_MOD as f64).round() as u64).clamp(0, U56_MAX)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Are you sure this is a good idea?
f64 have 53 bits of precision, and you are doing 56 bits arithmetic. So when you cast back to u64, if 1.0 - rate is bigger than 1/8th you will collapse the lower bits of your result together

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.

I think that the system tests also suffer from this issue. This is the code that the OTel spec for this points to as an example https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/main/pkg/sampling/probability.go

@MilanGarnier MilanGarnier Aug 6, 2026 •

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.

The spec we used as a basis (and also for system tests) is https://opentelemetry.io/docs/specs/otel/trace/tracestate-probability-sampling/
The code you linked also uses round and then the part about adding precision I'm not exactly sure to understand why we would need this.

About system-tests, although the comment says int(), the values are calculated with round() and the go tracer opted for round() as well https://github.com/DataDog/dd-trace-go/pull/5060/changes

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.

About doing 1.0 - rate, I understand it would be better to do the operations in 56 bit arithmetic (converting rate to a 56 bit int before), this is just a different implem from what we went for with the go tracer and system tests already @paullegranddc

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.

An alternative would be to do

fn derive_th(rate: f64) -> u64 {
    const U56_MOD: u64 = 1u64 << 56;
    const U56_MAX: u64 = U56_MOD - 1;
    U56_MAX - ((U56_MOD as f64 * rate) as u64).clamp(0, U56_MAX)
}

This would not pass the current system tests in their current form
WDYT ?

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.

The thing I'm most confused about is why the code and system tests don't do what the spec says:

OpenTelemetry SDKs are recommended to use 4 digits of precision by default.

Why do we compare the full hex representation and not only to 4 digits of precision?

Also, the comments in system tests must be very explicit (and correct) about how the values are computed and what precision is expected of the tracers.

@MilanGarnier MilanGarnier Aug 10, 2026 •

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.

Also, the comments in system tests must be very explicit (and correct) about how the values are computed and what precision is expected of the tracers.

I'm changing the comment in system-tests: DataDog/system-tests#7482
This is only one recommendation we are not following but it is technically respecting the spec.

If it's needed to make small gains on the tracestate size one day, it will still be possible to implement it but since it's already implemented (and merged) this way in dd-trace-go, I prefer consistency

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.

I'm changing the comment in system-tests: DataDog/system-tests#7482

Thanks

This is only one recommendation we are not following but it is technically respecting the spec.

Sure, it's optional.

@MilanGarnier
MilanGarnier force-pushed the milan.garnier/otel-consistent-sampling branch from 4a4cfad to 532f4fb Compare August 6, 2026 15:15
@gh-worker-dd-mergequeue-cf854d
gh-worker-dd-mergequeue-cf854d Bot merged commit ed5af0e into main Aug 11, 2026
152 checks passed
@gh-worker-dd-mergequeue-cf854d
gh-worker-dd-mergequeue-cf854d Bot deleted the milan.garnier/otel-consistent-sampling branch August 11, 2026 17:25
hoolioh added a commit that referenced this pull request Aug 17, 2026
# Release proposal for libdd-sampling and its dependencies

This PR contains version bumps based on public API changes and commits
since last release.

## libdd-common
**Next version:** `5.2.0`
**Semver bump:** `minor`
**Tag:** `libdd-common-v5.2.0`

### Commits

- feat(remote-config)!: agentless RC fetcher (#2112)
- feat(common): allocation-free tag validation and parsing (#2329)

## libdd-trace-utils
**Next version:** `10.1.0`
**Semver bump:** `minor`
**Tag:** `libdd-trace-utils-v10.1.0`

### Commits

- feat(data-pipeline): adds new attributes to OTLP trace metrics export
(#2316)
- feat: add v1 isolated bricks for trace exporter (#2280)

## libdd-sampling
**Next version:** `6.0.0`
**Semver bump:** `major`
**Tag:** `libdd-sampling-v6.0.0`

### ⚠️ major bump forced due to:

- `libdd-trace-utils`: ^9.0.0 → ^10.1.0

### Commits

- feat(sampling): OTel consistent-probability rv/th derivation
(APMAPI-2181) (#2276)
- fix(sampling): record rate limiter's effective rate on allow, not just
drop (#2288)
- refactor: migrate to workspace dependencies, phase 4 (#2296)
- chore: moving to workspace-level dependencies, phase 2 (#2270)
- refactor: consolidate core dependencies at workspace level (phase 1)
(#2253)

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: hoolioh <107922352+hoolioh@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants