Skip to content

Commit a9f4269

Browse files
author
elsa
committed
feat: write in tls on span creation
* BUILD.bazel * include/datadog/tls_storage.h * include/datadog/trace_segment.h * include/datadog/tracer.h * src/datadog/trace_segment.cpp * src/datadog/tracer.cpp
1 parent 21d74f3 commit a9f4269

File tree

6 files changed

+55
-8
lines changed

6 files changed

+55
-8
lines changed

BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ cc_library(
109109
"include/datadog/span_matcher.h",
110110
"include/datadog/span_sampler_config.h",
111111
"include/datadog/string_view.h",
112+
"include/datadog/tls_storage.h",
112113
"include/datadog/tracer.h",
113114
"include/datadog/tracer_config.h",
114115
"include/datadog/tracer_signature.h",

include/datadog/tls_storage.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#pragma once
2+
3+
#include <datadog/trace_id.h>
4+
5+
#include <array>
6+
#include <cstdint>
7+
8+
// Global struct used to exposed thread-specific information.
9+
// https://github.com/elastic/apm/blob/149cd3e39a77a58002344270ed2ad35357bdd02d/specs/agents/universal-profiling-integration.md#thread-local-storage-layout
10+
11+
namespace datadog {
12+
namespace tracing {
13+
struct __attribute__((packed)) TLSStorage {
14+
uint16_t layout_minor_version;
15+
uint8_t valid;
16+
uint8_t trace_present;
17+
uint8_t trace_flags;
18+
uint64_t trace_id_low;
19+
uint64_t trace_id_high;
20+
uint64_t span_id;
21+
uint64_t transaction_id;
22+
};
23+
24+
} // namespace tracing
25+
} // namespace datadog

include/datadog/trace_segment.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ class TraceSegment {
102102
const Optional<std::string>& origin() const;
103103
Optional<SamplingDecision> sampling_decision() const;
104104

105+
uint64_t local_root_id() const;
106+
105107
Logger& logger() const;
106108

107109
// Inject trace context for the specified `span` into the specified `writer`.

include/datadog/tracer.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
// obtained from a `TracerConfig` via the `finalize_config` function. See
1111
// `tracer_config.h`.
1212

13-
#include <threads.h>
13+
#include <datadog/tls_storage.h>
1414

1515
#include <cstddef>
1616
#include <memory>
@@ -26,7 +26,8 @@
2626
#include "tracer_signature.h"
2727

2828
extern const void* elastic_apm_profiling_correlation_process_storage_v1;
29-
extern thread_local void* elastic_apm_profiling_correlation_tls_v1;
29+
extern thread_local struct datadog::tracing::TLSStorage*
30+
elastic_apm_profiling_correlation_tls_v1;
3031

3132
namespace datadog {
3233
namespace tracing {

src/datadog/trace_segment.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@ Optional<SamplingDecision> TraceSegment::sampling_decision() const {
142142
return sampling_decision_;
143143
}
144144

145+
uint64_t TraceSegment::local_root_id() const { return spans_.front()->span_id; }
146+
145147
Logger& TraceSegment::logger() const { return *logger_; }
146148

147149
void TraceSegment::register_span(std::unique_ptr<SpanData> span) {

src/datadog/tracer.cpp

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include <algorithm>
1414
#include <cassert>
15+
#include <memory>
1516

1617
#include "config_manager.h"
1718
#include "datadog_agent.h"
@@ -30,7 +31,10 @@
3031
#include "w3c_propagation.h"
3132

3233
const void* elastic_apm_profiling_correlation_process_storage_v1 = nullptr;
33-
thread_local void* elastic_apm_profiling_correlation_tls_v1 = nullptr;
34+
thread_local struct datadog::tracing::TLSStorage*
35+
elastic_apm_profiling_correlation_tls_v1 = nullptr;
36+
thread_local std::unique_ptr<datadog::tracing::TLSStorage> tls_info_holder =
37+
nullptr;
3438

3539
namespace datadog {
3640
namespace tracing {
@@ -115,11 +119,23 @@ Tracer::Tracer(const FinalizedTracerConfig& config,
115119
store_config();
116120
}
117121

118-
void Tracer::correlate(const Span&) {
119-
// TODO: update this variablle with data
120-
// See Layout:
121-
// https://github.com/elastic/apm/blob/149cd3e39a77a58002344270ed2ad35357bdd02d/specs/agents/universal-profiling-integration.md#thread-local-storage-layout
122-
elastic_apm_profiling_correlation_tls_v1 = (char*)"randomdata\n";
122+
void Tracer::correlate(const Span& span) {
123+
tls_info_holder = std::make_unique<datadog::tracing::TLSStorage>();
124+
elastic_apm_profiling_correlation_tls_v1 = tls_info_holder.get();
125+
126+
struct TLSStorage* tls_data = elastic_apm_profiling_correlation_tls_v1;
127+
tls_data->valid = 0;
128+
129+
tls_data->layout_minor_version = 1;
130+
tls_data->trace_present = 1; // We are in a span so no errors
131+
tls_data->trace_flags = 0; // IDK
132+
auto trace_id = span.trace_id();
133+
tls_data->trace_id_low = trace_id.low;
134+
tls_data->trace_id_high = trace_id.high;
135+
tls_data->span_id = span.id();
136+
tls_data->transaction_id = span.trace_segment().local_root_id();
137+
138+
tls_data->valid = 1;
123139
}
124140

125141
std::string Tracer::config() const {

0 commit comments

Comments
 (0)