Skip to content

Commit ed1638c

Browse files
khanayan123claude
andcommitted
refactor: move root session ID singleton internals to .cpp file
Hide instance() and flag() in an anonymous namespace in root_session_id.cpp, matching the telemetry Meyer's singleton pattern. Only set() and get() are exposed in the header. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent c7a17b2 commit ed1638c

4 files changed

Lines changed: 36 additions & 20 deletions

File tree

BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ cc_library(
4545
"src/datadog/propagation_style.cpp",
4646
"src/datadog/random.cpp",
4747
"src/datadog/random.h",
48+
"src/datadog/root_session_id.cpp",
4849
"src/datadog/root_session_id.h",
4950
"src/datadog/rate.cpp",
5051
"src/datadog/remote_config/product.cpp",

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ target_sources(dd-trace-cpp-objects
197197
src/datadog/parse_util.cpp
198198
src/datadog/propagation_style.cpp
199199
src/datadog/random.cpp
200+
src/datadog/root_session_id.cpp
200201
src/datadog/rate.cpp
201202
src/datadog/remote_config/product.cpp
202203
src/datadog/remote_config/remote_config.cpp

src/datadog/root_session_id.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include "root_session_id.h"
2+
3+
#include <mutex>
4+
5+
namespace datadog {
6+
namespace tracing {
7+
namespace root_session_id {
8+
9+
namespace {
10+
11+
std::string& instance() {
12+
static std::string id;
13+
return id;
14+
}
15+
16+
std::once_flag& flag() {
17+
static std::once_flag f;
18+
return f;
19+
}
20+
21+
} // namespace
22+
23+
void set(const std::string& id) {
24+
std::call_once(flag(), [&]() { instance() = id; });
25+
}
26+
27+
const std::string& get() { return instance(); }
28+
29+
} // namespace root_session_id
30+
} // namespace tracing
31+
} // namespace datadog

src/datadog/root_session_id.h

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,17 @@
11
#pragma once
22

3-
#include <mutex>
43
#include <string>
54

65
namespace datadog {
76
namespace tracing {
7+
namespace root_session_id {
88

99
// Thread-safe singleton for the root session ID. The first call to `set`
1010
// captures the value; subsequent calls are no-ops. Immutable after init,
1111
// so fork-safe.
12-
13-
namespace root_session_id {
14-
15-
inline std::string& instance() {
16-
static std::string id;
17-
return id;
18-
}
19-
20-
inline std::once_flag& flag() {
21-
static std::once_flag f;
22-
return f;
23-
}
24-
25-
inline void set(const std::string& id) {
26-
std::call_once(flag(), [&]() { instance() = id; });
27-
}
28-
29-
inline const std::string& get() { return instance(); }
12+
void set(const std::string& id);
13+
const std::string& get();
3014

3115
} // namespace root_session_id
32-
3316
} // namespace tracing
3417
} // namespace datadog

0 commit comments

Comments
 (0)