Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ cc_library(
"src/datadog/logger.cpp",
"src/datadog/msgpack.cpp",
"src/datadog/parse_util.cpp",
"src/datadog/platform_util.cpp",
"src/datadog/propagation_style.cpp",
"src/datadog/random.cpp",
"src/datadog/rate.cpp",
Expand Down Expand Up @@ -84,7 +83,20 @@ cc_library(
"src/datadog/threaded_event_scheduler.h",
"src/datadog/trace_sampler.h",
"src/datadog/w3c_propagation.h",
] + select({
"@platforms//os:windows": [
"src/datadog/platform_util_windows.cpp",
],
"@platforms//os:linux": [
"src/datadog/platform_util_unix.cpp",
],
"@platforms//os:macos": [
"src/datadog/platform_util_darwin.cpp",
],
"//conditions:default": [
"src/datadog/platform_util_unknown.cpp",
],
}),
hdrs = [
"include/datadog/baggage.h",
"include/datadog/cerr_logger.h",
Expand Down
11 changes: 10 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@ target_sources(dd-trace-cpp-objects
src/datadog/logger.cpp
src/datadog/msgpack.cpp
src/datadog/parse_util.cpp
src/datadog/platform_util.cpp
src/datadog/propagation_style.cpp
src/datadog/random.cpp
src/datadog/rate.cpp
Expand Down Expand Up @@ -212,6 +211,16 @@ target_sources(dd-trace-cpp-objects
src/datadog/w3c_propagation.cpp
)

if (WIN32)
target_sources(dd-trace-cpp-objects PRIVATE src/datadog/platform_util_windows.cpp)
elseif (APPLE)
target_sources(dd-trace-cpp-objects PRIVATE src/datadog/platform_util_darwin.cpp)
elseif (UNIX)
target_sources(dd-trace-cpp-objects PRIVATE src/datadog/platform_util_unix.cpp)
else ()
target_sources(dd-trace-cpp-objects PRIVATE src/datadog/platform_util_unknown.cpp)
endif ()

target_include_directories(dd-trace-cpp-objects
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/src/datadog
Expand Down
6 changes: 5 additions & 1 deletion MODULE.bazel
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
module(
name = "dd-trace-cpp",
version = "",
version = "2.0.0",
)

bazel_dep(
name = "platforms",
version = "0.0.11"
)
bazel_dep(
name = "bazel_skylib",
version = "1.2.1",
Expand Down
6 changes: 6 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,9 @@ http_archive(
urls = ["https://github.com/bazelbuild/bazel-skylib/releases/download/1.2.1/bazel-skylib-1.2.1.tar.gz"],
sha256 = "f7be3474d42aae265405a592bb7da8e171919d74c16f082a5457840f06054728",
)

http_archive(
name = "platforms",
urls = ["https://github.com/bazelbuild/platforms/releases/download/0.0.8/platforms-0.0.8.tar.gz"],
sha256 = "8150406605389ececb6da07cbcb509d5637a3ab9a24bc69b1101531367d89d74",
)
13 changes: 13 additions & 0 deletions src/datadog/platform_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@
#include <datadog/string_view.h>

#include <string>
#include <vector>

// clang-format off
#if defined(__x86_64__) || defined(_M_X64)
# define DD_SDK_CPU_ARCH "x86_64"
#elif defined(i386) || defined(__i386__) || defined(__i386) || defined(_M_IX86)
# define DD_SDK_CPU_ARCH "x86"
#elif defined(__aarch64__) || defined(_M_ARM64)
# define DD_SDK_CPU_ARCH "arm64"
#else
# define DD_SDK_CPU_ARCH "unknown"
#endif
// clang-format on

namespace datadog {
namespace tracing {
Expand Down
147 changes: 147 additions & 0 deletions src/datadog/platform_util_darwin.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
#include <pthread.h>
#include <sys/sysctl.h>
#include <sys/types.h>
#include <sys/utsname.h>
#include <unistd.h>

#include <cassert>
#include <cstdint>
#include <fstream>
#include <regex>

#include "platform_util.h"

#define DD_SDK_OS "Darwin"
#define DD_SDK_KERNEL "Darwin"

namespace datadog {
namespace tracing {
namespace {

std::string get_os_version() {
char os_version[20] = "";
size_t len = sizeof(os_version);

sysctlbyname("kern.osproductversion", os_version, &len, NULL, 0);
return os_version;
}

HostInfo _get_host_info() {
HostInfo res;

struct utsname buffer;
if (uname(&buffer) != 0) {
return res;
}

res.os = DD_SDK_OS;
res.os_version = get_os_version();
res.hostname = buffer.nodename;
res.cpu_architecture = DD_SDK_CPU_ARCH;
res.kernel_name = DD_SDK_KERNEL;
res.kernel_version = buffer.version;
res.kernel_release = buffer.release;

return res;
}

} // namespace

HostInfo get_host_info() {
static const HostInfo host_info = _get_host_info();
return host_info;
}

std::string get_hostname() { return get_host_info().hostname; }

int get_process_id() { return ::getpid(); }

std::string get_process_name() {
const char* process_name = getprogname();
return (process_name != nullptr) ? process_name : "unknown-service";
}

int at_fork_in_child(void (*on_fork)()) {
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_atfork.html
return pthread_atfork(/*before fork*/ nullptr, /*in parent*/ nullptr,
/*in child*/ on_fork);
}

InMemoryFile::InMemoryFile(void* handle) : handle_(handle) {}

InMemoryFile::~InMemoryFile() {}

InMemoryFile::InMemoryFile(InMemoryFile&& rhs) {
std::swap(rhs.handle_, handle_);
}

InMemoryFile& InMemoryFile::operator=(InMemoryFile&& rhs) {
std::swap(handle_, rhs.handle_);
return *this;
}

bool InMemoryFile::write_then_seal(const std::string&) { return false; }
Expected<InMemoryFile> InMemoryFile::make(StringView) {
return Error{Error::Code::NOT_IMPLEMENTED, "In-memory file not implemented"};
}

namespace container {

Optional<std::string> find_container_id(std::istream& source) {
std::string line;

// Look for Docker container IDs in the basic format: `docker-<uuid>.scope`.
constexpr std::string_view docker_str = "docker-";

while (std::getline(source, line)) {
// Example:
// `0::/system.slice/docker-abcdef0123456789abcdef0123456789.scope`
if (auto beg = line.find(docker_str); beg != std::string::npos) {
beg += docker_str.size();
auto end = line.find(".scope", beg);
if (end == std::string::npos || end - beg <= 0) {
continue;
}

auto container_id = line.substr(beg, end - beg);
return container_id;
}
}

// Reset the stream to the beginning.
source.clear();
source.seekg(0);

// Perform a second pass using a regular expression for matching container IDs
// in a Fargate environment. This two-step approach is used because STL
// `regex` is relatively slow, so we avoid using it unless necessary.
static const std::string uuid_regex_str =
"[0-9a-f]{8}[-_][0-9a-f]{4}[-_][0-9a-f]{4}[-_][0-9a-f]{4}[-_][0-9a-f]{12}"
"|(?:[0-9a-f]{8}(?:-[0-9a-f]{4}){4}$)";
static const std::string container_regex_str = "[0-9a-f]{64}";
static const std::string task_regex_str = "[0-9a-f]{32}-\\d+";
static const std::regex path_reg("(?:.+)?(" + uuid_regex_str + "|" +
container_regex_str + "|" + task_regex_str +
")(?:\\.scope)?$");

while (std::getline(source, line)) {
// Example:
// `0::/system.slice/docker-abcdef0123456789abcdef0123456789.scope`
std::smatch match;
if (std::regex_match(line, match, path_reg) && match.size() == 2) {
assert(match.ready());
assert(match.size() == 2);

return match.str(1);
}
}

return nullopt;
}

Optional<ContainerID> get_id() { return nullopt; }

} // namespace container

} // namespace tracing
} // namespace datadog
Loading