From 005eb777285e9e186206726d0c83305f35ce62c9 Mon Sep 17 00:00:00 2001 From: Thomas Ebner <96168670+samohte@users.noreply.github.com> Date: Wed, 20 Nov 2024 20:53:14 +0100 Subject: [PATCH] limit calculated sampling exponent (#37240) Ensure that calculated sampling exponent stays below a certain limit Risk Level: Low Testing: Unit test, manual Docs Changes: N/A Release Notes: N/A Platform Specific Features: Fixes #37199 Signed-off-by: thomas.ebner --- changelogs/current.yaml | 3 +++ .../samplers/dynatrace/sampling_controller.cc | 2 +- .../samplers/dynatrace/sampling_controller_test.cc | 7 +++++++ 3 files changed, 11 insertions(+), 1 deletion(-) 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