Skip to content

Commit 089779d

Browse files
authored
[RESOURCE DETECTOR] Add the host resource detector (#4418)
1 parent 2499c49 commit 089779d

11 files changed

Lines changed: 433 additions & 4 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ Increment the:
1515

1616
## [Unreleased]
1717

18+
* [RESOURCE DETECTOR] Add the host resource detector
19+
[#4413](https://github.com/open-telemetry/opentelemetry-cpp/issues/4413)
1820
* [CONFIGURATION] Add configuration builders for the container and process
1921
resource detectors
2022
[#4412](https://github.com/open-telemetry/opentelemetry-cpp/issues/4412)

‎install/test/src/test_resource_detectors.cc‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <opentelemetry/resource_detectors/container_detector.h>
77
#include <opentelemetry/resource_detectors/container_detector_builder.h>
88
#include <opentelemetry/resource_detectors/env_entity_detector.h>
9+
#include <opentelemetry/resource_detectors/host_detector.h>
910
#include <opentelemetry/resource_detectors/process_detector.h>
1011
#include <opentelemetry/resource_detectors/process_detector_builder.h>
1112
#include <memory>
@@ -26,6 +27,14 @@ TEST(ResourceDetectorsInstall, EnvEntityDetector)
2627
ASSERT_NO_THROW(auto resource = detector->Detect());
2728
}
2829

30+
TEST(ResourceDetectorsInstall, HostResourceDetector)
31+
{
32+
std::unique_ptr<opentelemetry::sdk::resource::ResourceDetector> detector =
33+
std::make_unique<opentelemetry::resource_detector::HostResourceDetector>();
34+
ASSERT_TRUE(detector != nullptr);
35+
ASSERT_NO_THROW(auto resource = detector->Detect());
36+
}
37+
2938
TEST(ResourceDetectorsInstall, ProcessResourceDetector)
3039
{
3140
std::unique_ptr<opentelemetry::sdk::resource::ResourceDetector> detector =

‎resource_detectors/BUILD‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ cc_library(
1717
"src/container_detector.cc",
1818
"src/container_detector_utils.cc",
1919
"src/env_entity_detector.cc",
20+
"src/host_detector.cc",
21+
"src/host_detector_utils.cc",
2022
"src/process_detector.cc",
2123
"src/process_detector_utils.cc",
2224
],

‎resource_detectors/CMakeLists.txt‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@
33

44
add_library(
55
opentelemetry_resource_detectors
6-
src/container_detector_utils.cc src/container_detector.cc
7-
src/env_entity_detector.cc src/process_detector.cc
6+
src/container_detector_utils.cc
7+
src/container_detector.cc
8+
src/env_entity_detector.cc
9+
src/host_detector.cc
10+
src/host_detector_utils.cc
11+
src/process_detector.cc
812
src/process_detector_utils.cc)
913

1014
set_target_properties(opentelemetry_resource_detectors
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
#pragma once
5+
6+
#include <string>
7+
8+
#include "opentelemetry/version.h"
9+
10+
OPENTELEMETRY_BEGIN_NAMESPACE
11+
namespace resource_detector
12+
{
13+
namespace detail
14+
{
15+
16+
/**
17+
* Retrieves the hostname.
18+
* Platform-specific behavior:
19+
* - Windows: Uses GetComputerNameExW() with ComputerNameDnsHostname.
20+
* - Linux/MacOS: Uses gethostname().
21+
* @return the hostname or an empty string on error
22+
*/
23+
std::string GetHostName();
24+
25+
/**
26+
* Returns the host.arch semantic convention value for the architecture this
27+
* binary was compiled for, or an empty string when the architecture is not one
28+
* of the values the semantic conventions enumerate.
29+
*/
30+
std::string GetHostArch();
31+
32+
/**
33+
* Retrieves a unique identifier for the host.
34+
* Platform-specific behavior:
35+
* - Windows: Reads the MachineGuid value from the
36+
* HKLM\SOFTWARE\Microsoft\Cryptography registry key.
37+
* - MacOS: Uses gethostuuid().
38+
* - Linux/Unix: Reads /etc/machine-id, falling back to
39+
* /var/lib/dbus/machine-id.
40+
* @return the host id or an empty string when no source is available
41+
*/
42+
std::string GetHostId();
43+
44+
/**
45+
* Reads a machine id from the first line of the given file, trimmed of
46+
* surrounding whitespace. This function is the Linux implementation behind
47+
* GetHostId() and is exposed for testability.
48+
* @param file_path path of the machine id file
49+
* @return the machine id or an empty string on error
50+
*/
51+
std::string GetHostIdFromFile(const char *file_path);
52+
53+
} // namespace detail
54+
} // namespace resource_detector
55+
OPENTELEMETRY_END_NAMESPACE
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
#pragma once
5+
6+
#include "opentelemetry/sdk/resource/resource_detector.h"
7+
#include "opentelemetry/version.h"
8+
9+
OPENTELEMETRY_BEGIN_NAMESPACE
10+
namespace resource_detector
11+
{
12+
13+
/**
14+
* HostResourceDetector to detect resource attributes describing the host the process
15+
* runs on. It sets host.name, host.arch and host.id following the OpenTelemetry
16+
* semantic conventions.
17+
*
18+
* Platform specific behavior and limitations:
19+
* - host.name is read with gethostname() on Linux and MacOS and with
20+
* GetComputerNameExW() on Windows.
21+
* - host.id is read from /etc/machine-id (falling back to /var/lib/dbus/machine-id)
22+
* on Linux, from gethostuuid() on MacOS and from the
23+
* HKLM\SOFTWARE\Microsoft\Cryptography\MachineGuid registry value on Windows.
24+
* The attribute is omitted when no source is available, for example in Linux
25+
* containers without a machine id.
26+
* - host.arch is derived at compile time from the architecture the SDK was built
27+
* for and is omitted when that architecture has no host.arch value in the
28+
* semantic conventions.
29+
*/
30+
class HostResourceDetector : public opentelemetry::sdk::resource::ResourceDetector
31+
{
32+
public:
33+
opentelemetry::sdk::resource::Resource Detect() noexcept override;
34+
};
35+
36+
} // namespace resource_detector
37+
OPENTELEMETRY_END_NAMESPACE
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
#include "opentelemetry/resource_detectors/host_detector.h"
5+
#include "opentelemetry/nostd/variant.h"
6+
#include "opentelemetry/resource_detectors/detail/host_detector_utils.h"
7+
#include "opentelemetry/sdk/common/global_log_handler.h"
8+
#include "opentelemetry/sdk/resource/resource.h"
9+
#include "opentelemetry/sdk/resource/resource_detector.h"
10+
#include "opentelemetry/semconv/incubating/host_attributes.h"
11+
#include "opentelemetry/semconv/schema_url.h"
12+
#include "opentelemetry/version.h"
13+
14+
#include <exception>
15+
#include <ostream>
16+
#include <string>
17+
#include <utility>
18+
19+
OPENTELEMETRY_BEGIN_NAMESPACE
20+
namespace resource_detector
21+
{
22+
23+
opentelemetry::sdk::resource::Resource HostResourceDetector::Detect() noexcept
24+
{
25+
opentelemetry::sdk::resource::ResourceAttributes attributes;
26+
27+
try
28+
{
29+
std::string host_name = opentelemetry::resource_detector::detail::GetHostName();
30+
if (!host_name.empty())
31+
{
32+
attributes[semconv::host::kHostName] = std::move(host_name);
33+
}
34+
}
35+
catch (const std::exception &ex)
36+
{
37+
OTEL_INTERNAL_LOG_ERROR("[Host Resource Detector] " << "Error extracting the host name: "
38+
<< ex.what());
39+
}
40+
41+
std::string host_arch = opentelemetry::resource_detector::detail::GetHostArch();
42+
if (!host_arch.empty())
43+
{
44+
attributes[semconv::host::kHostArch] = std::move(host_arch);
45+
}
46+
47+
try
48+
{
49+
std::string host_id = opentelemetry::resource_detector::detail::GetHostId();
50+
if (!host_id.empty())
51+
{
52+
attributes[semconv::host::kHostId] = std::move(host_id);
53+
}
54+
}
55+
catch (const std::exception &ex)
56+
{
57+
OTEL_INTERNAL_LOG_ERROR("[Host Resource Detector] " << "Error extracting the host id: "
58+
<< ex.what());
59+
}
60+
61+
return ResourceDetector::Create(attributes, semconv::kSchemaUrl);
62+
}
63+
64+
} // namespace resource_detector
65+
OPENTELEMETRY_END_NAMESPACE
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
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

‎resource_detectors/test/BUILD‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ cc_test(
88
srcs = [
99
"container_detector_test.cc",
1010
"env_entity_detector_test.cc",
11+
"host_detector_test.cc",
1112
"process_detector_test.cc",
1213
],
1314
tags = ["test"],

‎resource_detectors/test/CMakeLists.txt‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
# SPDX-License-Identifier: Apache-2.0
33

44
add_executable(
5-
resource_detector_test container_detector_test.cc process_detector_test.cc
6-
env_entity_detector_test.cc)
5+
resource_detector_test container_detector_test.cc host_detector_test.cc
6+
process_detector_test.cc env_entity_detector_test.cc)
77

88
# Link the required dependencies
99
target_link_libraries(

0 commit comments

Comments
 (0)