Skip to content

Commit ef4d91b

Browse files
bm1549claude
andcommitted
Do not set _dd.p.ksr when using the DEFAULT sampling mechanism
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>
1 parent 28fee0b commit ef4d91b

2 files changed

Lines changed: 15 additions & 5 deletions

File tree

src/datadog/trace_segment.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,8 +328,14 @@ void TraceSegment::make_sampling_decision_if_null() {
328328

329329
update_decision_maker_trace_tag();
330330

331-
trace_tags_.emplace_back(tags::internal::ksr,
332-
format_rate(*sampling_decision_->configured_rate));
331+
// Only set ksr when the sampling mechanism is explicit (agent rate, rule, or
332+
// remote rule). The DEFAULT mechanism means we haven't received any
333+
// configuration from the agent yet, so ksr would be meaningless.
334+
if (sampling_decision_->mechanism &&
335+
*sampling_decision_->mechanism != int(SamplingMechanism::DEFAULT)) {
336+
trace_tags_.emplace_back(tags::internal::ksr,
337+
format_rate(*sampling_decision_->configured_rate));
338+
}
333339
}
334340

335341
void TraceSegment::update_decision_maker_trace_tag() {

test/test_trace_segment.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,9 @@ TEST_CASE("TraceSegment finalization of spans") {
312312
REQUIRE_THAT(span.tags, ContainsSubset(filtered));
313313
// "_dd.p.dm" will be added, because we made a sampling decision.
314314
REQUIRE(span.tags.count("_dd.p.dm") == 1);
315-
REQUIRE(span.tags.count("_dd.p.ksr") == 1);
315+
// "_dd.p.ksr" is NOT set because this uses the DEFAULT mechanism (no
316+
// agent configuration received yet).
317+
REQUIRE(span.tags.count("_dd.p.ksr") == 0);
316318
}
317319

318320
SECTION("rate tags") {
@@ -327,7 +329,8 @@ TEST_CASE("TraceSegment finalization of spans") {
327329
REQUIRE(collector->span_count() == 1);
328330
const auto& span = collector->first_span();
329331
REQUIRE(span.numeric_tags.at(tags::internal::agent_sample_rate) == 1.0);
330-
REQUIRE(span.tags.at(tags::internal::ksr) == "1");
332+
// ksr is NOT set for the DEFAULT mechanism.
333+
REQUIRE(span.tags.count(tags::internal::ksr) == 0);
331334
}
332335

333336
SECTION(
@@ -350,7 +353,8 @@ TEST_CASE("TraceSegment finalization of spans") {
350353
{
351354
REQUIRE(collector_response->span_count() == 1);
352355
const auto& span = collector_response->first_span();
353-
CHECK(span.tags.at(tags::internal::ksr) == "1");
356+
// ksr is NOT set for the DEFAULT mechanism.
357+
CHECK(span.tags.count(tags::internal::ksr) == 0);
354358
}
355359

356360
collector_response->chunks.clear();

0 commit comments

Comments
 (0)