From 41d6d16bd36c97f1d2c6562a0830843b35f9bfe7 Mon Sep 17 00:00:00 2001 From: nikhilbhatia08 Date: Fri, 22 Aug 2025 16:59:35 +0530 Subject: [PATCH 1/9] [resource_detectors] implementation of remaining process attributes --- .../detail/process_detector_utils.h | 23 ++++++ resource_detectors/process_detector.cc | 18 +++++ resource_detectors/process_detector_utils.cc | 62 ++++++++++++++++ .../test/process_detector_test.cc | 74 +++++++++++++++++++ 4 files changed, 177 insertions(+) diff --git a/resource_detectors/include/opentelemetry/resource_detectors/detail/process_detector_utils.h b/resource_detectors/include/opentelemetry/resource_detectors/detail/process_detector_utils.h index aeb61a8e28..1780db1b2b 100644 --- a/resource_detectors/include/opentelemetry/resource_detectors/detail/process_detector_utils.h +++ b/resource_detectors/include/opentelemetry/resource_detectors/detail/process_detector_utils.h @@ -40,6 +40,15 @@ std::string GetExecutablePath(const int32_t &pid); */ std::string ExtractCommand(const std::string &command_line_path); +/** + * Extracts the command-line arguments from the command line path. + * Platform-specific behavior: + * - Windows: Uses CommandLineToArgvW() to parse the command line. + * - Linux/Unix: Reads the /proc//cmdline file and splits it into arguments. + * - TODO: Need to implement for Darwin + */ +std::vector ExtractCommandWithArgs(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 @@ -47,6 +56,20 @@ std::string ExtractCommand(const std::string &command_line_path); */ std::string GetCommand(const int32_t &pid); +/** + * Retrieves the command-line arguments used to launch the process for a given PID. + * This function is a wrapper around ExtractCommandWithArgs() and is provided for convenience and + * testability of ExtractCommandWithArgs(). + */ +std::vector GetCommandWithArgs(const int32_t &pid); + +/** + * Converts a vector of command-line arguments into a single command-line string. + * process.command_line is the string representation of the command line that we collected using + * GetCommandWithArgs(). + */ +std::string ConvertCommandArgsToString(const std::vector &command_args); + } // namespace detail } // namespace resource_detector OPENTELEMETRY_END_NAMESPACE diff --git a/resource_detectors/process_detector.cc b/resource_detectors/process_detector.cc index c0bde2555a..8f08a46ddc 100644 --- a/resource_detectors/process_detector.cc +++ b/resource_detectors/process_detector.cc @@ -62,6 +62,24 @@ opentelemetry::sdk::resource::Resource ProcessResourceDetector::Detect() noexcep << ex.what()); } + try + { + std::vector args = + opentelemetry::resource_detector::detail::GetCommandWithArgs(pid); + if (!args.empty()) + { + std::string commandline_args = + opentelemetry::resource_detector::detail::ConvertCommandArgsToString(args); + attributes[semconv::process::kProcessCommandLine] = std::move(commandline_args); + attributes[semconv::process::kProcessCommandArgs] = std::move(args); + } + } + catch (const std::exception &ex) + { + OTEL_INTERNAL_LOG_ERROR("[Process Resource Detector] " + << "Error extracting command with arguments: " << ex.what()); + } + return ResourceDetector::Create(attributes); } diff --git a/resource_detectors/process_detector_utils.cc b/resource_detectors/process_detector_utils.cc index 27a083ea7e..3dd732c786 100644 --- a/resource_detectors/process_detector_utils.cc +++ b/resource_detectors/process_detector_utils.cc @@ -5,11 +5,13 @@ #include #include +#include #ifdef _MSC_VER // clang-format off # include # include +# include // clang-format on #else # include @@ -105,6 +107,66 @@ std::string ExtractCommand(const std::string &command_line_path) return command; } +std::vector GetCommandWithArgs(const int32_t &pid) +{ +#ifdef _MSC_VER + int argc = 0; + LPWSTR *argvW = CommandLineToArgvW(GetCommandLineW(), &argc); + if (!argvW) + { + return {}; + } + + std::vector args; + for (int i = 0; i < argc; i++) + { + // Convert UTF-16 to UTF-8 + int size_needed = WideCharToMultiByte(CP_UTF8, 0, argvW[i], -1, NULL, 0, NULL, NULL); + if (size_needed > 0) + { + std::string arg(size_needed - 1, 0); + WideCharToMultiByte(CP_UTF8, 0, argvW[i], -1, &arg[0], size_needed, NULL, NULL); + args.push_back(arg); + } + } + + LocalFree(argvW); + return args; +#else + std::string command_line_path = FormFilePath(pid, kCmdlineName); + return ExtractCommandWithArgs(command_line_path); +#endif +} + +std::vector ExtractCommandWithArgs(const std::string &command_line_path) +{ + std::vector commands; + std::ifstream command_line_file(command_line_path, std::ios::in | std::ios::binary); + std::string command; + while (std::getline(command_line_file, command, '\0')) + { + if (!command.empty()) + { + commands.push_back(command); + } + } + return commands; +} + +std::string ConvertCommandArgsToString(const std::vector &command_args) +{ + std::string command_line; + for (const auto &arg : command_args) + { + if (!command_line.empty()) + { + command_line += " "; + } + command_line += arg; + } + return command_line; +} + std::string FormFilePath(const int32_t &pid, const char *process_type) { char buff[64]; diff --git a/resource_detectors/test/process_detector_test.cc b/resource_detectors/test/process_detector_test.cc index 374a3a61cb..45281816cb 100644 --- a/resource_detectors/test/process_detector_test.cc +++ b/resource_detectors/test/process_detector_test.cc @@ -11,6 +11,7 @@ # include # include # include +# include # define getpid _getpid // clang-format on #else @@ -47,6 +48,23 @@ TEST(ProcessDetectorUtilsTest, ExtractCommand) std::remove(filename.c_str()); // Cleanup } +TEST(processDetectorUtilsTest, ExtractCommandWithArgs) +{ + std::string filename{"test_command_args.txt"}; + + { + std::ofstream outfile(filename, std::ios::binary); + const char raw_data[] = "test_command\0arg1\0arg2\0arg3\0"; + outfile.write(raw_data, sizeof(raw_data) - 1); + } + + std::vector args = + opentelemetry::resource_detector::detail::ExtractCommandWithArgs(filename); + EXPECT_EQ(args, (std::vector{"test_command", "arg1", "arg2", "arg3"})); + + std::remove(filename.c_str()); // Cleanup +} + TEST(ProcessDetectorUtilsTest, EmptyCommandFile) { std::string filename{"empty_command.txt"}; @@ -59,6 +77,27 @@ TEST(ProcessDetectorUtilsTest, EmptyCommandFile) std::remove(filename.c_str()); // Cleanup } +TEST(ProcessDetectorUtilsTest, EmptyCommandWithArgsFile) +{ + std::string filename{"empty_command_args.txt"}; + std::ofstream outfile(filename, std::ios::binary); + outfile.close(); + + std::vector args = + opentelemetry::resource_detector::detail::ExtractCommandWithArgs(filename); + EXPECT_TRUE(args.empty()); + + std::remove(filename.c_str()); // Cleanup +} + +TEST(ProcessDetectorUtilsTest, ConvertCommandArgsToStringTest) +{ + std::vector args = {"test_command", "arg1", "arg2", "arg3"}; + std::string command_line = + opentelemetry::resource_detector::detail::ConvertCommandArgsToString(args); + EXPECT_EQ(command_line, "test_command arg1 arg2 arg3"); +} + TEST(ProcessDetectorUtilsTest, GetExecutablePathTest) { int32_t pid = getpid(); @@ -146,3 +185,38 @@ TEST(ProcessDetectorUtilsTest, GetCommandTest) std::string expected_command = opentelemetry::resource_detector::detail::GetCommand(pid); EXPECT_EQ(command, expected_command); } + +TEST(ProcessDetectorUtilsTest, GetCommandWithArgsTest) +{ + int32_t pid = getpid(); + std::vector args; +#ifdef _MSC_VER + int argc = 0; + LPWSTR *argvW = CommandLineToArgvW(GetCommandLineW(), &argc); + if (!argvW) + { + return {}; + } + + for (int i = 0; i < argc; i++) + { + // Convert UTF-16 to UTF-8 + int size_needed = WideCharToMultiByte(CP_UTF8, 0, argvW[i], -1, NULL, 0, NULL, NULL); + if (size_needed > 0) + { + std::string arg(size_needed - 1, 0); + WideCharToMultiByte(CP_UTF8, 0, argvW[i], -1, &arg[0], size_needed, NULL, NULL); + args.push_back(arg); + } + } + + LocalFree(argvW); +#else + std::string command_line_path = + opentelemetry::resource_detector::detail::FormFilePath(pid, "cmdline"); + args = opentelemetry::resource_detector::detail::ExtractCommandWithArgs(command_line_path); +#endif + std::vector expected_args = + opentelemetry::resource_detector::detail::GetCommandWithArgs(pid); + EXPECT_EQ(args, expected_args); +} From 8a9ae6b13aeab24f999773f57f647e38c89cc138 Mon Sep 17 00:00:00 2001 From: nikhilbhatia08 Date: Fri, 22 Aug 2025 17:44:07 +0530 Subject: [PATCH 2/9] changes with respect to command_args --- .../detail/process_detector_utils.h | 24 ++-------- resource_detectors/process_detector.cc | 23 +++------- resource_detectors/process_detector_utils.cc | 37 ---------------- .../test/process_detector_test.cc | 44 +++++-------------- 4 files changed, 20 insertions(+), 108 deletions(-) diff --git a/resource_detectors/include/opentelemetry/resource_detectors/detail/process_detector_utils.h b/resource_detectors/include/opentelemetry/resource_detectors/detail/process_detector_utils.h index 1780db1b2b..3c869953c5 100644 --- a/resource_detectors/include/opentelemetry/resource_detectors/detail/process_detector_utils.h +++ b/resource_detectors/include/opentelemetry/resource_detectors/detail/process_detector_utils.h @@ -32,39 +32,23 @@ std::string FormFilePath(const int32_t &pid, const char *process_type); 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//cmdline file. - * - TODO: Need to implement for Darwin - */ -std::string ExtractCommand(const std::string &command_line_path); - -/** - * Extracts the command-line arguments from the command line path. + * Extracts the command-line arguments and the command. * Platform-specific behavior: * - Windows: Uses CommandLineToArgvW() to parse the command line. - * - Linux/Unix: Reads the /proc//cmdline file and splits it into arguments. + * - Linux/Unix: Reads the /proc//cmdline file and splits it into command and arguments. * - TODO: Need to implement for Darwin */ std::vector ExtractCommandWithArgs(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); - -/** - * Retrieves the command-line arguments used to launch the process for a given PID. + * Retrieves the command-line arguments and the command used to launch the process for a given PID. * This function is a wrapper around ExtractCommandWithArgs() and is provided for convenience and * testability of ExtractCommandWithArgs(). */ std::vector GetCommandWithArgs(const int32_t &pid); /** - * Converts a vector of command-line arguments into a single command-line string. + * Converts a vector of command-line arguments and command into a single command-line string. * process.command_line is the string representation of the command line that we collected using * GetCommandWithArgs(). */ diff --git a/resource_detectors/process_detector.cc b/resource_detectors/process_detector.cc index 8f08a46ddc..d6e40e065b 100644 --- a/resource_detectors/process_detector.cc +++ b/resource_detectors/process_detector.cc @@ -50,28 +50,15 @@ opentelemetry::sdk::resource::Resource ProcessResourceDetector::Detect() noexcep 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()); - } - - try - { - std::vector args = + std::vector command_with_args = opentelemetry::resource_detector::detail::GetCommandWithArgs(pid); - if (!args.empty()) + if (!command_with_args.empty()) { std::string commandline_args = - opentelemetry::resource_detector::detail::ConvertCommandArgsToString(args); + opentelemetry::resource_detector::detail::ConvertCommandArgsToString(command_with_args); attributes[semconv::process::kProcessCommandLine] = std::move(commandline_args); - attributes[semconv::process::kProcessCommandArgs] = std::move(args); + attributes[semconv::process::kProcessCommand] = command_with_args[0]; + attributes[semconv::process::kProcessCommandArgs] = std::move(command_with_args); } } catch (const std::exception &ex) diff --git a/resource_detectors/process_detector_utils.cc b/resource_detectors/process_detector_utils.cc index 3dd732c786..3c9ac968a7 100644 --- a/resource_detectors/process_detector_utils.cc +++ b/resource_detectors/process_detector_utils.cc @@ -70,43 +70,6 @@ std::string GetExecutablePath(const int32_t &pid) #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) - { - 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) - { - 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); - - 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::vector GetCommandWithArgs(const int32_t &pid) { #ifdef _MSC_VER diff --git a/resource_detectors/test/process_detector_test.cc b/resource_detectors/test/process_detector_test.cc index 45281816cb..666c1c4633 100644 --- a/resource_detectors/test/process_detector_test.cc +++ b/resource_detectors/test/process_detector_test.cc @@ -32,23 +32,7 @@ TEST(ProcessDetectorUtilsTest, FormFilePath) EXPECT_EQ(exe_path, "/proc/1234/exe"); } -TEST(ProcessDetectorUtilsTest, ExtractCommand) -{ - std::string filename{"test_command.txt"}; - - { - std::ofstream outfile(filename, std::ios::binary); - const char raw_data[] = "test_command\0arg1\0arg2\0arg3\0"; - outfile.write(raw_data, sizeof(raw_data) - 1); - } - - std::string command = opentelemetry::resource_detector::detail::ExtractCommand(filename); - EXPECT_EQ(command, std::string{"test_command"}); - - std::remove(filename.c_str()); // Cleanup -} - -TEST(processDetectorUtilsTest, ExtractCommandWithArgs) +TEST(ProcessDetectorUtilsTest, ExtractCommandWithArgs) { std::string filename{"test_command_args.txt"}; @@ -65,18 +49,6 @@ TEST(processDetectorUtilsTest, ExtractCommandWithArgs) std::remove(filename.c_str()); // Cleanup } -TEST(ProcessDetectorUtilsTest, EmptyCommandFile) -{ - std::string filename{"empty_command.txt"}; - std::ofstream outfile(filename, std::ios::binary); - outfile.close(); - - std::string command = opentelemetry::resource_detector::detail::ExtractCommand(filename); - EXPECT_EQ(command, std::string{""}); - - std::remove(filename.c_str()); // Cleanup -} - TEST(ProcessDetectorUtilsTest, EmptyCommandWithArgsFile) { std::string filename{"empty_command_args.txt"}; @@ -148,7 +120,7 @@ TEST(ProcessDetectorUtilsTest, GetExecutablePathTest) EXPECT_EQ(path, expected_path); } -TEST(ProcessDetectorUtilsTest, GetCommandTest) +TEST(ProcessDetectorUtilsTest, CommandTest) { int32_t pid = getpid(); std::string command; @@ -177,12 +149,18 @@ TEST(ProcessDetectorUtilsTest, GetCommandTest) } } #else - // This is the path to get the command that was used to start the process std::string command_line_path = opentelemetry::resource_detector::detail::FormFilePath(pid, "cmdline"); - command = opentelemetry::resource_detector::detail::ExtractCommand(command_line_path); + std::ifstream command_line_file(command_line_path, std::ios::in | std::ios::binary); + std::getline(command_line_file, command, '\0'); #endif - std::string expected_command = opentelemetry::resource_detector::detail::GetCommand(pid); + std::vector expected_command_with_args = + opentelemetry::resource_detector::detail::GetCommandWithArgs(pid); + std::string expected_command; + if (!expected_command_with_args.empty()) + { + expected_command = expected_command_with_args[0]; + } EXPECT_EQ(command, expected_command); } From a915a3dd62b7e7a355d436045b5c9ce316889b3c Mon Sep 17 00:00:00 2001 From: nikhilbhatia08 Date: Fri, 22 Aug 2025 22:23:58 +0530 Subject: [PATCH 3/9] minor error fixes --- .../detail/process_detector_utils.h | 1 + resource_detectors/process_detector_utils.cc | 30 +++++++++---------- .../test/process_detector_test.cc | 2 +- 3 files changed, 17 insertions(+), 16 deletions(-) diff --git a/resource_detectors/include/opentelemetry/resource_detectors/detail/process_detector_utils.h b/resource_detectors/include/opentelemetry/resource_detectors/detail/process_detector_utils.h index 3c869953c5..677a7554e9 100644 --- a/resource_detectors/include/opentelemetry/resource_detectors/detail/process_detector_utils.h +++ b/resource_detectors/include/opentelemetry/resource_detectors/detail/process_detector_utils.h @@ -5,6 +5,7 @@ #include #include +#include #include "opentelemetry/version.h" diff --git a/resource_detectors/process_detector_utils.cc b/resource_detectors/process_detector_utils.cc index 3c9ac968a7..949e180553 100644 --- a/resource_detectors/process_detector_utils.cc +++ b/resource_detectors/process_detector_utils.cc @@ -70,6 +70,21 @@ std::string GetExecutablePath(const int32_t &pid) #endif } +std::vector ExtractCommandWithArgs(const std::string &command_line_path) +{ + std::vector commands; + std::ifstream command_line_file(command_line_path, std::ios::in | std::ios::binary); + std::string command; + while (std::getline(command_line_file, command, '\0')) + { + if (!command.empty()) + { + commands.push_back(command); + } + } + return commands; +} + std::vector GetCommandWithArgs(const int32_t &pid) { #ifdef _MSC_VER @@ -101,21 +116,6 @@ std::vector GetCommandWithArgs(const int32_t &pid) #endif } -std::vector ExtractCommandWithArgs(const std::string &command_line_path) -{ - std::vector commands; - std::ifstream command_line_file(command_line_path, std::ios::in | std::ios::binary); - std::string command; - while (std::getline(command_line_file, command, '\0')) - { - if (!command.empty()) - { - commands.push_back(command); - } - } - return commands; -} - std::string ConvertCommandArgsToString(const std::vector &command_args) { std::string command_line; diff --git a/resource_detectors/test/process_detector_test.cc b/resource_detectors/test/process_detector_test.cc index 666c1c4633..7aca0f6b08 100644 --- a/resource_detectors/test/process_detector_test.cc +++ b/resource_detectors/test/process_detector_test.cc @@ -173,7 +173,7 @@ TEST(ProcessDetectorUtilsTest, GetCommandWithArgsTest) LPWSTR *argvW = CommandLineToArgvW(GetCommandLineW(), &argc); if (!argvW) { - return {}; + args = {}; } for (int i = 0; i < argc; i++) From 07cd812cfaf202fb6bf673f2cd0b585f1d60c02a Mon Sep 17 00:00:00 2001 From: nikhilbhatia08 Date: Sun, 24 Aug 2025 23:14:01 +0530 Subject: [PATCH 4/9] command test fix --- resource_detectors/process_detector.cc | 1 + .../test/process_detector_test.cc | 34 +++++++++---------- 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/resource_detectors/process_detector.cc b/resource_detectors/process_detector.cc index d6e40e065b..c9e5c535fa 100644 --- a/resource_detectors/process_detector.cc +++ b/resource_detectors/process_detector.cc @@ -16,6 +16,7 @@ #include #include #include +#include #ifdef _MSC_VER # include diff --git a/resource_detectors/test/process_detector_test.cc b/resource_detectors/test/process_detector_test.cc index 7aca0f6b08..95f8c0b364 100644 --- a/resource_detectors/test/process_detector_test.cc +++ b/resource_detectors/test/process_detector_test.cc @@ -5,6 +5,7 @@ #include #include #include +#include #ifdef _MSC_VER // clang-format off @@ -12,6 +13,7 @@ # include # include # include +# pragma comment(lib, "shell32.lib") # define getpid _getpid // clang-format on #else @@ -125,28 +127,24 @@ TEST(ProcessDetectorUtilsTest, CommandTest) int32_t pid = getpid(); std::string command; #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) + int argc = 0; + LPWSTR *argvW = CommandLineToArgvW(GetCommandLineW(), &argc); + + if (argvW && argc > 0) { - command = std::string(); + int size_needed = WideCharToMultiByte(CP_UTF8, 0, argvW[0], -1, NULL, 0, NULL, NULL); + if (size_needed > 0) + { + std::string arg(size_needed - 1, 0); + WideCharToMultiByte(CP_UTF8, 0, argvW[0], -1, &arg[0], size_needed, NULL, NULL); + command = arg; + } + + LocalFree(argvW); } else { - - // Convert UTF-16 to UTF-8 - int size_needed = WideCharToMultiByte(CP_UTF8, 0, wcmd, -1, NULL, 0, NULL, NULL); - if (size_needed <= 0) - { - command = std::string(); - } - else - { - std::string utf8_command(size_needed - 1, 0); // exclude null terminator - WideCharToMultiByte(CP_UTF8, 0, wcmd, -1, &utf8_command[0], size_needed, NULL, NULL); - command = utf8_command; - } + command = std::string(); } #else std::string command_line_path = From f47f3ebf29b60aa9216d7b0654a52d25ee0e4927 Mon Sep 17 00:00:00 2001 From: nikhilbhatia08 Date: Fri, 29 Aug 2025 22:46:58 +0530 Subject: [PATCH 5/9] nit changes --- resource_detectors/process_detector_utils.cc | 3 ++- .../test/process_detector_test.cc | 20 +++++++++---------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/resource_detectors/process_detector_utils.cc b/resource_detectors/process_detector_utils.cc index 949e180553..39dcb14949 100644 --- a/resource_detectors/process_detector_utils.cc +++ b/resource_detectors/process_detector_utils.cc @@ -12,6 +12,7 @@ # include # include # include +# pragma comment(lib, "shell32.lib") // clang-format on #else # include @@ -92,7 +93,7 @@ std::vector GetCommandWithArgs(const int32_t &pid) LPWSTR *argvW = CommandLineToArgvW(GetCommandLineW(), &argc); if (!argvW) { - return {}; + return {}; // returns an empty vector if CommandLineToArgvW fails } std::vector args; diff --git a/resource_detectors/test/process_detector_test.cc b/resource_detectors/test/process_detector_test.cc index 95f8c0b364..9d5ca6f16e 100644 --- a/resource_detectors/test/process_detector_test.cc +++ b/resource_detectors/test/process_detector_test.cc @@ -12,8 +12,6 @@ # include # include # include -# include -# pragma comment(lib, "shell32.lib") # define getpid _getpid // clang-format on #else @@ -173,16 +171,18 @@ TEST(ProcessDetectorUtilsTest, GetCommandWithArgsTest) { args = {}; } - - for (int i = 0; i < argc; i++) + else { - // Convert UTF-16 to UTF-8 - int size_needed = WideCharToMultiByte(CP_UTF8, 0, argvW[i], -1, NULL, 0, NULL, NULL); - if (size_needed > 0) + for (int i = 0; i < argc; i++) { - std::string arg(size_needed - 1, 0); - WideCharToMultiByte(CP_UTF8, 0, argvW[i], -1, &arg[0], size_needed, NULL, NULL); - args.push_back(arg); + // Convert UTF-16 to UTF-8 + int size_needed = WideCharToMultiByte(CP_UTF8, 0, argvW[i], -1, NULL, 0, NULL, NULL); + if (size_needed > 0) + { + std::string arg(size_needed - 1, 0); + WideCharToMultiByte(CP_UTF8, 0, argvW[i], -1, &arg[0], size_needed, NULL, NULL); + args.push_back(arg); + } } } From 9f6dfd68d44cf8e2361f798aa5f6ab78a20cea88 Mon Sep 17 00:00:00 2001 From: nikhilbhatia08 Date: Sun, 31 Aug 2025 20:53:10 +0530 Subject: [PATCH 6/9] unicode and non-unicode --- resource_detectors/process_detector_utils.cc | 29 ++++++++++++++++---- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/resource_detectors/process_detector_utils.cc b/resource_detectors/process_detector_utils.cc index 39dcb14949..130327d09c 100644 --- a/resource_detectors/process_detector_utils.cc +++ b/resource_detectors/process_detector_utils.cc @@ -89,17 +89,20 @@ std::vector ExtractCommandWithArgs(const std::string &command_line_ std::vector GetCommandWithArgs(const int32_t &pid) { #ifdef _MSC_VER - int argc = 0; - LPWSTR *argvW = CommandLineToArgvW(GetCommandLineW(), &argc); + int argc = 0; + LPTSTR cmdLine = GetCommandLine(); + +# ifdef UNICODE + // for UNICODE + LPWSTR *argvW = CommandLineToArgvW(cmdLine, &argc); if (!argvW) { - return {}; // returns an empty vector if CommandLineToArgvW fails + return {}; } std::vector args; for (int i = 0; i < argc; i++) { - // Convert UTF-16 to UTF-8 int size_needed = WideCharToMultiByte(CP_UTF8, 0, argvW[i], -1, NULL, 0, NULL, NULL); if (size_needed > 0) { @@ -111,7 +114,23 @@ std::vector GetCommandWithArgs(const int32_t &pid) LocalFree(argvW); return args; -#else +# else + // for Non-UNICODE + LPSTR *argvA = CommandLineToArgvA(cmdLine, &argc); + if (!argvA) + { + return {}; + } + + std::vector args; + for (int i = 0; i < argc; i++) + { + args.push_back(argvA[i]); + } + + LocalFree(argvA); + return args; +# endif // endif for UNICODE std::string command_line_path = FormFilePath(pid, kCmdlineName); return ExtractCommandWithArgs(command_line_path); #endif From 57d3dd6e6cf4d46cab2b8d23d3adc2024748fc0f Mon Sep 17 00:00:00 2001 From: nikhilbhatia08 Date: Sun, 31 Aug 2025 21:10:50 +0530 Subject: [PATCH 7/9] changed back to W --- resource_detectors/process_detector_utils.cc | 29 ++++---------------- 1 file changed, 5 insertions(+), 24 deletions(-) diff --git a/resource_detectors/process_detector_utils.cc b/resource_detectors/process_detector_utils.cc index 130327d09c..39dcb14949 100644 --- a/resource_detectors/process_detector_utils.cc +++ b/resource_detectors/process_detector_utils.cc @@ -89,20 +89,17 @@ std::vector ExtractCommandWithArgs(const std::string &command_line_ std::vector GetCommandWithArgs(const int32_t &pid) { #ifdef _MSC_VER - int argc = 0; - LPTSTR cmdLine = GetCommandLine(); - -# ifdef UNICODE - // for UNICODE - LPWSTR *argvW = CommandLineToArgvW(cmdLine, &argc); + int argc = 0; + LPWSTR *argvW = CommandLineToArgvW(GetCommandLineW(), &argc); if (!argvW) { - return {}; + return {}; // returns an empty vector if CommandLineToArgvW fails } std::vector args; for (int i = 0; i < argc; i++) { + // Convert UTF-16 to UTF-8 int size_needed = WideCharToMultiByte(CP_UTF8, 0, argvW[i], -1, NULL, 0, NULL, NULL); if (size_needed > 0) { @@ -114,23 +111,7 @@ std::vector GetCommandWithArgs(const int32_t &pid) LocalFree(argvW); return args; -# else - // for Non-UNICODE - LPSTR *argvA = CommandLineToArgvA(cmdLine, &argc); - if (!argvA) - { - return {}; - } - - std::vector args; - for (int i = 0; i < argc; i++) - { - args.push_back(argvA[i]); - } - - LocalFree(argvA); - return args; -# endif // endif for UNICODE +#else std::string command_line_path = FormFilePath(pid, kCmdlineName); return ExtractCommandWithArgs(command_line_path); #endif From a7fb3e4c26728185df47758cf90eaebeedb72c9a Mon Sep 17 00:00:00 2001 From: nikhilbhatia08 Date: Sun, 7 Sep 2025 01:27:18 +0530 Subject: [PATCH 8/9] comment and removal for command args --- .../detail/process_detector_utils.h | 7 ------- resource_detectors/process_detector.cc | 8 +++----- resource_detectors/process_detector_utils.cc | 14 -------------- 3 files changed, 3 insertions(+), 26 deletions(-) diff --git a/resource_detectors/include/opentelemetry/resource_detectors/detail/process_detector_utils.h b/resource_detectors/include/opentelemetry/resource_detectors/detail/process_detector_utils.h index 677a7554e9..06b2d8e329 100644 --- a/resource_detectors/include/opentelemetry/resource_detectors/detail/process_detector_utils.h +++ b/resource_detectors/include/opentelemetry/resource_detectors/detail/process_detector_utils.h @@ -48,13 +48,6 @@ std::vector ExtractCommandWithArgs(const std::string &command_line_ */ std::vector GetCommandWithArgs(const int32_t &pid); -/** - * Converts a vector of command-line arguments and command into a single command-line string. - * process.command_line is the string representation of the command line that we collected using - * GetCommandWithArgs(). - */ -std::string ConvertCommandArgsToString(const std::vector &command_args); - } // namespace detail } // namespace resource_detector OPENTELEMETRY_END_NAMESPACE diff --git a/resource_detectors/process_detector.cc b/resource_detectors/process_detector.cc index c9e5c535fa..217ecc7c6c 100644 --- a/resource_detectors/process_detector.cc +++ b/resource_detectors/process_detector.cc @@ -55,11 +55,9 @@ opentelemetry::sdk::resource::Resource ProcessResourceDetector::Detect() noexcep opentelemetry::resource_detector::detail::GetCommandWithArgs(pid); if (!command_with_args.empty()) { - std::string commandline_args = - opentelemetry::resource_detector::detail::ConvertCommandArgsToString(command_with_args); - attributes[semconv::process::kProcessCommandLine] = std::move(commandline_args); - attributes[semconv::process::kProcessCommand] = command_with_args[0]; - attributes[semconv::process::kProcessCommandArgs] = std::move(command_with_args); + // Commented until they are properly sanitized + // attributes[semconv::process::kProcessCommand] = command_with_args[0]; + // attributes[semconv::process::kProcessCommandArgs] = std::move(command_with_args); } } catch (const std::exception &ex) diff --git a/resource_detectors/process_detector_utils.cc b/resource_detectors/process_detector_utils.cc index 39dcb14949..9da40a52a8 100644 --- a/resource_detectors/process_detector_utils.cc +++ b/resource_detectors/process_detector_utils.cc @@ -117,20 +117,6 @@ std::vector GetCommandWithArgs(const int32_t &pid) #endif } -std::string ConvertCommandArgsToString(const std::vector &command_args) -{ - std::string command_line; - for (const auto &arg : command_args) - { - if (!command_line.empty()) - { - command_line += " "; - } - command_line += arg; - } - return command_line; -} - std::string FormFilePath(const int32_t &pid, const char *process_type) { char buff[64]; From 7b7ba303be295e546ded286479afe09c675a7697 Mon Sep 17 00:00:00 2001 From: nikhilbhatia08 Date: Sun, 7 Sep 2025 01:32:09 +0530 Subject: [PATCH 9/9] fix for test --- resource_detectors/test/process_detector_test.cc | 8 -------- 1 file changed, 8 deletions(-) diff --git a/resource_detectors/test/process_detector_test.cc b/resource_detectors/test/process_detector_test.cc index 9d5ca6f16e..8985958f77 100644 --- a/resource_detectors/test/process_detector_test.cc +++ b/resource_detectors/test/process_detector_test.cc @@ -62,14 +62,6 @@ TEST(ProcessDetectorUtilsTest, EmptyCommandWithArgsFile) std::remove(filename.c_str()); // Cleanup } -TEST(ProcessDetectorUtilsTest, ConvertCommandArgsToStringTest) -{ - std::vector args = {"test_command", "arg1", "arg2", "arg3"}; - std::string command_line = - opentelemetry::resource_detector::detail::ConvertCommandArgsToString(args); - EXPECT_EQ(command_line, "test_command arg1 arg2 arg3"); -} - TEST(ProcessDetectorUtilsTest, GetExecutablePathTest) { int32_t pid = getpid();