Skip to content

Commit fe47aa6

Browse files
test addition and details folder
1 parent c2f7f45 commit fe47aa6

File tree

9 files changed

+98
-14
lines changed

9 files changed

+98
-14
lines changed

resource_detectors/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ otel_add_component(
3030
PATTERN
3131
"*.h"
3232
PATTERN
33-
"container_detector_utils.h"
33+
"resource_detectors/detail/*"
3434
EXCLUDE)
3535

3636
if(BUILD_TESTING)

resource_detectors/container_detector.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
#include "opentelemetry/resource_detectors/container_detector.h"
55
#include "opentelemetry/nostd/variant.h"
6-
#include "opentelemetry/resource_detectors/container_detector_utils.h"
6+
#include "opentelemetry/resource_detectors/detail/container_detector_utils.h"
77
#include "opentelemetry/sdk/resource/resource.h"
88
#include "opentelemetry/sdk/resource/resource_detector.h"
99
#include "opentelemetry/semconv/incubating/container_attributes.h"

resource_detectors/container_detector_utils.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright The OpenTelemetry Authors
22
// SPDX-License-Identifier: Apache-2.0
33

4-
#include "opentelemetry/resource_detectors/container_detector_utils.h"
4+
#include "opentelemetry/resource_detectors/detail/container_detector_utils.h"
55
#include "opentelemetry/nostd/string_view.h"
66

77
#include <fstream>

resource_detectors/process_detector.cc

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33

44
#include "opentelemetry/resource_detectors/process_detector.h"
55
#include "opentelemetry/nostd/variant.h"
6-
#include "opentelemetry/resource_detectors/process_detector_utils.h"
6+
#include "opentelemetry/resource_detectors/detail/process_detector_utils.h"
7+
#include "opentelemetry/sdk/common/global_log_handler.h"
78
#include "opentelemetry/sdk/resource/resource.h"
89
#include "opentelemetry/sdk/resource/resource_detector.h"
910
#include "opentelemetry/semconv/incubating/process_attributes.h"
1011
#include "opentelemetry/version.h"
1112

1213
#include <stdint.h>
14+
#include <exception>
1315
#include <string>
1416
#include <unordered_map>
1517
#include <utility>
@@ -31,16 +33,32 @@ opentelemetry::sdk::resource::Resource ProcessResourceDetector::Detect() noexcep
3133
opentelemetry::sdk::resource::ResourceAttributes attributes;
3234
attributes[semconv::process::kProcessPid] = pid;
3335

34-
std::string executable_path = opentelemetry::resource_detector::detail::GetExecutablePath(pid);
35-
if (!executable_path.empty())
36+
try
3637
{
37-
attributes[semconv::process::kProcessExecutablePath] = std::move(executable_path);
38+
std::string executable_path = opentelemetry::resource_detector::detail::GetExecutablePath(pid);
39+
if (!executable_path.empty())
40+
{
41+
attributes[semconv::process::kProcessExecutablePath] = std::move(executable_path);
42+
}
43+
}
44+
catch (const ::std::exception &ex)
45+
{
46+
OTEL_INTERNAL_LOG_ERROR("[Process Resource Detector] "
47+
<< "Error extracting the executable path: " << ex.what());
3848
}
3949

40-
std::string command = opentelemetry::resource_detector::detail::GetCommand(pid);
41-
if (!command.empty())
50+
try
51+
{
52+
std::string command = opentelemetry::resource_detector::detail::GetCommand(pid);
53+
if (!command.empty())
54+
{
55+
attributes[semconv::process::kProcessCommand] = std::move(command);
56+
}
57+
}
58+
catch (const std::exception &ex)
4259
{
43-
attributes[semconv::process::kProcessCommand] = std::move(command);
60+
OTEL_INTERNAL_LOG_ERROR("[Process Resource Detector] " << "Error extracting command: "
61+
<< ex.what());
4462
}
4563

4664
return ResourceDetector::Create(attributes);

resource_detectors/process_detector_utils.cc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright The OpenTelemetry Authors
22
// SPDX-License-Identifier: Apache-2.0
33

4-
#include "opentelemetry/resource_detectors/process_detector_utils.h"
4+
#include "opentelemetry/resource_detectors/detail/process_detector_utils.h"
55

66
#include <fstream>
77
#include <string>
@@ -75,12 +75,16 @@ std::string GetCommand(const int32_t &pid)
7575
// so we ignore `pid` and just return the current process's command line.
7676
LPCWSTR wcmd = GetCommandLineW();
7777
if (!wcmd)
78+
{
7879
return std::string();
80+
}
7981

8082
// Convert UTF-16 to UTF-8
8183
int size_needed = WideCharToMultiByte(CP_UTF8, 0, wcmd, -1, NULL, 0, NULL, NULL);
8284
if (size_needed <= 0)
85+
{
8386
return std::string();
87+
}
8488

8589
std::string utf8_command(size_needed - 1, 0); // exclude null terminator
8690
WideCharToMultiByte(CP_UTF8, 0, wcmd, -1, &utf8_command[0], size_needed, NULL, NULL);

resource_detectors/test/container_detector_test.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include <string>
88

99
#include "opentelemetry/nostd/string_view.h"
10-
#include "opentelemetry/resource_detectors/container_detector_utils.h"
10+
#include "opentelemetry/resource_detectors/detail/container_detector_utils.h"
1111

1212
TEST(ContainerIdDetectorTest, ExtractValidContainerIdFromLine)
1313
{

resource_detectors/test/process_detector_test.cc

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,23 @@
33

44
#include <gtest/gtest.h>
55
#include <stdint.h>
6-
#include <cstdio>
76
#include <fstream>
87
#include <string>
98

10-
#include "opentelemetry/resource_detectors/process_detector_utils.h"
9+
#ifdef _MSC_VER
10+
// clang-format off
11+
# include <process.h>
12+
# include <windows.h>
13+
# include <psapi.h>
14+
# define getpid _getpid
15+
// clang-format on
16+
#else
17+
# include <sys/types.h>
18+
# include <unistd.h>
19+
# include <cstdio>
20+
#endif
21+
22+
#include "opentelemetry/resource_detectors/detail/process_detector_utils.h"
1123

1224
TEST(ProcessDetectorUtilsTest, FormFilePath)
1325
{
@@ -46,3 +58,53 @@ TEST(ProcessDetectorUtilsTest, EmptyCommandFile)
4658

4759
std::remove(filename.c_str()); // Cleanup
4860
}
61+
62+
TEST(ProcessDetectorUtilsTest, GetExecutablePathTest)
63+
{
64+
int32_t pid = getpid();
65+
std::string path;
66+
#ifdef _MSC_VER
67+
HANDLE hProcess =
68+
OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, static_cast<DWORD>(pid));
69+
if (!hProcess)
70+
{
71+
path = std::string();
72+
}
73+
else
74+
{
75+
76+
WCHAR wbuffer[MAX_PATH];
77+
DWORD len = GetProcessImageFileNameW(hProcess, wbuffer, MAX_PATH);
78+
CloseHandle(hProcess);
79+
80+
if (len == 0)
81+
{
82+
path = std::string();
83+
}
84+
else
85+
{
86+
int size_needed = WideCharToMultiByte(CP_UTF8, 0, wbuffer, len, NULL, 0, NULL, NULL);
87+
std::string utf8_path(size_needed, 0);
88+
WideCharToMultiByte(CP_UTF8, 0, wbuffer, len, &utf8_path[0], size_needed, NULL, NULL);
89+
90+
path = utf8_path;
91+
}
92+
}
93+
#else
94+
std::string exe_path = opentelemetry::resource_detector::detail::FormFilePath(pid, "exe");
95+
char buffer[4096];
96+
97+
ssize_t len = readlink(exe_path.c_str(), buffer, sizeof(buffer) - 1);
98+
if (len != -1)
99+
{
100+
buffer[len] = '\0';
101+
path = std::string(buffer);
102+
}
103+
else
104+
{
105+
path = std::string();
106+
}
107+
#endif
108+
std::string expected_path = opentelemetry::resource_detector::detail::GetExecutablePath(pid);
109+
EXPECT_EQ(path, expected_path);
110+
}

0 commit comments

Comments
 (0)