fix(tracer): only set _dd.p.ksr after agent rates are received#4523
fix(tracer): only set _dd.p.ksr after agent rates are received#4523
Conversation
…ceived The priority sampler was unconditionally setting the _dd.p.ksr tag on every span, even when using the initial client-side default rate (1.0) before any agent response was received. This is inconsistent with other Datadog tracers, which only propagate ksr when actual agent rates or sampling rules are applied. Add an agentRatesLoaded flag to prioritySampler that is set to true the first time readRatesJSON processes an agent response. The apply method now only sets keyKnuthSamplingRate when this flag is true. Rule-based sampling (applyTraceRuleSampling) is unaffected and continues to set ksr unconditionally, which is the correct behavior. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files
🚀 New features to boost your workflow:
|
|
✅ Tests 🎉 All green!❄️ No new flaky tests detected 🎯 Code Coverage (details) 🔗 Commit SHA: ef998e2 | Docs | Datadog PR Page | Was this helpful? React with 👍/👎 or give us feedback! |
BenchmarksBenchmark execution time: 2026-03-13 18:44:11 Comparing candidate commit ef998e2 in PR branch Found 9 performance improvements and 0 performance regressions! Performance is the same for 139 metrics, 7 unstable metrics.
|
|
Can we resolve the conflicts with main? There's been some changes to the sampling rate functions since you last pulled. |
Resolve conflicts in ddtrace/tracer/sampler.go: keep both agentRatesLoaded field (from this branch) and lastCapped field with rate capping logic (from main). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
What Does This Do
Fixes
_dd.p.ksr(Knuth Sampling Rate) to only be set on spans when the agent has provided sampling rates viareadRatesJSON(). Previously, ksr was unconditionally set inprioritySampler.apply(), including when the rate was the initial client-side default (1.0) before any agent response arrived.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
agentRatesLoadedbool field toprioritySampler, set totrueinreadRatesJSON()apply()now gates ksr behindagentRatesLoadedcheckapplyTraceRuleSamplingin span.go) unchanged — correctly always sets ksrksr-not-set-without-agent-ratesandksr-set-after-agent-rates-receivedRelated PRs across tracers:
_dd.p.ksrformatting to use 6 significant digits without trailing zeros dd-trace-cpp#288🤖 Generated with Claude Code