Skip to content

Commit fad6060

Browse files
authored
chore: split platform code in different translation unit per OS (#255)
1 parent 6dab714 commit fad6060

File tree

9 files changed

+411
-184
lines changed

9 files changed

+411
-184
lines changed

BUILD.bazel

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ cc_library(
2828
"src/datadog/logger.cpp",
2929
"src/datadog/msgpack.cpp",
3030
"src/datadog/parse_util.cpp",
31-
"src/datadog/platform_util.cpp",
3231
"src/datadog/propagation_style.cpp",
3332
"src/datadog/random.cpp",
3433
"src/datadog/rate.cpp",
@@ -84,7 +83,20 @@ cc_library(
8483
"src/datadog/threaded_event_scheduler.h",
8584
"src/datadog/trace_sampler.h",
8685
"src/datadog/w3c_propagation.h",
86+
] + select({
87+
"@platforms//os:windows": [
88+
"src/datadog/platform_util_windows.cpp",
8789
],
90+
"@platforms//os:linux": [
91+
"src/datadog/platform_util_unix.cpp",
92+
],
93+
"@platforms//os:macos": [
94+
"src/datadog/platform_util_darwin.cpp",
95+
],
96+
"//conditions:default": [
97+
"src/datadog/platform_util_unknown.cpp",
98+
],
99+
}),
88100
hdrs = [
89101
"include/datadog/baggage.h",
90102
"include/datadog/cerr_logger.h",

CMakeLists.txt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,6 @@ target_sources(dd-trace-cpp-objects
184184
src/datadog/logger.cpp
185185
src/datadog/msgpack.cpp
186186
src/datadog/parse_util.cpp
187-
src/datadog/platform_util.cpp
188187
src/datadog/propagation_style.cpp
189188
src/datadog/random.cpp
190189
src/datadog/rate.cpp
@@ -212,6 +211,16 @@ target_sources(dd-trace-cpp-objects
212211
src/datadog/w3c_propagation.cpp
213212
)
214213

214+
if (WIN32)
215+
target_sources(dd-trace-cpp-objects PRIVATE src/datadog/platform_util_windows.cpp)
216+
elseif (APPLE)
217+
target_sources(dd-trace-cpp-objects PRIVATE src/datadog/platform_util_darwin.cpp)
218+
elseif (UNIX)
219+
target_sources(dd-trace-cpp-objects PRIVATE src/datadog/platform_util_unix.cpp)
220+
else ()
221+
target_sources(dd-trace-cpp-objects PRIVATE src/datadog/platform_util_unknown.cpp)
222+
endif ()
223+
215224
target_include_directories(dd-trace-cpp-objects
216225
PRIVATE
217226
${CMAKE_CURRENT_SOURCE_DIR}/src/datadog

MODULE.bazel

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
module(
22
name = "dd-trace-cpp",
3-
version = "",
3+
version = "2.0.0",
44
)
55

6+
bazel_dep(
7+
name = "platforms",
8+
version = "0.0.11"
9+
)
610
bazel_dep(
711
name = "bazel_skylib",
812
version = "1.2.1",

WORKSPACE

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,9 @@ http_archive(
2626
urls = ["https://github.com/bazelbuild/bazel-skylib/releases/download/1.2.1/bazel-skylib-1.2.1.tar.gz"],
2727
sha256 = "f7be3474d42aae265405a592bb7da8e171919d74c16f082a5457840f06054728",
2828
)
29+
30+
http_archive(
31+
name = "platforms",
32+
urls = ["https://github.com/bazelbuild/platforms/releases/download/0.0.8/platforms-0.0.8.tar.gz"],
33+
sha256 = "8150406605389ececb6da07cbcb509d5637a3ab9a24bc69b1101531367d89d74",
34+
)

src/datadog/platform_util.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,19 @@
66
#include <datadog/string_view.h>
77

88
#include <string>
9+
#include <vector>
10+
11+
// clang-format off
12+
#if defined(__x86_64__) || defined(_M_X64)
13+
# define DD_SDK_CPU_ARCH "x86_64"
14+
#elif defined(i386) || defined(__i386__) || defined(__i386) || defined(_M_IX86)
15+
# define DD_SDK_CPU_ARCH "x86"
16+
#elif defined(__aarch64__) || defined(_M_ARM64)
17+
# define DD_SDK_CPU_ARCH "arm64"
18+
#else
19+
# define DD_SDK_CPU_ARCH "unknown"
20+
#endif
21+
// clang-format on
922

1023
namespace datadog {
1124
namespace tracing {
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
#include <pthread.h>
2+
#include <sys/sysctl.h>
3+
#include <sys/types.h>
4+
#include <sys/utsname.h>
5+
#include <unistd.h>
6+
7+
#include <cassert>
8+
#include <cstdint>
9+
#include <fstream>
10+
#include <regex>
11+
12+
#include "platform_util.h"
13+
14+
#define DD_SDK_OS "Darwin"
15+
#define DD_SDK_KERNEL "Darwin"
16+
17+
namespace datadog {
18+
namespace tracing {
19+
namespace {
20+
21+
std::string get_os_version() {
22+
char os_version[20] = "";
23+
size_t len = sizeof(os_version);
24+
25+
sysctlbyname("kern.osproductversion", os_version, &len, NULL, 0);
26+
return os_version;
27+
}
28+
29+
HostInfo _get_host_info() {
30+
HostInfo res;
31+
32+
struct utsname buffer;
33+
if (uname(&buffer) != 0) {
34+
return res;
35+
}
36+
37+
res.os = DD_SDK_OS;
38+
res.os_version = get_os_version();
39+
res.hostname = buffer.nodename;
40+
res.cpu_architecture = DD_SDK_CPU_ARCH;
41+
res.kernel_name = DD_SDK_KERNEL;
42+
res.kernel_version = buffer.version;
43+
res.kernel_release = buffer.release;
44+
45+
return res;
46+
}
47+
48+
} // namespace
49+
50+
HostInfo get_host_info() {
51+
static const HostInfo host_info = _get_host_info();
52+
return host_info;
53+
}
54+
55+
std::string get_hostname() { return get_host_info().hostname; }
56+
57+
int get_process_id() { return ::getpid(); }
58+
59+
std::string get_process_name() {
60+
const char* process_name = getprogname();
61+
return (process_name != nullptr) ? process_name : "unknown-service";
62+
}
63+
64+
int at_fork_in_child(void (*on_fork)()) {
65+
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_atfork.html
66+
return pthread_atfork(/*before fork*/ nullptr, /*in parent*/ nullptr,
67+
/*in child*/ on_fork);
68+
}
69+
70+
InMemoryFile::InMemoryFile(void* handle) : handle_(handle) {}
71+
72+
InMemoryFile::~InMemoryFile() {}
73+
74+
InMemoryFile::InMemoryFile(InMemoryFile&& rhs) {
75+
std::swap(rhs.handle_, handle_);
76+
}
77+
78+
InMemoryFile& InMemoryFile::operator=(InMemoryFile&& rhs) {
79+
std::swap(handle_, rhs.handle_);
80+
return *this;
81+
}
82+
83+
bool InMemoryFile::write_then_seal(const std::string&) { return false; }
84+
Expected<InMemoryFile> InMemoryFile::make(StringView) {
85+
return Error{Error::Code::NOT_IMPLEMENTED, "In-memory file not implemented"};
86+
}
87+
88+
namespace container {
89+
90+
Optional<std::string> find_container_id(std::istream& source) {
91+
std::string line;
92+
93+
// Look for Docker container IDs in the basic format: `docker-<uuid>.scope`.
94+
constexpr std::string_view docker_str = "docker-";
95+
96+
while (std::getline(source, line)) {
97+
// Example:
98+
// `0::/system.slice/docker-abcdef0123456789abcdef0123456789.scope`
99+
if (auto beg = line.find(docker_str); beg != std::string::npos) {
100+
beg += docker_str.size();
101+
auto end = line.find(".scope", beg);
102+
if (end == std::string::npos || end - beg <= 0) {
103+
continue;
104+
}
105+
106+
auto container_id = line.substr(beg, end - beg);
107+
return container_id;
108+
}
109+
}
110+
111+
// Reset the stream to the beginning.
112+
source.clear();
113+
source.seekg(0);
114+
115+
// Perform a second pass using a regular expression for matching container IDs
116+
// in a Fargate environment. This two-step approach is used because STL
117+
// `regex` is relatively slow, so we avoid using it unless necessary.
118+
static const std::string uuid_regex_str =
119+
"[0-9a-f]{8}[-_][0-9a-f]{4}[-_][0-9a-f]{4}[-_][0-9a-f]{4}[-_][0-9a-f]{12}"
120+
"|(?:[0-9a-f]{8}(?:-[0-9a-f]{4}){4}$)";
121+
static const std::string container_regex_str = "[0-9a-f]{64}";
122+
static const std::string task_regex_str = "[0-9a-f]{32}-\\d+";
123+
static const std::regex path_reg("(?:.+)?(" + uuid_regex_str + "|" +
124+
container_regex_str + "|" + task_regex_str +
125+
")(?:\\.scope)?$");
126+
127+
while (std::getline(source, line)) {
128+
// Example:
129+
// `0::/system.slice/docker-abcdef0123456789abcdef0123456789.scope`
130+
std::smatch match;
131+
if (std::regex_match(line, match, path_reg) && match.size() == 2) {
132+
assert(match.ready());
133+
assert(match.size() == 2);
134+
135+
return match.str(1);
136+
}
137+
}
138+
139+
return nullopt;
140+
}
141+
142+
Optional<ContainerID> get_id() { return nullopt; }
143+
144+
} // namespace container
145+
146+
} // namespace tracing
147+
} // namespace datadog

0 commit comments

Comments
 (0)