Skip to content

Add _dd.p.ksr propagated tag for Knuth sampling rate#7741

Merged
bm1549 merged 2 commits intomasterfrom
brian.marks/add-ksr-tag
Mar 17, 2026
Merged

Add _dd.p.ksr propagated tag for Knuth sampling rate#7741
bm1549 merged 2 commits intomasterfrom
brian.marks/add-ksr-tag

Conversation

@bm1549
Copy link
Copy Markdown
Contributor

@bm1549 bm1549 commented Mar 11, 2026

What does this PR do?

Adds _dd.p.ksr (Knuth Sampling Rate) as a propagated tag set when agent-based or rule-based sampling decisions are made. The tag is stored in span meta (string type) with up to 6 significant digits and no trailing zeros.

Motivation

To enable consistent sampling across tracers and backend retention filters, the backend needs to know the sampling rate applied by the tracer. Without transmitting the tracer's rate via _dd.p.ksr, backend resampling cannot correctly compute effective rates in multi-stage sampling scenarios.

See RFC: "Transmit Knuth sampling rate to backend"

Additional Notes

Key files changed:

  • packages/dd-trace/src/constants.js — Added SAMPLING_KNUTH_RATE constant
  • packages/dd-trace/src/priority_sampler.js — Added formatKnuthRate() and set ksr in agent/rule sampling paths
  • packages/dd-trace/test/priority_sampler.spec.js — 7 test cases

Related PRs across tracers:

🤖 Generated with Claude Code

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Mar 11, 2026

Overall package size

Self size: 4.95 MB
Deduped: 5.79 MB
No deduping: 5.79 MB

Dependency sizes | name | version | self size | total size | |------|---------|-----------|------------| | import-in-the-middle | 3.0.0 | 81.15 kB | 815.98 kB | | dc-polyfill | 0.1.10 | 26.73 kB | 26.73 kB |

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

@pr-commenter
Copy link
Copy Markdown

pr-commenter bot commented Mar 11, 2026

Benchmarks

Benchmark execution time: 2026-03-11 02:53:56

Comparing candidate commit 8a10db8 in PR branch brian.marks/add-ksr-tag with baseline commit e97f095 in branch master.

Found 0 performance improvements and 0 performance regressions! Performance is the same for 230 metrics, 30 unstable metrics.

@codecov
Copy link
Copy Markdown

codecov bot commented Mar 11, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 80.40%. Comparing base (e97f095) to head (8a10db8).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #7741   +/-   ##
=======================================
  Coverage   80.40%   80.40%           
=======================================
  Files         741      741           
  Lines       32084    32090    +6     
=======================================
+ Hits        25796    25802    +6     
  Misses       6288     6288           
Flag Coverage Δ
aiguard-macos 38.90% <87.50%> (-0.08%) ⬇️
aiguard-ubuntu 39.02% <87.50%> (-0.08%) ⬇️
aiguard-windows 38.75% <87.50%> (-0.08%) ⬇️
apm-capabilities-tracing-macos 48.86% <100.00%> (+<0.01%) ⬆️
apm-capabilities-tracing-ubuntu 48.95% <100.00%> (+0.01%) ⬆️
apm-capabilities-tracing-windows 48.66% <100.00%> (-0.01%) ⬇️
apm-integrations-child-process 38.46% <87.50%> (-0.08%) ⬇️
apm-integrations-couchbase-18 37.24% <87.50%> (-0.21%) ⬇️
apm-integrations-couchbase-eol 37.85% <87.50%> (-0.07%) ⬇️
apm-integrations-oracledb 37.69% <87.50%> (-0.07%) ⬇️
appsec-express 55.15% <87.50%> (-0.06%) ⬇️
appsec-fastify 51.51% <87.50%> (-0.05%) ⬇️
appsec-graphql 51.70% <87.50%> (-0.05%) ⬇️
appsec-kafka 44.23% <87.50%> (-0.06%) ⬇️
appsec-ldapjs 43.91% <75.00%> (-0.07%) ⬇️
appsec-lodash 43.54% <0.00%> (-0.10%) ⬇️
appsec-macos 58.19% <87.50%> (-0.05%) ⬇️
appsec-mongodb-core 48.70% <87.50%> (-0.06%) ⬇️
appsec-mongoose 49.37% <87.50%> (-0.06%) ⬇️
appsec-mysql 50.74% <75.00%> (-0.06%) ⬇️
appsec-node-serialize 43.09% <75.00%> (-0.07%) ⬇️
appsec-passport 47.50% <87.50%> (-0.06%) ⬇️
appsec-postgres 50.50% <75.00%> (-0.06%) ⬇️
appsec-sourcing 42.51% <87.50%> (-0.06%) ⬇️
appsec-template 43.25% <75.00%> (-0.07%) ⬇️
appsec-ubuntu 58.27% <87.50%> (-0.05%) ⬇️
appsec-windows 58.03% <87.50%> (-0.05%) ⬇️
instrumentations-instrumentation-bluebird 32.28% <0.00%> (-0.12%) ⬇️
instrumentations-instrumentation-body-parser 40.40% <87.50%> (-0.07%) ⬇️
instrumentations-instrumentation-child_process 37.78% <87.50%> (-0.08%) ⬇️
instrumentations-instrumentation-cookie-parser 34.26% <0.00%> (-0.11%) ⬇️
instrumentations-instrumentation-express 34.58% <0.00%> (-0.11%) ⬇️
instrumentations-instrumentation-express-mongo-sanitize 34.39% <0.00%> (-0.11%) ⬇️
instrumentations-instrumentation-express-session 40.03% <87.50%> (-0.07%) ⬇️
instrumentations-instrumentation-fs 31.90% <0.00%> (-0.12%) ⬇️
instrumentations-instrumentation-generic-pool 29.91% <ø> (ø)
instrumentations-instrumentation-http 39.75% <87.50%> (-0.07%) ⬇️
instrumentations-instrumentation-knex 32.29% <0.00%> (-0.12%) ⬇️
instrumentations-instrumentation-mongoose 33.41% <0.00%> (-0.11%) ⬇️
instrumentations-instrumentation-multer 40.15% <87.50%> (-0.07%) ⬇️
instrumentations-instrumentation-mysql2 38.24% <87.50%> (-0.07%) ⬇️
instrumentations-instrumentation-passport 43.89% <75.00%> (-0.08%) ⬇️
instrumentations-instrumentation-passport-http 43.57% <87.50%> (-0.07%) ⬇️
instrumentations-instrumentation-passport-local 44.11% <87.50%> (-0.07%) ⬇️
instrumentations-instrumentation-pg 37.67% <87.50%> (-0.08%) ⬇️
instrumentations-instrumentation-promise 32.21% <0.00%> (-0.12%) ⬇️
instrumentations-instrumentation-promise-js 32.22% <0.00%> (-0.12%) ⬇️
instrumentations-instrumentation-q 32.26% <0.00%> (-0.12%) ⬇️
instrumentations-instrumentation-url 32.18% <0.00%> (-0.12%) ⬇️
instrumentations-instrumentation-when 32.23% <0.00%> (-0.12%) ⬇️
llmobs-ai 42.22% <87.50%> (-0.07%) ⬇️
llmobs-anthropic 40.21% <87.50%> (-0.07%) ⬇️
llmobs-bedrock 39.19% <87.50%> (-0.06%) ⬇️
llmobs-google-genai 39.75% <87.50%> (-0.07%) ⬇️
llmobs-langchain 39.98% <87.50%> (-0.06%) ⬇️
llmobs-openai 43.93% <87.50%> (-0.07%) ⬇️
llmobs-vertex-ai 40.00% <87.50%> (-0.07%) ⬇️
platform-core 31.53% <ø> (ø)
platform-esbuild 34.48% <ø> (ø)
platform-instrumentations-misc 48.40% <ø> (ø)
platform-shimmer 37.63% <ø> (ø)
platform-unit-guardrails 32.95% <ø> (ø)
plugins-azure-event-hubs 25.83% <ø> (ø)
plugins-azure-service-bus 25.19% <ø> (ø)
plugins-bullmq 44.20% <87.50%> (-0.07%) ⬇️
plugins-cassandra 37.72% <87.50%> (-0.07%) ⬇️
plugins-cookie 26.89% <ø> (ø)
plugins-cookie-parser 26.67% <ø> (ø)
plugins-crypto 26.79% <ø> (ø)
plugins-dd-trace-api 38.26% <50.00%> (-0.11%) ⬇️
plugins-express-mongo-sanitize 26.82% <ø> (ø)
plugins-express-session 26.63% <ø> (ø)
plugins-fastify 42.11% <87.50%> (-0.07%) ⬇️
plugins-fetch 38.29% <87.50%> (-0.07%) ⬇️
plugins-fs 38.56% <87.50%> (-0.08%) ⬇️
plugins-generic-pool 25.87% <ø> (ø)
plugins-google-cloud-pubsub 45.32% <87.50%> (-0.06%) ⬇️
plugins-grpc 40.84% <87.50%> (-0.07%) ⬇️
plugins-handlebars 26.86% <ø> (ø)
plugins-hapi 40.03% <87.50%> (-0.07%) ⬇️
plugins-hono 40.29% <87.50%> (-0.07%) ⬇️
plugins-ioredis 38.37% <87.50%> (-0.08%) ⬇️
plugins-knex 26.50% <ø> (ø)
plugins-ldapjs 24.36% <ø> (ø)
plugins-light-my-request 26.23% <ø> (ø)
plugins-limitd-client 32.56% <0.00%> (-0.12%) ⬇️
plugins-lodash 25.96% <ø> (ø)
plugins-mariadb 39.42% <87.50%> (-0.08%) ⬇️
plugins-memcached 38.10% <87.50%> (-0.08%) ⬇️
plugins-microgateway-core 39.09% <87.50%> (-0.15%) ⬇️
plugins-moleculer 40.44% <87.50%> (-0.06%) ⬇️
plugins-mongodb 39.12% <100.00%> (-0.06%) ⬇️
plugins-mongodb-core 38.96% <100.00%> (-0.07%) ⬇️
plugins-mongoose 38.80% <87.50%> (-0.07%) ⬇️
plugins-multer 26.63% <ø> (ø)
plugins-mysql 39.12% <100.00%> (-0.07%) ⬇️
plugins-mysql2 39.22% <100.00%> (-0.07%) ⬇️
plugins-node-serialize 26.93% <ø> (ø)
plugins-opensearch 37.56% <87.50%> (-0.07%) ⬇️
plugins-passport-http 26.68% <ø> (ø)
plugins-postgres 35.69% <100.00%> (-0.06%) ⬇️
plugins-process 26.79% <ø> (ø)
plugins-pug 26.89% <ø> (ø)
plugins-redis 38.69% <87.50%> (-0.08%) ⬇️
plugins-router 42.84% <87.50%> (-0.06%) ⬇️
plugins-sequelize 25.47% <ø> (ø)
plugins-test-and-upstream-amqp10 38.45% <87.50%> (-0.08%) ⬇️
plugins-test-and-upstream-amqplib 43.79% <87.50%> (-0.07%) ⬇️
plugins-test-and-upstream-apollo 38.95% <87.50%> (-0.07%) ⬇️
plugins-test-and-upstream-avsc 38.59% <50.00%> (-0.11%) ⬇️
plugins-test-and-upstream-bunyan 33.82% <0.00%> (-0.12%) ⬇️
plugins-test-and-upstream-connect 40.68% <87.50%> (-0.07%) ⬇️
plugins-test-and-upstream-graphql 40.06% <87.50%> (-0.07%) ⬇️
plugins-test-and-upstream-koa 40.27% <87.50%> (-0.07%) ⬇️
plugins-test-and-upstream-protobufjs 38.82% <50.00%> (-0.11%) ⬇️
plugins-test-and-upstream-rhea 43.95% <87.50%> (-0.07%) ⬇️
plugins-undici 39.06% <87.50%> (-0.04%) ⬇️
plugins-url 26.79% <ø> (ø)
plugins-valkey 38.04% <87.50%> (-0.08%) ⬇️
plugins-vm 26.79% <ø> (ø)
plugins-winston 34.01% <0.00%> (-0.12%) ⬇️
plugins-ws 41.79% <87.50%> (-0.07%) ⬇️
profiling-macos 39.84% <50.00%> (-0.10%) ⬇️
profiling-ubuntu 39.97% <50.00%> (-0.10%) ⬇️
profiling-windows 41.15% <50.00%> (-0.10%) ⬇️
serverless-azure-functions-client 25.54% <ø> (ø)
serverless-azure-functions-eventhubs 25.54% <ø> (ø)
serverless-azure-functions-servicebus 25.54% <ø> (ø)

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

☔ View full report in Codecov by Sentry.
📢 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.

Only set the SAMPLING_KNUTH_RATE (_dd.p.ksr) tag when actual agent
rates have been received (SAMPLING_MECHANISM_AGENT), not when using
the built-in default sampler (SAMPLING_MECHANISM_DEFAULT).

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@bm1549 bm1549 marked this pull request as ready for review March 17, 2026 01:30
@bm1549 bm1549 requested a review from a team as a code owner March 17, 2026 01:30
@bm1549 bm1549 requested review from khanayan123 and removed request for a team March 17, 2026 01:30
Copy link
Copy Markdown
Collaborator

@khanayan123 khanayan123 left a comment

Choose a reason for hiding this comment

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

LGTM

@bm1549 bm1549 merged commit a0f249a into master Mar 17, 2026
844 of 845 checks passed
@bm1549 bm1549 deleted the brian.marks/add-ksr-tag branch March 17, 2026 14:06
dd-octo-sts bot pushed a commit that referenced this pull request Mar 17, 2026
* Add _dd.p.ksr propagated tag for Knuth sampling rate

Co-Authored-By: Claude Opus 4.6 <[email protected]>

* Do not set _dd.p.ksr when default sampling mechanism is used

Only set the SAMPLING_KNUTH_RATE (_dd.p.ksr) tag when actual agent
rates have been received (SAMPLING_MECHANISM_AGENT), not when using
the built-in default sampler (SAMPLING_MECHANISM_DEFAULT).

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>

---------

Co-authored-by: Claude Opus 4.6 <[email protected]>
@dd-octo-sts dd-octo-sts bot mentioned this pull request Mar 17, 2026
juan-fernandez pushed a commit that referenced this pull request Mar 18, 2026
* Add _dd.p.ksr propagated tag for Knuth sampling rate

Co-Authored-By: Claude Opus 4.6 <[email protected]>

* Do not set _dd.p.ksr when default sampling mechanism is used

Only set the SAMPLING_KNUTH_RATE (_dd.p.ksr) tag when actual agent
rates have been received (SAMPLING_MECHANISM_AGENT), not when using
the built-in default sampler (SAMPLING_MECHANISM_DEFAULT).

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>

---------

Co-authored-by: Claude Opus 4.6 <[email protected]>
bm1549 added a commit to DataDog/dd-trace-rs that referenced this pull request Mar 19, 2026
# What does this PR do?

Adds `_dd.p.ksr` (Knuth Sampling Rate) as a propagated tag set when
agent-based or rule-based sampling decisions are made. The tag is stored
in span `meta` (string type) with up to 6 significant digits and no
trailing zeros.

`format_sampling_rate` now returns `Option<String>` and guards against
invalid inputs (negative, >1.0, NaN, infinity), returning `None` instead
of producing garbage output.

# Motivation

To enable consistent sampling across tracers and backend retention
filters, the backend needs to know the sampling rate applied by the
tracer. Without transmitting the tracer's rate via `_dd.p.ksr`, backend
resampling cannot correctly compute effective rates in multi-stage
sampling scenarios.

See RFC: "Transmit Knuth sampling rate to backend"

# Additional Notes

Key files changed:
- `datadog-opentelemetry/src/core/constants.rs` — Added
`SAMPLING_KNUTH_RATE_TAG_KEY` constant
- `datadog-opentelemetry/src/sampling/datadog_sampler.rs` — Added
`format_sampling_rate()` helper (returns `Option<String>`, defensive
against invalid rates) and set ksr in agent/rule sampling paths
- Updated 2 snapshot JSON files

Related PRs across tracers:
- Java: DataDog/dd-trace-java#10802
- .NET: DataDog/dd-trace-dotnet#8287
- Ruby: DataDog/dd-trace-rb#5436
- Node.js: DataDog/dd-trace-js#7741
- PHP: DataDog/dd-trace-php#3701
- C++: DataDog/dd-trace-cpp#288
- System tests: DataDog/system-tests#6466

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 4.6 <[email protected]>
bm1549 added a commit to DataDog/dd-trace-go that referenced this pull request Mar 19, 2026
### What does this PR do?

Fixes `_dd.p.ksr` (Knuth Sampling Rate) to only be set on spans when the
agent has provided sampling rates via `readRatesJSON()`. Previously, ksr
was unconditionally set in `prioritySampler.apply()`, including when the
rate was the initial client-side default (1.0) before any agent response
arrived.

Also refactors `prioritySampler` to consolidate lock acquisitions:
extracts `getRateLocked()` so `apply()` acquires `ps.mu.RLock` only once
to read both the rate and `agentRatesLoaded`.

### Motivation

Cross-language consistency: Python, Java, PHP, and other tracers only
set ksr when actual agent rates or sampling rules are applied, not for
the default fallback. This aligns Go with that behavior.

See RFC: "Transmit Knuth sampling rate to backend"

### Additional Notes

- Added `agentRatesLoaded` bool field to `prioritySampler`, set to
`true` in `readRatesJSON()`
- `apply()` now gates ksr behind `agentRatesLoaded` check
- Extracted `getRateLocked()` to avoid double lock acquisition in
`apply()`
- Rule-based sampling path (`applyTraceRuleSampling` in span.go)
unchanged — correctly always sets ksr
- Tests added: `ksr-not-set-without-agent-rates` and
`ksr-set-after-agent-rates-received`

Related PRs across tracers:
- Java: DataDog/dd-trace-java#10802
- .NET: DataDog/dd-trace-dotnet#8287
- Ruby: DataDog/dd-trace-rb#5436
- Node.js: DataDog/dd-trace-js#7741
- PHP: DataDog/dd-trace-php#3701
- Rust: DataDog/dd-trace-rs#180
- C++: DataDog/dd-trace-cpp#288
- System tests: DataDog/system-tests#6466

### Reviewer's Checklist

- [x] Changed code has unit tests for its functionality at or near 100%
coverage.
- [x] [System-Tests](https://github.com/DataDog/system-tests/) covering
this feature have been added and enabled with the va.b.c-dev version
tag.
- [ ] There is a benchmark for any new code, or changes to existing
code.
- [x] If this interacts with the agent in a new way, a system test has
been added.
- [x] New code is free of linting errors. You can check this by running
`make lint` locally.
- [x] New code doesn't break existing tests. You can check this by
running `make test` locally.
- [ ] Add an appropriate team label so this PR gets put in the right
place for the release notes.
- [ ] All generated files are up to date. You can check this by running
`make generate` locally.
- [ ] Non-trivial go.mod changes, e.g. adding new modules, are reviewed
by @DataDog/dd-trace-go-guild. Make sure all nested modules are up to
date by running `make fix-modules` locally.

Unsure? Have a question? Request a review!

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 4.6 (1M context) <[email protected]>
Co-authored-by: Dario Castañé <[email protected]>
Co-authored-by: Mikayla Toffler <[email protected]>
bm1549 added a commit to DataDog/dd-trace-dotnet that referenced this pull request Mar 25, 2026
## Summary of changes

Add `_dd.p.ksr` (Knuth Sampling Rate) propagated tag to spans when
sampling is applied via agent rates or trace sampling rules, per the
[Transmit Knuth Sampling Rate to Backend
RFC](https://docs.google.com/document/d/1Po3qtJb6PGheFeKFSUMv2pVY_y-HFAxTzNLuacCbCXY/edit).

## Reason for change

The backend needs to know the exact sampling rate applied by the tracer
to correctly compute effective rates during resampling (e.g., tracer 0.5
× backend 0.5 = effective 0.25). This tag enables that by propagating
the rate via `x-datadog-tags` and W3C `tracestate`.

## Implementation details

- Set `_dd.p.ksr` in `TraceContext.SetSamplingPriority()` for
`AgentRate`, `LocalTraceSamplingRule`, `RemoteAdaptiveSamplingRule`, and
`RemoteUserSamplingRule` mechanisms
- Use `TryAddTag` to preserve the original rate (consistent with
`AppliedSamplingRate ??= rate` semantics)
- Format with `"0.######"` (up to 6 decimal digits, no trailing zeros,
no scientific notation) per RFC spec
- Added `.IsOptional("_dd.p.ksr")` to `SpanTagAssertion.cs` so
integration test tag validators accept the new tag

## Test coverage

- Unit tests in `TraceContextTests_KnuthSamplingRate.cs`:
  - KSR set for agent rate sampling
- KSR set for trace sampling rules (local, remote adaptive, remote user)
- KSR NOT set for manual, AppSec, rate limiter, or single span
mechanisms
  - KSR preserved on subsequent sampling calls (TryAddTag semantics)
- Formatting with up to 6 decimal digits (boundary values including
small rates like 0.00001)
- System tests in [system-tests
#6466](DataDog/system-tests#6466)

## Other details

Related PRs across tracers:
- Java: DataDog/dd-trace-java#10802
- Ruby: DataDog/dd-trace-rb#5436
- Node.js: DataDog/dd-trace-js#7741
- PHP: DataDog/dd-trace-php#3701
- Rust: DataDog/dd-trace-rs#180
- C++: DataDog/dd-trace-cpp#288

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 4.6 <[email protected]>
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-patch

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants