Skip to content

Commit 5bcf94e

Browse files
elsaelsakeirouz
authored andcommitted
feat: use custom labels to share trace & span ids
* include/datadog/tracer.h * src/datadog/otel_identifiers.h * src/datadog/span.cpp * src/datadog/trace_segment.cpp * src/datadog/tracer.cpp
1 parent d1243f8 commit 5bcf94e

File tree

5 files changed

+77
-0
lines changed

5 files changed

+77
-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: 15 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,19 @@ 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),
52+
reinterpret_cast<const unsigned char*>(OTEL_SPAN_ID_IDENTIFIER)},
53+
{span_id.size(),
54+
reinterpret_cast<const unsigned char*>(span_id.c_str())});
55+
}
56+
#endif
57+
4358
trace_segment_->span_finished();
4459
}
4560

src/datadog/trace_segment.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
#ifdef __linux__
2+
extern "C" {
3+
#include <customlabels/customlabels.h>
4+
}
5+
#endif
6+
17
#include <datadog/collector.h>
28
#include <datadog/dict_reader.h>
39
#include <datadog/dict_writer.h>
@@ -254,6 +260,15 @@ void TraceSegment::span_finished() {
254260
}
255261
}
256262

263+
#ifdef __linux__
264+
// When all spans are finished, so is the current trace.
265+
if (process_storage != nullptr) {
266+
auto saved_set = custom_labels_current_set;
267+
custom_labels_current_set = nullptr;
268+
custom_labels_labelset_free(saved_set);
269+
}
270+
#endif
271+
257272
telemetry::counter::increment(metrics::tracer::trace_segments_closed);
258273
}
259274

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"
@@ -108,6 +109,27 @@ Tracer::Tracer(const FinalizedTracerConfig& config,
108109
store_config();
109110
}
110111

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

@@ -410,6 +437,11 @@ Expected<Span> Tracer::extract_span(const DictReader& reader,
410437
Span span{span_data_ptr, segment,
411438
[generator = generator_]() { return generator->span_id(); },
412439
clock_};
440+
441+
#ifdef __linux__
442+
if (correlate_full_host_profiles_) correlate(span);
443+
#endif
444+
413445
return span;
414446
}
415447

0 commit comments

Comments
 (0)