diff --git a/changelogs/current.yaml b/changelogs/current.yaml index c84a0083ec8c..ce3e02d9ee8e 100644 --- a/changelogs/current.yaml +++ b/changelogs/current.yaml @@ -100,6 +100,9 @@ bug_fixes: Reverted :ref:`custom header ` extension to its original behavior by disabling automatic XFF header appending that was inadvertently introduced in PR #31831. +- area: tracers + change: | + Avoid possible overflow when setting span attributes in Dynatrace sampler. removed_config_or_runtime: # *Normally occurs at the end of the* :ref:`deprecation period ` diff --git a/source/extensions/tracers/opentelemetry/samplers/dynatrace/sampling_controller.cc b/source/extensions/tracers/opentelemetry/samplers/dynatrace/sampling_controller.cc index e14ad0fa3493..e4a7643730e0 100644 --- a/source/extensions/tracers/opentelemetry/samplers/dynatrace/sampling_controller.cc +++ b/source/extensions/tracers/opentelemetry/samplers/dynatrace/sampling_controller.cc @@ -75,7 +75,7 @@ SamplingState SamplingController::getSamplingState(const std::string& sampling_k } absl::ReaderMutexLock ss_lock{&stream_summary_mutex_}; const uint32_t exp = stream_summary_->getN() / divisor; - return SamplingState{exp}; + return SamplingState{std::min(exp, MAX_SAMPLING_EXPONENT)}; } std::string SamplingController::getSamplingKey(const absl::string_view path_query, diff --git a/test/extensions/tracers/opentelemetry/samplers/dynatrace/sampling_controller_test.cc b/test/extensions/tracers/opentelemetry/samplers/dynatrace/sampling_controller_test.cc index f56f2c86d4a3..17da6ac7cb3b 100644 --- a/test/extensions/tracers/opentelemetry/samplers/dynatrace/sampling_controller_test.cc +++ b/test/extensions/tracers/opentelemetry/samplers/dynatrace/sampling_controller_test.cc @@ -206,6 +206,13 @@ TEST(SamplingControllerTest, TestWarmup) { EXPECT_EQ(sc.getSamplingState("GET_1").getExponent(), 8); EXPECT_EQ(sc.getSamplingState("GET_6").getExponent(), 8); EXPECT_EQ(sc.getSamplingState("GET_789").getExponent(), 8); + + offerEntry(sc, "GET_7", 10000); + EXPECT_EQ(sc.getSamplingState("GET_1").getExponent(), SamplingController::MAX_SAMPLING_EXPONENT); + EXPECT_EQ(sc.getSamplingState("GET_6").getExponent(), SamplingController::MAX_SAMPLING_EXPONENT); + EXPECT_EQ(sc.getSamplingState("GET_789").getExponent(), + SamplingController::MAX_SAMPLING_EXPONENT); + EXPECT_EQ(sc.getSamplingState("GET_7").getExponent(), SamplingController::MAX_SAMPLING_EXPONENT); } // Test getting sampling state from an empty SamplingController