forked from open-telemetry/opentelemetry-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaws_xray_id_generator.cc
42 lines (37 loc) · 1.33 KB
/
aws_xray_id_generator.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
#include "opentelemetry/sdk/trace/aws_xray_id_generator.h"
#include "opentelemetry/version.h"
#include "src/common/random.h"
#include <chrono>
OPENTELEMETRY_BEGIN_NAMESPACE
namespace sdk
{
namespace trace
{
namespace trace_api = opentelemetry::trace;
trace_api::SpanId AwsXrayIdGenerator::GenerateSpanId() noexcept
{
uint8_t span_id_buf[trace_api::SpanId::kSize];
sdk::common::Random::GenerateRandomBuffer(span_id_buf);
return trace_api::SpanId(span_id_buf);
}
trace_api::TraceId AwsXrayIdGenerator::GenerateTraceId() noexcept
{
uint8_t trace_id_buf[trace_api::TraceId::kSize];
const auto p1 = std::chrono::system_clock::now();
uint64_t ts = std::chrono::duration_cast<std::chrono::seconds>(p1.time_since_epoch()).count();
uint64_t lo_rand = sdk::common::Random::GenerateRandom64();
uint64_t hi_rand = sdk::common::Random::GenerateRandom64() & 0x00000000FFFFFFFF;
uint64_t hi = ts << 32 | hi_rand;
memcpy(&trace_id_buf[0], &lo_rand, sizeof(uint64_t));
memcpy(&trace_id_buf[sizeof(uint64_t)], &hi, sizeof(uint64_t));
for(size_t l=0,h=trace_api::TraceId::kSize-1; l < h; l++, h--)
{
std::swap(trace_id_buf[l], trace_id_buf[h]);
}
return trace_api::TraceId(trace_id_buf);
}
} // namespace trace
} // namespace sdk
OPENTELEMETRY_END_NAMESPACE