diff --git a/src/datadog/trace_segment.cpp b/src/datadog/trace_segment.cpp index fc70445c..bfcf2917 100644 --- a/src/datadog/trace_segment.cpp +++ b/src/datadog/trace_segment.cpp @@ -14,7 +14,6 @@ #include #include #include -#include #include #include #include @@ -173,9 +172,10 @@ Optional resolve_otel_tracestate_value( } constexpr std::uint64_t max_value = UINT64_C(1) << 56; - std::uint64_t threshold = static_cast( - std::round((1.0 - decision.configured_rate->value()) * - static_cast(max_value))); + const double threshold_value = (1.0 - decision.configured_rate->value()) * + static_cast(max_value); + // The value is non-negative. Adding 0.5 before truncation rounds it. + std::uint64_t threshold = static_cast(threshold_value + 0.5); threshold = std::min(threshold, max_value - 1); std::uint64_t random_value = (~knuth_hash(trace_id.low)) >> 8; diff --git a/test/test_span.cpp b/test/test_span.cpp index 6b6f0d40..cacc5d8e 100644 --- a/test/test_span.cpp +++ b/test/test_span.cpp @@ -988,11 +988,13 @@ TEST_SPAN("OpenTelemetry consistent probability sampling") { }; const auto test_case = GENERATE(values({ + {0.0, 1, false, "rv:f0948a54d43b8e;th:ffffffffffffff"}, {0.01, 1, false, "rv:f0948a54d43b8e;th:fd70a3d70a3d7"}, {0.1, 1, true, "rv:f0948a54d43b8e;th:e6666666666668"}, {0.2, 1, true, "rv:f0948a54d43b8e;th:ccccccccccccd"}, {0.5, 1, true, "rv:f0948a54d43b8e;th:8"}, {0.99, 1, true, "rv:f0948a54d43b8e;th:028f5c28f5c29"}, + {1.0, 1, true, "rv:f0948a54d43b8e;th:0"}, {0.1, UINT64_C(0x03A93EE8B1999F00), true, "rv:e6666666666668;th:e6666666666668"}, {0.05, UINT64_C(5401449561355763072), false,