Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ Increment the:
* [API] Remove regex from trace_state.h
[#4570](https://github.com/open-telemetry/opentelemetry-cpp/pull/4570)

* [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)
* [SDK] Add a missing `<cstdint>` include to `predicate_factory.h`, which
uses `uint8_t` without including the header that declares it. This relied
on a transitive include from elsewhere in the translation unit and failed
Expand Down
7 changes: 6 additions & 1 deletion sdk/src/resource/resource.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string>(it_process_executable_name->second);
const auto *process_executable_name =
nostd::get_if<std::string>(&it_process_executable_name->second);
if (process_executable_name != nullptr)
{
default_service_name += ":" + *process_executable_name;
}
}
resource.attributes_[semconv::service::kServiceName] = default_service_name;
}
Expand Down
26 changes: 26 additions & 0 deletions sdk/test/resource/resource_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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<std::string>(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<std::string>(service_name->second), "unknown_service:otel-service");
}

TEST(ResourceTest, create_with_servicename)
{
ResourceAttributes expected_attributes = {
Expand Down
Loading