Skip to content

Commit 841ddb8

Browse files
author
elsa
committed
feat: expose process storage for trace to profile correlation
* include/datadog/tracer.h * include/datadog/tracer_signature.h * src/datadog/tracer.cpp
1 parent 9a4ed40 commit 841ddb8

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

include/datadog/tracer.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
#include "tracer_config.h"
2424
#include "tracer_signature.h"
2525

26+
#ifdef __linux__
27+
extern const void* elastic_apm_profiling_correlation_process_storage_v1;
28+
#endif
29+
2630
namespace datadog {
2731
namespace tracing {
2832

include/datadog/tracer_signature.h

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

22+
#ifdef __linux__
23+
#include <cstring>
24+
#include <memory>
25+
#include <vector>
26+
#endif
27+
2228
#include <string>
2329

2430
#include "runtime_id.h"
@@ -31,6 +37,17 @@
3137
namespace datadog {
3238
namespace tracing {
3339

40+
#ifdef __linux__
41+
namespace {
42+
void write_utf8_string(std::vector<uint8_t>& buffer, const std::string& str) {
43+
uint32_t length = str.length();
44+
buffer.insert(buffer.end(), reinterpret_cast<uint8_t*>(&length),
45+
reinterpret_cast<uint8_t*>(&length) + sizeof(length));
46+
buffer.insert(buffer.end(), str.begin(), str.end());
47+
}
48+
} // namespace
49+
#endif
50+
3451
struct TracerSignature {
3552
RuntimeID runtime_id;
3653
std::string default_service;
@@ -47,6 +64,32 @@ struct TracerSignature {
4764
library_version(tracer_version),
4865
library_language("cpp"),
4966
library_language_version(DD_TRACE_STRINGIFY(__cplusplus), 6) {}
67+
68+
#ifdef __linux__
69+
// The process correlation storage contains information needed to
70+
// correlate traces to profiles generated by dd-otel-host-profiler.
71+
const std::unique_ptr<uint8_t*> generate_process_correlation_storage() {
72+
std::vector<uint8_t> buffer;
73+
74+
// Currently, layout minor version is 2 to differ from Elastic's
75+
// version which includes a socket path.
76+
// Layout:
77+
// https://github.com/elastic/apm/blob/149cd3e39a77a58002344270ed2ad35357bdd02d/specs/agents/universal-profiling-integration.md#process-storage-layout
78+
uint16_t layout_minor_version = 2;
79+
buffer.insert(buffer.end(),
80+
reinterpret_cast<uint8_t*>(&layout_minor_version),
81+
reinterpret_cast<uint8_t*>(&layout_minor_version) +
82+
sizeof(layout_minor_version));
83+
84+
write_utf8_string(buffer, default_service);
85+
write_utf8_string(buffer, default_environment);
86+
write_utf8_string(buffer, runtime_id.string());
87+
88+
uint8_t* res = new uint8_t[buffer.size()];
89+
memcpy(res, buffer.data(), buffer.size());
90+
return std::make_unique<uint8_t*>(res);
91+
}
92+
#endif
5093
};
5194

5295
} // namespace tracing

src/datadog/tracer.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313

1414
#include <algorithm>
1515
#include <cassert>
16+
#ifdef __linux__
17+
#include <memory>
18+
#endif
1619

1720
#include "config_manager.h"
1821
#include "datadog_agent.h"
@@ -29,6 +32,10 @@
2932
#include "trace_sampler.h"
3033
#include "w3c_propagation.h"
3134

35+
#ifdef __linux__
36+
const void* elastic_apm_profiling_correlation_process_storage_v1 = nullptr;
37+
#endif
38+
3239
namespace datadog {
3340
namespace tracing {
3441

@@ -94,6 +101,13 @@ Tracer::Tracer(const FinalizedTracerConfig& config,
94101
}
95102
}
96103

104+
#ifdef __linux__
105+
// TODO: change the way this is done to handle programs that fork.
106+
// TODO: add a flag to disable this by default.
107+
elastic_apm_profiling_correlation_process_storage_v1 =
108+
*signature_.generate_process_correlation_storage();
109+
#endif
110+
97111
if (config.log_on_startup) {
98112
logger_->log_startup([configuration = this->config()](std::ostream& log) {
99113
log << "DATADOG TRACER CONFIGURATION - " << configuration;

0 commit comments

Comments
 (0)