|
| 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