Skip to content

Commit 0ca941e

Browse files
author
elsa
committed
feat: write process storage
* CMakeLists.txt * include/datadog/environment.h * include/datadog/tracer.h * include/datadog/tracer_config.h * include/datadog/tracer_signature.h * src/datadog/tracer.cpp * src/datadog/tracer_config.cpp
1 parent b19466b commit 0ca941e

File tree

7 files changed

+72
-1
lines changed

7 files changed

+72
-1
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ if (BUILD_SHARED_LIBS)
203203
PUBLIC
204204
dd_trace::obj
205205
CURL::libcurl_shared
206+
customlabels
206207
PRIVATE
207208
dd_trace::specs
208209
)

include/datadog/environment.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ namespace environment {
5050
MACRO(DD_TRACE_TAGS_PROPAGATION_MAX_LENGTH) \
5151
MACRO(DD_VERSION) \
5252
MACRO(DD_TRACE_128_BIT_TRACEID_GENERATION_ENABLED) \
53+
MACRO(DD_TRACE_CORRELATE_FULL_HOST_PROFILES) \
5354
MACRO(DD_TELEMETRY_HEARTBEAT_INTERVAL) \
5455
MACRO(DD_TELEMETRY_METRICS_ENABLED) \
5556
MACRO(DD_TELEMETRY_METRICS_INTERVAL_SECONDS) \

include/datadog/tracer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class Tracer {
5454
Baggage::Options baggage_opts_;
5555
bool baggage_injection_enabled_;
5656
bool baggage_extraction_enabled_;
57+
bool correlate_full_host_profiles_;
5758

5859
public:
5960
// Create a tracer configured using the specified `config`, and optionally:

include/datadog/tracer_config.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,14 @@ struct TracerConfig {
133133
// the `DD_TRACE_128_BIT_TRACEID_GENERATION_ENABLED` environment variable.
134134
Optional<bool> generate_128bit_trace_ids;
135135

136+
// `correlate_full_host_profiles` indicates whether we want to correlate
137+
// traces and spans with profiles generated by the eBPF full host profiler.
138+
// This correlation only works on linux, due to the eBPF-based nature of
139+
// the profiler. It implies writing some process-level and thread-level
140+
// data in variables which the profiler will then read from the process's
141+
// memory.
142+
Optional<bool> correlate_full_host_profiles;
143+
136144
// `runtime_id` denotes the current run of the application in which the tracer
137145
// is embedded. If `runtime_id` is not specified, then it defaults to a
138146
// pseudo-randomly generated value. A server that contains multiple tracers,
@@ -197,6 +205,7 @@ class FinalizedTracerConfig final {
197205
std::shared_ptr<Logger> logger;
198206
bool log_on_startup;
199207
bool generate_128bit_trace_ids;
208+
bool correlate_full_host_profiles;
200209
Optional<RuntimeID> runtime_id;
201210
Clock clock;
202211
std::string integration_name;

include/datadog/tracer_signature.h

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

22+
#ifdef __linux__
23+
#include <cstring>
24+
#include <vector>
25+
extern "C" {
26+
#include <customlabels/customlabels.h>
27+
}
28+
#endif
29+
2230
#include <string>
2331

2432
#include "runtime_id.h"
@@ -31,6 +39,17 @@
3139
namespace datadog {
3240
namespace tracing {
3341

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

5295
} // namespace tracing

src/datadog/tracer.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ Tracer::Tracer(const FinalizedTracerConfig& config,
5858
tags_header_max_size_(config.tags_header_size),
5959
baggage_opts_(config.baggage_opts),
6060
baggage_injection_enabled_(false),
61-
baggage_extraction_enabled_(false) {
61+
baggage_extraction_enabled_(false),
62+
correlate_full_host_profiles_(config.correlate_full_host_profiles) {
6263
telemetry::init(config.telemetry, logger_, config.http_client,
6364
config.event_scheduler, config.agent_url);
6465
if (config.report_hostname) {
@@ -92,6 +93,12 @@ Tracer::Tracer(const FinalizedTracerConfig& config,
9293
}
9394
}
9495

96+
#ifdef __linux__
97+
// TODO: change the way this is done to handle programs that fork.
98+
if (correlate_full_host_profiles_)
99+
signature_.generate_process_correlation_storage();
100+
#endif
101+
95102
if (config.log_on_startup) {
96103
logger_->log_startup([configuration = this->config()](std::ostream& log) {
97104
log << "DATADOG TRACER CONFIGURATION - " << configuration;

src/datadog/tracer_config.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ Expected<TracerConfig> load_tracer_env_config(Logger &logger) {
127127
lookup(environment::DD_TRACE_128_BIT_TRACEID_GENERATION_ENABLED)) {
128128
env_cfg.generate_128bit_trace_ids = !falsy(*enabled_env);
129129
}
130+
if (auto enabled_env =
131+
lookup(environment::DD_TRACE_CORRELATE_FULL_HOST_PROFILES)) {
132+
env_cfg.correlate_full_host_profiles = !falsy(*enabled_env);
133+
}
130134

131135
// Baggage
132136
if (auto baggage_items_env =
@@ -361,6 +365,11 @@ Expected<FinalizedTracerConfig> finalize_config(const TracerConfig &user_config,
361365
ConfigMetadata(ConfigName::GENEREATE_128BIT_TRACE_IDS,
362366
to_string(final_config.generate_128bit_trace_ids), origin);
363367

368+
// Correlate with Full Host Profiles
369+
final_config.correlate_full_host_profiles =
370+
value_or(env_config->correlate_full_host_profiles,
371+
user_config.correlate_full_host_profiles, false);
372+
364373
// Integration name & version
365374
final_config.integration_name = value_or(
366375
env_config->integration_name, user_config.integration_name, "datadog");

0 commit comments

Comments
 (0)