Fix _dd.p.ksr scientific notation for very small sampling rates#5497
Fix _dd.p.ksr scientific notation for very small sampling rates#5497
Conversation
Very small sampling rates (e.g. 0.000001) were formatted using Ruby's %.6g format which outputs scientific notation like "1e-06". This changes to explicit rounding at the integer level and %.6f formatting to always produce decimal notation with up to 6 decimal digits, trailing zeros stripped (e.g. "0.000001"). Fixes APMAPI-1869 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
|
✅ Tests 🎉 All green!❄️ No new flaky tests detected 🎯 Code Coverage (details) 🔗 Commit SHA: 515477f | Docs | Datadog PR Page | Was this helpful? React with 👍/👎 or give us feedback! |
BenchmarksBenchmark execution time: 2026-03-24 01:15:37 Comparing candidate commit 515477f in PR branch Found 0 performance improvements and 0 performance regressions! Performance is the same for 45 metrics, 1 unstable metrics.
|
…tific notation (#17086) ## Description Fix `_dd.p.ksr` span tag formatting for very small sampling rates. Previously, rates below 0.001 were formatted using Python's `:.6g` format which outputs scientific notation (e.g. `0.000001` → `"1e-06"`). This changes to explicit integer-level rounding and `:.6f` formatting to always produce decimal notation with up to 6 decimal digits, trailing zeros stripped. **Related PRs:** - dd-trace-rb: DataDog/dd-trace-rb#5497 - dd-trace-js: DataDog/dd-trace-js#7846 - system-tests: DataDog/system-tests#6466 Fixes APMAPI-1869 ## Testing - Added parametrized unit tests in `tests/tracer/test_sampler.py::test_ksr_formatting` covering: - Rate 1.0 → `"1"` (trailing zeros stripped) - Rate 0.000001 → `"0.000001"` (6 decimal precision boundary) - Rate 0.0000001 → `"0"` (below precision, rounds to zero) - Rate 0.0000005 → `"0.000001"` (rounds up to one millionth) - Rate 0.5 → `"0.5"` (simple case) - Rate 0.7654321 → `"0.765432"` (truncation at 6 decimal places) - These match the system test cases in DataDog/system-tests#6466 ## Risks None — the formatting change only affects `_dd.p.ksr` string values for very small rates. Values ≥ 0.001 produce identical output to the previous `:.6g` format. ## Additional Notes Uses `math.floor(rate * 1e6 + 0.5) / 1e6` instead of `round(rate * 1e6) / 1e6` to avoid Python's banker's rounding which would round `0.0000005` down to `0` instead of up to `0.000001`. Co-authored-by: brian.marks <brian.marks@datadoghq.com>
What does this PR do?
Fix
_dd.p.ksrspan tag formatting for very small sampling rates. Rates below 0.001 were formatted using Ruby's%.6gformat which outputs scientific notation (e.g.0.000001→"1e-06"). Changed to explicit integer-level rounding and%.6fformatting to always produce decimal notation with up to 6 decimal digits, trailing zeros stripped.Motivation:
Fixes APMAPI-1869. System tests expect decimal notation for
_dd.p.ksrvalues (see DataDog/system-tests#6466).Related PRs:
Change log entry
Yes. Fix
_dd.p.ksrspan tag formatting for very small sampling rates to use decimal notation instead of scientific notation.Additional Notes:
Uses
(rate * 1e6).round / 1e6.to_ffor consistent rounding at the integer level, avoiding IEEE 754 precision issues with%.6falone.How to test the change?
Updated unit tests in
spec/datadog/tracing/transport/trace_formatter_spec.rbcovering:"0.000001"(was"1e-06")"0"(rounds to zero, new case)"0.000001"(rounds up, new case)