Skip to content

Commit 092e49e

Browse files
bm1549claude
andcommitted
Fix _dd.p.ksr formatting to use 6 significant digits without trailing 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>
1 parent f8c3913 commit 092e49e

3 files changed

Lines changed: 26 additions & 13 deletions

File tree

src/datadog/trace_segment.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
#include <cassert>
1515
#include <charconv>
16+
#include <cstdio>
1617
#include <string>
1718
#include <unordered_map>
1819
#include <utility>
@@ -34,6 +35,12 @@ namespace datadog {
3435
namespace tracing {
3536
namespace {
3637

38+
std::string format_rate(double rate) {
39+
char buf[32];
40+
std::snprintf(buf, sizeof(buf), "%.6g", rate);
41+
return std::string(buf);
42+
}
43+
3744
struct Cache {
3845
static int process_id;
3946

@@ -323,7 +330,7 @@ void TraceSegment::make_sampling_decision_if_null() {
323330

324331
trace_tags_.emplace_back(
325332
tags::internal::ksr,
326-
std::to_string(*sampling_decision_->configured_rate));
333+
format_rate(*sampling_decision_->configured_rate));
327334
}
328335

329336
void TraceSegment::update_decision_maker_trace_tag() {

test/test_span.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -784,8 +784,8 @@ TEST_SPAN("injecting W3C tracestate header") {
784784
{"x-datadog-parent-id", "1"},
785785
{"x-datadog-origin", "France"},
786786
},
787-
// The "s:-1" and "t.ksr:0.000000" comes from the 0% sample rate.
788-
"dd=s:-1;p:$parent_id;o:France;t.ksr:0.000000"},
787+
// The "s:-1" and "t.ksr:0" comes from the 0% sample rate.
788+
"dd=s:-1;p:$parent_id;o:France;t.ksr:0"},
789789

790790
{__LINE__,
791791
"trace tags",
@@ -794,8 +794,8 @@ TEST_SPAN("injecting W3C tracestate header") {
794794
{"x-datadog-parent-id", "1"},
795795
{"x-datadog-tags", "_dd.p.foo=x,_dd.p.bar=y,ignored=wrong_prefix"},
796796
},
797-
// The "s:-1" and "t.ksr:0.000000" comes from the 0% sample rate.
798-
"dd=s:-1;p:$parent_id;t.foo:x;t.bar:y;t.ksr:0.000000"},
797+
// The "s:-1" and "t.ksr:0" comes from the 0% sample rate.
798+
"dd=s:-1;p:$parent_id;t.foo:x;t.bar:y;t.ksr:0"},
799799

800800
{__LINE__,
801801
"extra fields",
@@ -825,7 +825,7 @@ TEST_SPAN("injecting W3C tracestate header") {
825825
},
826826
// The "s:-1" comes from the 0% sample rate.
827827
"dd=s:-1;p:$parent_id;o:France_ is a country~nation_ so is "
828-
"______.;t.ksr:0.000000",
828+
"______.;t.ksr:0",
829829
},
830830

831831
{__LINE__,
@@ -836,7 +836,7 @@ TEST_SPAN("injecting W3C tracestate header") {
836836
{"x-datadog-tags", "_dd.p.a;d台北x =foo,_dd.p.ok=bar"},
837837
},
838838
// The "s:-1" comes from the 0% sample rate.
839-
"dd=s:-1;p:$parent_id;t.a_d______x_:foo;t.ok:bar;t.ksr:0.000000"},
839+
"dd=s:-1;p:$parent_id;t.a_d______x_:foo;t.ok:bar;t.ksr:0"},
840840

841841
{__LINE__,
842842
"replace invalid characters in trace tag value",
@@ -847,7 +847,7 @@ TEST_SPAN("injecting W3C tracestate header") {
847847
},
848848
// The "s:-1" comes from the 0% sample rate.
849849
"dd=s:-1;p:$parent_id;t.wacky:hello fr_d_ how are "
850-
"_________?;t.ksr:0.000000"},
850+
"_________?;t.ksr:0"},
851851

852852
{__LINE__,
853853
"replace equal signs with tildes in trace tag value",
@@ -857,7 +857,7 @@ TEST_SPAN("injecting W3C tracestate header") {
857857
{"x-datadog-tags", "_dd.p.base64_thingy=d2Fra2EhIHdhaw=="},
858858
},
859859
// The "s:-1" comes from the 0% sample rate.
860-
"dd=s:-1;p:$parent_id;t.base64_thingy:d2Fra2EhIHdhaw~~;t.ksr:0.000000"},
860+
"dd=s:-1;p:$parent_id;t.base64_thingy:d2Fra2EhIHdhaw~~;t.ksr:0"},
861861

862862
{__LINE__,
863863
"oversized origin truncates it and subsequent fields",

test/test_trace_segment.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
#include <datadog/tracer.h>
88
#include <datadog/tracer_config.h>
99

10+
#include <cstdio>
1011
#include <regex>
12+
#include <string>
1113
#include <vector>
1214

1315
#include "matchers.h"
@@ -325,7 +327,7 @@ TEST_CASE("TraceSegment finalization of spans") {
325327
REQUIRE(collector->span_count() == 1);
326328
const auto& span = collector->first_span();
327329
REQUIRE(span.numeric_tags.at(tags::internal::agent_sample_rate) == 1.0);
328-
REQUIRE(span.tags.at(tags::internal::ksr) == "1.000000");
330+
REQUIRE(span.tags.at(tags::internal::ksr) == "1");
329331
}
330332

331333
SECTION(
@@ -348,7 +350,7 @@ TEST_CASE("TraceSegment finalization of spans") {
348350
{
349351
REQUIRE(collector_response->span_count() == 1);
350352
const auto& span = collector_response->first_span();
351-
CHECK(span.tags.at(tags::internal::ksr) == "1.000000");
353+
CHECK(span.tags.at(tags::internal::ksr) == "1");
352354
}
353355

354356
collector_response->chunks.clear();
@@ -361,7 +363,7 @@ TEST_CASE("TraceSegment finalization of spans") {
361363
REQUIRE(collector_response->span_count() == 1);
362364
const auto& span = collector_response->first_span();
363365
CHECK(span.numeric_tags.at(tags::internal::agent_sample_rate) == 1.0);
364-
CHECK(span.tags.at(tags::internal::ksr) == "1.000000");
366+
CHECK(span.tags.at(tags::internal::ksr) == "1");
365367
}
366368
}
367369

@@ -392,7 +394,11 @@ TEST_CASE("TraceSegment finalization of spans") {
392394
const auto& span = collector->first_span();
393395
REQUIRE(span.numeric_tags.at(tags::internal::rule_sample_rate) ==
394396
sample_rate);
395-
CHECK(span.tags.at(tags::internal::ksr) == std::to_string(sample_rate));
397+
{
398+
char buf[32];
399+
std::snprintf(buf, sizeof(buf), "%.6g", sample_rate);
400+
CHECK(span.tags.at(tags::internal::ksr) == std::string(buf));
401+
}
396402
if (sample_rate == 1.0) {
397403
REQUIRE(span.numeric_tags.at(
398404
tags::internal::rule_limiter_sample_rate) == 1.0);

0 commit comments

Comments
 (0)