|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +#include "opentelemetry/resource_detectors/detail/host_detector_utils.h" |
| 5 | + |
| 6 | +#include <fstream> |
| 7 | +#include <string> |
| 8 | + |
| 9 | +#ifdef _MSC_VER |
| 10 | +// clang-format off |
| 11 | +# include <windows.h> |
| 12 | +# pragma comment(lib, "advapi32.lib") |
| 13 | +// clang-format on |
| 14 | +#elif defined(__APPLE__) |
| 15 | +# include <unistd.h> |
| 16 | +# include <uuid/uuid.h> |
| 17 | +# include <ctime> |
| 18 | +#else |
| 19 | +# include <unistd.h> |
| 20 | +#endif |
| 21 | + |
| 22 | +#include "opentelemetry/semconv/incubating/host_attributes.h" |
| 23 | +#include "opentelemetry/version.h" |
| 24 | + |
| 25 | +OPENTELEMETRY_BEGIN_NAMESPACE |
| 26 | +namespace resource_detector |
| 27 | +{ |
| 28 | +namespace detail |
| 29 | +{ |
| 30 | + |
| 31 | +#if !defined(_MSC_VER) && !defined(__APPLE__) |
| 32 | +constexpr const char *kMachineIdPath = "/etc/machine-id"; |
| 33 | +constexpr const char *kDbusMachineIdPath = "/var/lib/dbus/machine-id"; |
| 34 | +#endif |
| 35 | + |
| 36 | +std::string GetHostName() |
| 37 | +{ |
| 38 | +#ifdef _MSC_VER |
| 39 | + DWORD size = 0; |
| 40 | + GetComputerNameExW(ComputerNameDnsHostname, nullptr, &size); |
| 41 | + if (size == 0) |
| 42 | + { |
| 43 | + return std::string(); |
| 44 | + } |
| 45 | + |
| 46 | + std::wstring wname(size, L'\0'); |
| 47 | + if (!GetComputerNameExW(ComputerNameDnsHostname, &wname[0], &size)) |
| 48 | + { |
| 49 | + return std::string(); |
| 50 | + } |
| 51 | + |
| 52 | + // Convert UTF-16 to UTF-8 |
| 53 | + int size_needed = WideCharToMultiByte(CP_UTF8, 0, wname.c_str(), static_cast<int>(size), nullptr, |
| 54 | + 0, nullptr, nullptr); |
| 55 | + if (size_needed <= 0) |
| 56 | + { |
| 57 | + return std::string(); |
| 58 | + } |
| 59 | + std::string utf8_name(size_needed, 0); |
| 60 | + WideCharToMultiByte(CP_UTF8, 0, wname.c_str(), static_cast<int>(size), &utf8_name[0], size_needed, |
| 61 | + nullptr, nullptr); |
| 62 | + return utf8_name; |
| 63 | +#else |
| 64 | + char buffer[256]; |
| 65 | + if (gethostname(buffer, sizeof(buffer)) != 0) |
| 66 | + { |
| 67 | + return std::string(); |
| 68 | + } |
| 69 | + buffer[sizeof(buffer) - 1] = '\0'; |
| 70 | + return std::string(buffer); |
| 71 | +#endif |
| 72 | +} |
| 73 | + |
| 74 | +std::string GetHostArch() |
| 75 | +{ |
| 76 | +#if defined(_M_AMD64) || defined(__x86_64__) |
| 77 | + return semconv::host::HostArchValues::kAmd64; |
| 78 | +#elif defined(_M_ARM64) || defined(__aarch64__) |
| 79 | + return semconv::host::HostArchValues::kArm64; |
| 80 | +#elif defined(_M_ARM) || defined(__arm__) |
| 81 | + return semconv::host::HostArchValues::kArm32; |
| 82 | +#elif defined(_M_IX86) || defined(__i386__) |
| 83 | + return semconv::host::HostArchValues::kX86; |
| 84 | +#elif defined(__powerpc64__) |
| 85 | + return semconv::host::HostArchValues::kPpc64; |
| 86 | +#elif defined(__powerpc__) |
| 87 | + return semconv::host::HostArchValues::kPpc32; |
| 88 | +#elif defined(__s390x__) |
| 89 | + return semconv::host::HostArchValues::kS390x; |
| 90 | +#elif defined(__ia64__) |
| 91 | + return semconv::host::HostArchValues::kIa64; |
| 92 | +#else |
| 93 | + return std::string(); |
| 94 | +#endif |
| 95 | +} |
| 96 | + |
| 97 | +std::string GetHostIdFromFile(const char *file_path) |
| 98 | +{ |
| 99 | + std::ifstream file(file_path); |
| 100 | + if (!file.is_open()) |
| 101 | + { |
| 102 | + return std::string(); |
| 103 | + } |
| 104 | + |
| 105 | + std::string line; |
| 106 | + std::getline(file, line); |
| 107 | + |
| 108 | + const auto end = line.find_last_not_of(" \t\n\r"); |
| 109 | + if (end == std::string::npos) |
| 110 | + { |
| 111 | + return std::string(); |
| 112 | + } |
| 113 | + const auto start = line.find_first_not_of(" \t\n\r"); |
| 114 | + return line.substr(start, end - start + 1); |
| 115 | +} |
| 116 | + |
| 117 | +std::string GetHostId() |
| 118 | +{ |
| 119 | +#ifdef _MSC_VER |
| 120 | + char buffer[64]; |
| 121 | + DWORD size = sizeof(buffer); |
| 122 | + LSTATUS status = RegGetValueA(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Cryptography", |
| 123 | + "MachineGuid", RRF_RT_REG_SZ, nullptr, buffer, &size); |
| 124 | + if (status != ERROR_SUCCESS || size <= 1) |
| 125 | + { |
| 126 | + return std::string(); |
| 127 | + } |
| 128 | + // size includes the terminating null character |
| 129 | + return std::string(buffer, size - 1); |
| 130 | +#elif defined(__APPLE__) |
| 131 | + uuid_t uuid; |
| 132 | + struct timespec timeout = {1, 0}; |
| 133 | + if (gethostuuid(uuid, &timeout) != 0) |
| 134 | + { |
| 135 | + return std::string(); |
| 136 | + } |
| 137 | + char uuid_str[37]; |
| 138 | + uuid_unparse_lower(uuid, uuid_str); |
| 139 | + return std::string(uuid_str); |
| 140 | +#else |
| 141 | + std::string machine_id = GetHostIdFromFile(kMachineIdPath); |
| 142 | + if (machine_id.empty()) |
| 143 | + { |
| 144 | + machine_id = GetHostIdFromFile(kDbusMachineIdPath); |
| 145 | + } |
| 146 | + return machine_id; |
| 147 | +#endif |
| 148 | +} |
| 149 | + |
| 150 | +} // namespace detail |
| 151 | +} // namespace resource_detector |
| 152 | +OPENTELEMETRY_END_NAMESPACE |
0 commit comments