Skip to content

fix: _dd.p.ksr formatting to use 6 significant digits without trailing zeros#288

Merged
bm1549 merged 7 commits intomainfrom
brian.marks/fix-ksr-formatting
Mar 12, 2026
Merged

fix: _dd.p.ksr formatting to use 6 significant digits without trailing zeros#288
bm1549 merged 7 commits intomainfrom
brian.marks/fix-ksr-formatting

Conversation

@bm1549
Copy link
Contributor

@bm1549 bm1549 commented Mar 11, 2026

Description

Fix _dd.p.ksr formatting to use 6 significant digits without trailing zeros, using std::to_chars with std::chars_format::general. Also skip setting _dd.p.ksr when the sampling mechanism is DEFAULT (no agent configuration received yet).

Changes:

  • Replace snprintf with std::to_chars for _dd.p.ksr formatting
  • Use std::chars_format::general with 6 significant digits (matches Go tracer behavior)
  • Skip setting _dd.p.ksr when SamplingMechanism::DEFAULT is active
  • Add error handling for std::to_chars conversion failures
  • Update tests to expect the new formatting

Motivation

The previous snprintf approach with %g format produced inconsistent output compared to other tracers. std::to_chars with std::chars_format::general gives us precise control over significant digits while automatically removing trailing zeros.

Additionally, setting _dd.p.ksr with the DEFAULT mechanism is meaningless since no sampling configuration has been received from the agent yet.

Additional Notes

Jira ticket: [PROJ-IDENT]

🤖 Generated with Claude Code

… zeros

Replace std::to_string() (which uses sprintf "%f" producing 6 trailing
decimal places) with snprintf "%.6g" which produces up to 6 significant
digits with no trailing zeros. This matches the behavior of Python's
f"{rate:.6g}" and Go's strconv.FormatFloat(rate, 'g', 6, 64).

Examples: 1.0 -> "1", 0.5 -> "0.5", 0.0 -> "0"

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@pr-commenter
Copy link

pr-commenter bot commented Mar 11, 2026

Benchmarks

Benchmark execution time: 2026-03-12 15:33:47

Comparing candidate commit d9275b8 in PR branch brian.marks/fix-ksr-formatting with baseline commit f8c3913 in branch main.

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

scenario:BM_TraceTinyCCSource

  • 🟩 execution_time [-2.357ms; -2.111ms] or [-3.017%; -2.701%]

Reformat the emplace_back call to match clang-format's expected style
(arguments on one line with alignment) to fix the verify CI job.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@bm1549 bm1549 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 Mar 11, 2026
@datadog-datadog-prod-us1
Copy link

datadog-datadog-prod-us1 bot commented Mar 11, 2026

🎯 Code Coverage (details)
Patch Coverage: 58.33%
Overall Coverage: 87.70% (-0.07%)

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: d9275b8 | Docs | Datadog PR Page | Was this helpful? React with 👍/👎 or give us feedback!

@codecov-commenter
Copy link

codecov-commenter commented Mar 11, 2026

Codecov Report

❌ Patch coverage is 58.33333% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 87.70%. Comparing base (f8c3913) to head (d9275b8).

Files with missing lines Patch % Lines
src/datadog/trace_segment.cpp 58.33% 5 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #288      +/-   ##
==========================================
- Coverage   87.76%   87.70%   -0.07%     
==========================================
  Files          84       84              
  Lines        5658     5668      +10     
==========================================
+ Hits         4966     4971       +5     
- Misses        692      697       +5     

☔ 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.

The ksr trace tag should only be set when the sampling decision comes
from an explicit source (agent rate, rule, or remote rule). When the
DEFAULT mechanism is used — meaning no agent configuration has been
received yet — the rate is a hardcoded 100% and ksr is meaningless.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Copy link
Collaborator

@dmehala dmehala left a comment

Choose a reason for hiding this comment

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

Well done.

EDIT: I'm trying to enforce semantic commits, please prefix your commit message with fix: during the merge. Thanks.

@dmehala dmehala changed the title Fix _dd.p.ksr formatting to use 6 significant digits without trailing zeros fix: _dd.p.ksr formatting to use 6 significant digits without trailing zeros Mar 12, 2026
Co-authored-by: Damien Mehala <damien.mehala@datadoghq.com>
bm1549 and others added 2 commits March 12, 2026 11:27
Fix two issues introduced by GitHub suggestion commits:
1. Correct the error condition: `ec == std::errc()` means success, not
   failure — changed to `ec != std::errc()` to properly detect errors.
2. Fix indentation from 4-space to 2-space to match project style and
   pass clang-format checks.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@bm1549 bm1549 merged commit 1572290 into main Mar 12, 2026
39 checks passed
@bm1549 bm1549 deleted the brian.marks/fix-ksr-formatting branch March 12, 2026 15:50
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants