Skip to content

Commit c08b825

Browse files
committed
Guard process executable service name fallback
1 parent 7ee11fd commit c08b825

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

‎sdk/src/resource/resource.cc‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,12 @@ Resource Resource::Create(const ResourceAttributes &attributes,
374374
resource_attributes.find(semconv::process::kProcessExecutableName);
375375
if (it_process_executable_name != resource_attributes.end())
376376
{
377-
default_service_name += ":" + nostd::get<std::string>(it_process_executable_name->second);
377+
const auto *process_executable_name =
378+
nostd::get_if<std::string>(&it_process_executable_name->second);
379+
if (process_executable_name != nullptr)
380+
{
381+
default_service_name += ":" + *process_executable_name;
382+
}
378383
}
379384
resource.unassociated_attributes_[semconv::service::kServiceName] = default_service_name;
380385
if (!resource.entities_.empty())

‎sdk/test/resource/resource_test.cc‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,15 @@ TEST(ResourceTest, CreateServiceNameFallbackUsesProcessExecutableName)
433433
"unknown_service:otel-test");
434434
}
435435

436+
TEST(ResourceTest, CreateNonStringProcessExecutableNameFallsBackToUnknownService)
437+
{
438+
ResourceAttributes attributes = {{semconv::process::kProcessExecutableName, true}};
439+
auto resource = Resource::Create(attributes);
440+
441+
EXPECT_EQ(nostd::get<std::string>(resource.GetAttributes().at(semconv::service::kServiceName)),
442+
"unknown_service");
443+
}
444+
436445
TEST(ResourceTest, MergeUnassociatedMatchesFlattened)
437446
{
438447
TestResource resource1(ResourceAttributes({{"service", "backend"}}));

0 commit comments

Comments
 (0)