From e88afb7b5a4cf53dc0ee63e35023533995509e66 Mon Sep 17 00:00:00 2001 From: ryux1 Date: Tue, 8 Sep 2026 06:41:44 +0200 Subject: [PATCH] Fix Resource creation for invalid executable names --- CHANGELOG.md | 4 ++++ sdk/src/resource/resource.cc | 7 ++++++- sdk/test/resource/resource_test.cc | 26 ++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 44bfbcf494..28f00ff696 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,10 @@ Increment the: ## [Unreleased] +* [SDK] Avoid throwing from `Resource::Create` when `process.executable.name` + has a non-string value and `service.name` is not set. + [#4535](https://github.com/open-telemetry/opentelemetry-cpp/issues/4535) + * [DOC] Fix and clarify the `StartSpanOptions` documentation [#4526](https://github.com/open-telemetry/opentelemetry-cpp/pull/4526) diff --git a/sdk/src/resource/resource.cc b/sdk/src/resource/resource.cc index 95b41498f8..9fdecbff74 100644 --- a/sdk/src/resource/resource.cc +++ b/sdk/src/resource/resource.cc @@ -51,7 +51,12 @@ Resource Resource::Create(const ResourceAttributes &attributes, const std::strin resource.attributes_.find(semconv::process::kProcessExecutableName); if (it_process_executable_name != resource.attributes_.end()) { - default_service_name += ":" + nostd::get(it_process_executable_name->second); + const auto *process_executable_name = + nostd::get_if(&it_process_executable_name->second); + if (process_executable_name != nullptr) + { + default_service_name += ":" + *process_executable_name; + } } resource.attributes_[semconv::service::kServiceName] = default_service_name; } diff --git a/sdk/test/resource/resource_test.cc b/sdk/test/resource/resource_test.cc index 4a80ca2a7a..29a716a8db 100644 --- a/sdk/test/resource/resource_test.cc +++ b/sdk/test/resource/resource_test.cc @@ -14,6 +14,7 @@ #include "opentelemetry/sdk/resource/resource.h" #include "opentelemetry/sdk/resource/resource_detector.h" #include "opentelemetry/sdk/version/version.h" +#include "opentelemetry/semconv/incubating/process_attributes.h" #include "opentelemetry/semconv/service_attributes.h" #include "opentelemetry/semconv/telemetry_attributes.h" @@ -84,6 +85,31 @@ TEST(ResourceTest, create_without_servicename) EXPECT_EQ(received_attributes.size(), expected_attributes.size()); // for missing service.name } +TEST(ResourceTest, create_with_non_string_process_executable_name) +{ + ResourceAttributes attributes = {{semconv::process::kProcessExecutableName, true}}; + + auto resource = Resource::Create(attributes); + const auto &received_attributes = resource.GetAttributes(); + auto service_name = received_attributes.find(semconv::service::kServiceName); + + ASSERT_NE(service_name, received_attributes.end()); + EXPECT_EQ(nostd::get(service_name->second), "unknown_service"); +} + +TEST(ResourceTest, create_with_process_executable_name) +{ + ResourceAttributes attributes = { + {semconv::process::kProcessExecutableName, std::string{"otel-service"}}}; + + auto resource = Resource::Create(attributes); + const auto &received_attributes = resource.GetAttributes(); + auto service_name = received_attributes.find(semconv::service::kServiceName); + + ASSERT_NE(service_name, received_attributes.end()); + EXPECT_EQ(nostd::get(service_name->second), "unknown_service:otel-service"); +} + TEST(ResourceTest, create_with_servicename) { ResourceAttributes expected_attributes = {