Skip to content

Commit fe712b5

Browse files
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 fdead14 commit fe712b5

File tree

6 files changed

+48
-4
lines changed

6 files changed

+48
-4
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: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#pragma once
2+
3+
#include <datadog/trace_id.h>
4+
5+
#include <array>
6+
#include <cstdint>
7+
8+
namespace datadog {
9+
namespace tracing {
10+
struct __attribute__((packed)) TLSStorage {
11+
uint16_t layout_minor_version;
12+
uint8_t valid;
13+
uint8_t trace_present;
14+
uint8_t trace_flags;
15+
uint64_t trace_id_low;
16+
uint64_t trace_id_high;
17+
uint64_t span_id;
18+
uint64_t transaction_id;
19+
};
20+
} // namespace tracing
21+
} // 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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
// obtained from a `TracerConfig` via the `finalize_config` function. See
1111
// `tracer_config.h`.
1212

13+
#include <datadog/tls_storage.h>
1314
#include <threads.h>
1415

1516
#include <cstddef>
@@ -26,7 +27,8 @@
2627
#include "tracer_signature.h"
2728

2829
extern const void* elastic_apm_profiling_correlation_process_storage_v1;
29-
extern thread_local void* elastic_apm_profiling_correlation_tls_v1;
30+
extern thread_local struct datadog::tracing::TLSStorage*
31+
elastic_apm_profiling_correlation_tls_v1;
3032

3133
namespace datadog {
3234
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: 19 additions & 3 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,8 @@
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;
3436

3537
namespace datadog {
3638
namespace tracing {
@@ -115,11 +117,25 @@ Tracer::Tracer(const FinalizedTracerConfig& config,
115117
store_config();
116118
}
117119

118-
void Tracer::correlate(const Span&) {
120+
void Tracer::correlate(const Span& span) {
119121
// TODO: update this variablle with data
120122
// See Layout:
121123
// 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";
124+
auto tls_storage_ptr = std::make_unique<struct TLSStorage>();
125+
elastic_apm_profiling_correlation_tls_v1 = tls_storage_ptr.get();
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)