Skip to content

Commit 21d74f3

Browse files
author
elsa
committed
wip: fill in proc storage & expose both variables
* include/datadog/tracer.h * include/datadog/tracer_signature.h * src/datadog/tracer.cpp
1 parent be57fe9 commit 21d74f3

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

include/datadog/tracer.h

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

13+
#include <threads.h>
14+
1315
#include <cstddef>
1416
#include <memory>
1517

@@ -23,6 +25,9 @@
2325
#include "tracer_config.h"
2426
#include "tracer_signature.h"
2527

28+
extern const void* elastic_apm_profiling_correlation_process_storage_v1;
29+
extern thread_local void* elastic_apm_profiling_correlation_tls_v1;
30+
2631
namespace datadog {
2732
namespace tracing {
2833

@@ -107,6 +112,7 @@ class Tracer {
107112
std::string config() const;
108113

109114
private:
115+
void correlate(const Span& span);
110116
void store_config();
111117
};
112118

include/datadog/tracer_signature.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@
1919
// polling the Datadog Agent. See
2020
// `RemoteConfigurationManager::process_response` in `remote_config.h`.
2121

22+
#include <cstring>
23+
#include <memory>
2224
#include <string>
25+
#include <vector>
2326

2427
#include "runtime_id.h"
2528
#include "string_view.h"
@@ -31,6 +34,15 @@
3134
namespace datadog {
3235
namespace tracing {
3336

37+
namespace {
38+
void write_utf8_string(std::vector<uint8_t>& buffer, const std::string& str) {
39+
uint32_t length = str.length();
40+
buffer.insert(buffer.end(), reinterpret_cast<uint8_t*>(&length),
41+
reinterpret_cast<uint8_t*>(&length) + sizeof(length));
42+
buffer.insert(buffer.end(), str.begin(), str.end());
43+
}
44+
} // namespace
45+
3446
struct TracerSignature {
3547
RuntimeID runtime_id;
3648
std::string default_service;
@@ -47,6 +59,28 @@ struct TracerSignature {
4759
library_version(tracer_version),
4860
library_language("cpp"),
4961
library_language_version(DD_TRACE_STRINGIFY(__cplusplus), 6) {}
62+
63+
const std::unique_ptr<uint8_t*> generate_process_correlation_storage() {
64+
std::vector<uint8_t> buffer;
65+
66+
// Currently, layout minor version is 2 to differ from Elastic's
67+
// version which includes a socket path.
68+
// Layout:
69+
// https://github.com/elastic/apm/blob/149cd3e39a77a58002344270ed2ad35357bdd02d/specs/agents/universal-profiling-integration.md#process-storage-layout
70+
uint16_t layout_minor_version = 2;
71+
buffer.insert(buffer.end(),
72+
reinterpret_cast<uint8_t*>(&layout_minor_version),
73+
reinterpret_cast<uint8_t*>(&layout_minor_version) +
74+
sizeof(layout_minor_version));
75+
76+
write_utf8_string(buffer, default_service);
77+
write_utf8_string(buffer, default_environment);
78+
write_utf8_string(buffer, runtime_id.string());
79+
80+
uint8_t* res = new uint8_t[buffer.size()];
81+
memcpy(res, buffer.data(), buffer.size());
82+
return std::make_unique<uint8_t*>(res);
83+
}
5084
};
5185

5286
} // namespace tracing

src/datadog/tracer.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
#include "tracer_telemetry.h"
3030
#include "w3c_propagation.h"
3131

32+
const void* elastic_apm_profiling_correlation_process_storage_v1 = nullptr;
33+
thread_local void* elastic_apm_profiling_correlation_tls_v1 = nullptr;
34+
3235
namespace datadog {
3336
namespace tracing {
3437

@@ -98,6 +101,11 @@ Tracer::Tracer(const FinalizedTracerConfig& config,
98101
}
99102
}
100103

104+
// TODO: change the way this is done to handle programs that fork.
105+
// TODO: add a flag to disable this by default.
106+
elastic_apm_profiling_correlation_process_storage_v1 =
107+
*signature_.generate_process_correlation_storage();
108+
101109
if (config.log_on_startup) {
102110
logger_->log_startup([configuration = this->config()](std::ostream& log) {
103111
log << "DATADOG TRACER CONFIGURATION - " << configuration;
@@ -107,6 +115,13 @@ Tracer::Tracer(const FinalizedTracerConfig& config,
107115
store_config();
108116
}
109117

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";
123+
}
124+
110125
std::string Tracer::config() const {
111126
// clang-format off
112127
auto config = nlohmann::json::object({
@@ -198,6 +213,7 @@ Span Tracer::create_span(const SpanConfig& config) {
198213
Span span{span_data_ptr, segment,
199214
[generator = generator_]() { return generator->span_id(); },
200215
clock_};
216+
correlate(span);
201217
return span;
202218
}
203219

@@ -400,6 +416,7 @@ Expected<Span> Tracer::extract_span(const DictReader& reader,
400416
Span span{span_data_ptr, segment,
401417
[generator = generator_]() { return generator->span_id(); },
402418
clock_};
419+
correlate(span);
403420
return span;
404421
}
405422

0 commit comments

Comments
 (0)