Skip to content

Commit 69eb64c

Browse files
khanayan123claude
andcommitted
refactor: simplify root session ID to single get_or_init() singleton
Replace separate set()/get() with a single get_or_init(runtime_id) that initializes on first call and returns the stored value on subsequent calls. Remove root_session_id from TracerConfig/FinalizedTracerConfig — the singleton is now the sole source of truth, read directly in Tracer construction. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 675ee33 commit 69eb64c

5 files changed

Lines changed: 8 additions & 23 deletions

File tree

include/datadog/tracer_config.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,6 @@ struct TracerConfig {
189189
// This option is ignored if `resource_renaming_enabled` is not `true`.
190190
Optional<bool> resource_renaming_always_simplified_endpoint;
191191

192-
// Populated internally from the root_session_id singleton. Tracks the
193-
// runtime ID of the first C++ process in a fork/spawn chain.
194-
Optional<std::string> root_session_id;
195-
196192
/// A mapping of process-specific tags used to uniquely identify processes.
197193
///
198194
/// The `process_tags` map allows associating arbitrary string-based keys and
@@ -229,7 +225,6 @@ class FinalizedTracerConfig final {
229225
bool log_on_startup;
230226
bool generate_128bit_trace_ids;
231227
Optional<RuntimeID> runtime_id;
232-
Optional<std::string> root_session_id;
233228
Clock clock;
234229
std::string integration_name;
235230
std::string integration_version;

src/datadog/root_session_id.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,14 @@ std::atomic<bool>& initialized() {
2020

2121
} // namespace
2222

23-
void set(const std::string& id) {
23+
const std::string& get_or_init(const std::string& runtime_id) {
2424
bool expected = false;
2525
if (initialized().compare_exchange_strong(expected, true)) {
26-
instance() = id;
26+
instance() = runtime_id;
2727
}
28+
return instance();
2829
}
2930

30-
const std::string& get() { return instance(); }
31-
3231
} // namespace root_session_id
3332
} // namespace tracing
3433
} // namespace datadog

src/datadog/root_session_id.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@ namespace datadog {
66
namespace tracing {
77
namespace root_session_id {
88

9-
// Thread-safe singleton for the root session ID. The first call to `set`
10-
// captures the value; subsequent calls are no-ops. Immutable after init,
11-
// so fork-safe.
12-
void set(const std::string& id);
13-
const std::string& get();
9+
// Meyer's singleton for the root session ID. The first call initializes the
10+
// value; subsequent calls return the same value regardless of the argument.
11+
// Thread-safe via atomic CAS. Immutable after init, so fork-safe.
12+
const std::string& get_or_init(const std::string& runtime_id);
1413

1514
} // namespace root_session_id
1615
} // namespace tracing

src/datadog/tracer.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ Tracer::Tracer(const FinalizedTracerConfig& config,
5050
runtime_id_(config.runtime_id ? *config.runtime_id
5151
: RuntimeID::generate()),
5252
signature_{runtime_id_,
53-
config.root_session_id.value_or(runtime_id_.string()),
53+
root_session_id::get_or_init(runtime_id_.string()),
5454
config.defaults.service, config.defaults.environment},
5555
config_manager_(std::make_shared<ConfigManager>(config)),
5656
collector_(/* see constructor body */),
@@ -66,8 +66,6 @@ Tracer::Tracer(const FinalizedTracerConfig& config,
6666
baggage_extraction_enabled_(false),
6767
tracing_enabled_(config.tracing_enabled),
6868
resource_renaming_mode_(config.resource_renaming_mode) {
69-
root_session_id::set(signature_.root_session_id);
70-
7169
telemetry::init(config.telemetry, signature_, logger_, config.http_client,
7270
config.event_scheduler, config.agent_url);
7371
if (config.report_hostname) {

src/datadog/tracer_config.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
#include "null_logger.h"
1515
#include "parse_util.h"
1616
#include "platform_util.h"
17-
#include "root_session_id.h"
1817
#include "string_util.h"
1918
#include "threaded_event_scheduler.h"
2019

@@ -262,11 +261,6 @@ Expected<TracerConfig> load_tracer_env_config(Logger &logger) {
262261
return std::move(error);
263262
}
264263

265-
const auto &root_sid = root_session_id::get();
266-
if (!root_sid.empty()) {
267-
env_cfg.root_session_id = root_sid;
268-
}
269-
270264
return env_cfg;
271265
}
272266

0 commit comments

Comments
 (0)