-
Notifications
You must be signed in to change notification settings - Fork 500
[resource_detectors] implementation of process resource detector as per semconv #3591
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
lalitb
merged 11 commits into
open-telemetry:main
from
nikhilbhatia08:implement_process_detector
Aug 20, 2025
Merged
Changes from 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
b60ef3d
[resource_detectors] implementation of process resource detector as p…
nikhilbhatia08 0f3b0b3
removed unclear changes
nikhilbhatia08 08a93c6
Merge branch 'main' into implement_process_detector
lalitb 8c9ecdd
fixes suggested by copilot
nikhilbhatia08 c2f7f45
disable clang-format for windows
nikhilbhatia08 fe47aa6
test addition and details folder
nikhilbhatia08 7dd7658
test for GetCommand and format fix
nikhilbhatia08 20712ac
minor error fix
nikhilbhatia08 fd88bc6
Merge branch 'main' into implement_process_detector
dbarker e92187d
todo for darwin
nikhilbhatia08 66c4b49
buffer len fix
nikhilbhatia08 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
51 changes: 51 additions & 0 deletions
51
resource_detectors/include/opentelemetry/resource_detectors/detail/process_detector_utils.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#pragma once | ||
|
||
#include <stdint.h> | ||
#include <string> | ||
|
||
#include "opentelemetry/version.h" | ||
|
||
OPENTELEMETRY_BEGIN_NAMESPACE | ||
namespace resource_detector | ||
{ | ||
namespace detail | ||
{ | ||
|
||
/** | ||
* Forms a file path for a process type based on the given PID. | ||
* for example - /proc/<pid>/cmdline, /proc/<pid>/exe | ||
*/ | ||
std::string FormFilePath(const int32_t &pid, const char *process_type); | ||
|
||
/** | ||
* Retrieves the absolute file system path to the executable for a given PID. | ||
* | ||
* Platform-specific behavior: | ||
* - Windows: Uses OpenProcess() + GetProcessImageFileNameW(). | ||
* - Linux/Unix: Reads the /proc/<pid>/exe symbolic link. | ||
nikhilbhatia08 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* | ||
* @param pid Process ID. | ||
*/ | ||
std::string GetExecutablePath(const int32_t &pid); | ||
|
||
/** | ||
* Retrieves the command used to launch the process for a given PID. | ||
* Platform-specific behavior: | ||
* - Windows: Uses GetCommandLineW() to get the command of the current process. | ||
* - Linux/Unix: Reads the zeroth string of /proc/<pid>/cmdline file. | ||
*/ | ||
std::string ExtractCommand(const std::string &command_line_path); | ||
|
||
/** | ||
* Retrieves the command used to launch the process for a given PID. | ||
* This function is a wrapper around ExtractCommand() and is provided for convenience and | ||
* testability of ExtractCommand(). | ||
*/ | ||
std::string GetCommand(const int32_t &pid); | ||
|
||
} // namespace detail | ||
} // namespace resource_detector | ||
OPENTELEMETRY_END_NAMESPACE |
35 changes: 35 additions & 0 deletions
35
resource_detectors/include/opentelemetry/resource_detectors/process_detector.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#pragma once | ||
|
||
#include "opentelemetry/sdk/resource/resource.h" | ||
#include "opentelemetry/sdk/resource/resource_detector.h" | ||
#include "opentelemetry/version.h" | ||
|
||
OPENTELEMETRY_BEGIN_NAMESPACE | ||
namespace resource_detector | ||
{ | ||
|
||
/** | ||
* ProcessResourceDetector to detect resource attributes when running in a process. | ||
* This detector extracts metadata such as process ID, executable path, and command line arguments | ||
* and sets attributes like process.pid, process.executable.path, and process.command following | ||
* the OpenTelemetry semantic conventions. | ||
*/ | ||
class ProcessResourceDetector : public opentelemetry::sdk::resource::ResourceDetector | ||
{ | ||
public: | ||
/** | ||
* Detect retrieves the resource attributes for the current process. | ||
* It reads: | ||
* - process.pid from the current process ID | ||
* - process.executable.path from the executable path of the current process | ||
* - process.command from the command used to launch the process | ||
* and returns a Resource with these attributes set. | ||
*/ | ||
opentelemetry::sdk::resource::Resource Detect() noexcept override; | ||
}; | ||
|
||
} // namespace resource_detector | ||
OPENTELEMETRY_END_NAMESPACE |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#include "opentelemetry/resource_detectors/process_detector.h" | ||
#include "opentelemetry/nostd/variant.h" | ||
#include "opentelemetry/resource_detectors/detail/process_detector_utils.h" | ||
#include "opentelemetry/sdk/common/global_log_handler.h" | ||
#include "opentelemetry/sdk/resource/resource.h" | ||
#include "opentelemetry/sdk/resource/resource_detector.h" | ||
#include "opentelemetry/semconv/incubating/process_attributes.h" | ||
#include "opentelemetry/version.h" | ||
|
||
#include <stdint.h> | ||
#include <exception> | ||
#include <ostream> | ||
#include <string> | ||
#include <unordered_map> | ||
#include <utility> | ||
|
||
#ifdef _MSC_VER | ||
# include <process.h> | ||
# define getpid _getpid | ||
#else | ||
# include <unistd.h> | ||
#endif | ||
|
||
OPENTELEMETRY_BEGIN_NAMESPACE | ||
namespace resource_detector | ||
{ | ||
|
||
opentelemetry::sdk::resource::Resource ProcessResourceDetector::Detect() noexcept | ||
dbarker marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
int32_t pid = getpid(); | ||
dbarker marked this conversation as resolved.
Show resolved
Hide resolved
|
||
opentelemetry::sdk::resource::ResourceAttributes attributes; | ||
attributes[semconv::process::kProcessPid] = pid; | ||
|
||
try | ||
{ | ||
std::string executable_path = opentelemetry::resource_detector::detail::GetExecutablePath(pid); | ||
if (!executable_path.empty()) | ||
{ | ||
attributes[semconv::process::kProcessExecutablePath] = std::move(executable_path); | ||
} | ||
} | ||
catch (const ::std::exception &ex) | ||
{ | ||
OTEL_INTERNAL_LOG_ERROR("[Process Resource Detector] " | ||
<< "Error extracting the executable path: " << ex.what()); | ||
} | ||
|
||
try | ||
{ | ||
std::string command = opentelemetry::resource_detector::detail::GetCommand(pid); | ||
if (!command.empty()) | ||
{ | ||
attributes[semconv::process::kProcessCommand] = std::move(command); | ||
} | ||
} | ||
catch (const std::exception &ex) | ||
{ | ||
OTEL_INTERNAL_LOG_ERROR("[Process Resource Detector] " << "Error extracting command: " | ||
<< ex.what()); | ||
} | ||
|
||
return ResourceDetector::Create(attributes); | ||
} | ||
|
||
} // namespace resource_detector | ||
OPENTELEMETRY_END_NAMESPACE |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#include "opentelemetry/resource_detectors/detail/process_detector_utils.h" | ||
|
||
#include <fstream> | ||
#include <string> | ||
|
||
#ifdef _MSC_VER | ||
// clang-format off | ||
# include <windows.h> | ||
# include <psapi.h> | ||
// clang-format on | ||
#else | ||
# include <sys/types.h> | ||
# include <unistd.h> | ||
# include <cstdio> | ||
#endif | ||
|
||
#include "opentelemetry/version.h" | ||
|
||
OPENTELEMETRY_BEGIN_NAMESPACE | ||
namespace resource_detector | ||
{ | ||
namespace detail | ||
{ | ||
|
||
constexpr const char *kExecutableName = "exe"; | ||
constexpr const char *kCmdlineName = "cmdline"; | ||
|
||
std::string GetExecutablePath(const int32_t &pid) | ||
{ | ||
#ifdef _MSC_VER | ||
HANDLE hProcess = | ||
OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, static_cast<DWORD>(pid)); | ||
if (!hProcess) | ||
{ | ||
return std::string(); | ||
} | ||
|
||
WCHAR wbuffer[MAX_PATH]; | ||
DWORD len = GetProcessImageFileNameW(hProcess, wbuffer, MAX_PATH); | ||
CloseHandle(hProcess); | ||
|
||
if (len == 0) | ||
{ | ||
return std::string(); | ||
} | ||
|
||
// Convert UTF-16 to UTF-8 | ||
int size_needed = WideCharToMultiByte(CP_UTF8, 0, wbuffer, len, NULL, 0, NULL, NULL); | ||
std::string utf8_path(size_needed, 0); | ||
WideCharToMultiByte(CP_UTF8, 0, wbuffer, len, &utf8_path[0], size_needed, NULL, NULL); | ||
|
||
return utf8_path; | ||
#else | ||
std::string path = FormFilePath(pid, kExecutableName); | ||
char buffer[4096]; | ||
|
||
ssize_t len = readlink(path.c_str(), buffer, sizeof(buffer) - 1); | ||
if (len != -1) | ||
{ | ||
buffer[len] = '\0'; | ||
return std::string(buffer); | ||
} | ||
|
||
return std::string(); | ||
#endif | ||
} | ||
|
||
std::string GetCommand(const int32_t &pid) | ||
{ | ||
#ifdef _MSC_VER | ||
// On Windows, GetCommandLineW only works for the CURRENT process, | ||
// so we ignore `pid` and just return the current process's command line. | ||
LPCWSTR wcmd = GetCommandLineW(); | ||
if (!wcmd) | ||
dbarker marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
return std::string(); | ||
} | ||
|
||
// Convert UTF-16 to UTF-8 | ||
int size_needed = WideCharToMultiByte(CP_UTF8, 0, wcmd, -1, NULL, 0, NULL, NULL); | ||
if (size_needed <= 0) | ||
dbarker marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
return std::string(); | ||
} | ||
|
||
std::string utf8_command(size_needed - 1, 0); // exclude null terminator | ||
WideCharToMultiByte(CP_UTF8, 0, wcmd, -1, &utf8_command[0], size_needed, NULL, NULL); | ||
dbarker marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return utf8_command; | ||
#else | ||
// This is the path to get the command that was used to start the process | ||
std::string command_line_path = FormFilePath(pid, kCmdlineName); | ||
return ExtractCommand(command_line_path); | ||
#endif | ||
} | ||
|
||
std::string ExtractCommand(const std::string &command_line_path) | ||
{ | ||
std::string command; | ||
std::ifstream command_line_file(command_line_path, std::ios::in | std::ios::binary); | ||
std::getline(command_line_file, command, '\0'); | ||
return command; | ||
} | ||
|
||
std::string FormFilePath(const int32_t &pid, const char *process_type) | ||
{ | ||
char buff[64]; | ||
int len = std::snprintf(buff, sizeof(buff), "/proc/%d/%s", pid, process_type); | ||
return std::string(buff, len); | ||
lalitb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
} // namespace detail | ||
} // namespace resource_detector | ||
OPENTELEMETRY_END_NAMESPACE |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.