Skip to content

Commit 115a6ef

Browse files
committed
check of size of trace_id
Signed-off-by: therealak12 <[email protected]>
1 parent fb9896b commit 115a6ef

File tree

3 files changed

+14
-8
lines changed

3 files changed

+14
-8
lines changed

source/extensions/tracers/opentelemetry/samplers/trace_id_ratio_based/BUILD

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ envoy_cc_library(
2626
srcs = ["trace_id_ratio_based_sampler.cc"],
2727
hdrs = ["trace_id_ratio_based_sampler.h"],
2828
deps = [
29+
# "//source/common/common:minimal_logger_lib",
30+
"//source/common/common:logger_lib",
2931
"//source/common/common:safe_memcpy_lib",
3032
"//source/extensions/tracers/opentelemetry:opentelemetry_tracer_lib",
3133
"//source/extensions/tracers/opentelemetry/samplers:sampler_lib",

source/extensions/tracers/opentelemetry/samplers/trace_id_ratio_based/trace_id_ratio_based_sampler.cc

+11-8
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,27 @@
99

1010
#include "envoy/type/v3/percent.pb.h"
1111

12+
#include "source/common/common/logger.h"
1213
#include "source/common/common/safe_memcpy.h"
1314
#include "source/extensions/tracers/opentelemetry/span_context.h"
1415

15-
namespace {
16+
namespace Envoy {
17+
namespace Extensions {
18+
namespace Tracers {
19+
namespace OpenTelemetry {
1620

1721
/**
1822
* @param trace_id a required value to be converted to uint64_t. trace_id must
1923
* at least 8 bytes long. trace_id is expected to be a valid hex string.
2024
* @return Returns the uint64 value associated with first 8 bytes of the trace_id.
2125
*
2226
*/
23-
uint64_t traceIdToUint64(const std::string& trace_id) noexcept {
27+
uint64_t TraceIdRatioBasedSampler::traceIdToUint64(const std::string& trace_id) noexcept {
28+
if (trace_id.size() < 16) {
29+
ENVOY_LOG(warn, "Trace ID is not long enough: {}", trace_id);
30+
return 0;
31+
}
32+
2433
uint8_t buffer[8] = {0};
2534
for (size_t i = 0; i < 8; ++i) {
2635
std::string byte_string = trace_id.substr(i * 2, 2);
@@ -32,12 +41,6 @@ uint64_t traceIdToUint64(const std::string& trace_id) noexcept {
3241

3342
return first_8_bytes;
3443
}
35-
} // namespace
36-
37-
namespace Envoy {
38-
namespace Extensions {
39-
namespace Tracers {
40-
namespace OpenTelemetry {
4144

4245
TraceIdRatioBasedSampler::TraceIdRatioBasedSampler(
4346
const envoy::extensions::tracers::opentelemetry::samplers::v3::TraceIdRatioBasedSamplerConfig&

source/extensions/tracers/opentelemetry/samplers/trace_id_ratio_based/trace_id_ratio_based_sampler.h

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class TraceIdRatioBasedSampler : public Sampler, Logger::Loggable<Logger::Id::tr
3131
OptRef<const Tracing::TraceContext> trace_context,
3232
const std::vector<SpanContext>& links) override;
3333
std::string getDescription() const override;
34+
uint64_t traceIdToUint64(const std::string& trace_id) noexcept;
3435

3536
private:
3637
std::string description_;

0 commit comments

Comments
 (0)