Skip to content

Commit 39a5f26

Browse files
author
elsa
committed
feat: use custom labels to share trace & span ids
* include/datadog/tracer.h * src/datadog/otel_identifiers.h * src/datadog/span.cpp * src/datadog/tracer.cpp
1 parent 4452fcf commit 39a5f26

File tree

4 files changed

+61
-0
lines changed

4 files changed

+61
-0
lines changed

include/datadog/tracer.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ class Tracer {
106106
std::string config() const;
107107

108108
private:
109+
#ifdef __linux__
110+
void correlate(const Span& span);
111+
#endif
109112
void store_config();
110113
};
111114

src/datadog/otel_identifiers.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#pragma once
2+
3+
namespace datadog {
4+
namespace tracing {
5+
6+
// Based on the OTel Trace API
7+
// https://opentelemetry.io/docs/specs/otel/trace/api
8+
#define OTEL_TRACE_ID_IDENTIFIER "TraceId"
9+
#define OTEL_SPAN_ID_IDENTIFIER "SpanId"
10+
11+
} // namespace tracing
12+
} // namespace datadog

src/datadog/span.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
#include <datadog/span_config.h>
55
#include <datadog/string_view.h>
66
#include <datadog/trace_segment.h>
7+
#include <datadog/tracer.h>
78

89
#include <cassert>
910
#include <string>
1011

12+
#include "otel_identifiers.h"
1113
#include "span_data.h"
1214
#include "tags.h"
1315

@@ -40,6 +42,18 @@ Span::~Span() {
4042
data_->duration = now - data_->start;
4143
}
4244

45+
#ifdef __linux__
46+
// When a span is finished, we must update the span_id to its parent's.
47+
if (process_storage != nullptr && parent_id().has_value()) {
48+
auto span_id = std::to_string(parent_id().value());
49+
custom_labels_labelset_set(
50+
custom_labels_current_set,
51+
{sizeof(OTEL_SPAN_ID_IDENTIFIER), reinterpret_cast<const unsigned char*>(OTEL_SPAN_ID_IDENTIFIER)},
52+
{span_id.size(),
53+
reinterpret_cast<const unsigned char*>(span_id.c_str())});
54+
}
55+
#endif
56+
4357
trace_segment_->span_finished();
4458
}
4559

src/datadog/tracer.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "hex.h"
2222
#include "json.hpp"
2323
#include "msgpack.h"
24+
#include "otel_identifiers.h"
2425
#include "platform_util.h"
2526
#include "random.h"
2627
#include "span_data.h"
@@ -110,6 +111,27 @@ Tracer::Tracer(const FinalizedTracerConfig& config,
110111
store_config();
111112
}
112113

114+
#ifdef __linux__
115+
void Tracer::correlate(const Span& span) {
116+
custom_labels_current_set = custom_labels_labelset_new(2);
117+
auto trace_id = span.trace_id().hex_padded();
118+
custom_labels_labelset_set(
119+
custom_labels_current_set,
120+
{sizeof(OTEL_TRACE_ID_IDENTIFIER),
121+
reinterpret_cast<const unsigned char*>(OTEL_TRACE_ID_IDENTIFIER)},
122+
{trace_id.size(),
123+
reinterpret_cast<const unsigned char*>(trace_id.c_str())});
124+
125+
auto span_id = std::to_string(span.id());
126+
custom_labels_labelset_set(
127+
custom_labels_current_set,
128+
{sizeof(OTEL_SPAN_ID_IDENTIFIER),
129+
reinterpret_cast<const unsigned char*>(OTEL_SPAN_ID_IDENTIFIER)},
130+
{span_id.size(),
131+
reinterpret_cast<const unsigned char*>(span_id.c_str())});
132+
}
133+
#endif
134+
113135
std::string Tracer::config() const {
114136
// clang-format off
115137
auto config = nlohmann::json::object({
@@ -200,6 +222,11 @@ Span Tracer::create_span(const SpanConfig& config) {
200222
Span span{span_data_ptr, segment,
201223
[generator = generator_]() { return generator->span_id(); },
202224
clock_};
225+
226+
#ifdef __linux__
227+
if (correlate_full_host_profiles_) correlate(span);
228+
#endif
229+
203230
return span;
204231
}
205232

@@ -402,6 +429,11 @@ Expected<Span> Tracer::extract_span(const DictReader& reader,
402429
Span span{span_data_ptr, segment,
403430
[generator = generator_]() { return generator->span_id(); },
404431
clock_};
432+
433+
#ifdef __linux__
434+
if (correlate_full_host_profiles_) correlate(span);
435+
#endif
436+
405437
return span;
406438
}
407439

0 commit comments

Comments
 (0)