Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions sdk/src/trace/samplers/trace_id_ratio.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,13 @@ uint64_t CalculateThresholdFromBuffer(const trace_api::TraceId &trace_id) noexce
// We only use the first 8 bytes of TraceId.
static_assert(trace_api::TraceId::kSize >= 8, "TraceID must be at least 8 bytes long.");

uint64_t res = 0;
std::memcpy(&res, &trace_id, 8);
// Always interpret as big-endian
const uint8_t *data = trace_id.Id().data();
uint64_t res = 0;
for (int i = 0; i < 8; ++i)
{
res = (res << 8) | data[i];
}

double ratio = static_cast<double>(res) / static_cast<double>(UINT64_MAX);

Expand Down
2 changes: 1 addition & 1 deletion sdk/test/trace/trace_id_ratio_sampler_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ TEST(TraceIdRatioBasedSampler, ShouldSampleWithoutContext)
ASSERT_EQ(Decision::RECORD_AND_SAMPLE, sampling_result.decision);
ASSERT_EQ(nullptr, sampling_result.attributes);

constexpr uint8_t buf[] = {0, 0, 0, 0, 0, 0, 0, 0x80, 0, 0, 0, 0, 0, 0, 0, 0};
constexpr uint8_t buf[] = {0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
trace_api::TraceId valid_trace_id(buf);

sampling_result = s1.ShouldSample(trace_api::SpanContext::GetInvalid(), valid_trace_id, "",
Expand Down
Loading