Skip to content

Commit

Permalink
support for OTEL_SERVICE_NAME
Browse files Browse the repository at this point in the history
OTEL_SERVICE_NAME overrides OTEL_RESOURCE_ATTRIBUTES when it is defined.
  • Loading branch information
willie committed Mar 5, 2024
1 parent c82306f commit 00be6ee
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 12 deletions.
36 changes: 24 additions & 12 deletions sdk/src/resource/resource_detector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,40 @@ namespace resource
{

const char *OTEL_RESOURCE_ATTRIBUTES = "OTEL_RESOURCE_ATTRIBUTES";
const char *OTEL_SERVICE_NAME = "OTEL_SERVICE_NAME";

Resource OTELResourceDetector::Detect() noexcept
{
std::string attributes_str;
bool exists;
std::string attributes_str, service_name;

exists = opentelemetry::sdk::common::GetStringEnvironmentVariable(OTEL_RESOURCE_ATTRIBUTES,
attributes_str);
if (!exists)
bool attributes_exists = opentelemetry::sdk::common::GetStringEnvironmentVariable(OTEL_RESOURCE_ATTRIBUTES, attributes_str);
bool service_name_exists = opentelemetry::sdk::common::GetStringEnvironmentVariable(OTEL_SERVICE_NAME, service_name);

if (!attributes_exists && !service_name_exists)
{
return Resource();
}

ResourceAttributes attributes;
std::istringstream iss(attributes_str);
std::string token;
while (std::getline(iss, token, ','))

if (attributes_exists)
{
size_t pos = token.find('=');
std::string key = token.substr(0, pos);
std::string value = token.substr(pos + 1);
attributes[key] = value;
std::istringstream iss(attributes_str);
std::string token;
while (std::getline(iss, token, ','))
{
size_t pos = token.find('=');
std::string key = token.substr(0, pos);
std::string value = token.substr(pos + 1);
attributes[key] = value;
}
}

if (service_name_exists)
{
attributes["service.name"] = service_name;
}

return Resource(attributes);
}

Expand Down
24 changes: 24 additions & 0 deletions sdk/test/resource/resource_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,30 @@ TEST(ResourceTest, OtelResourceDetector)
unsetenv("OTEL_RESOURCE_ATTRIBUTES");
}

TEST(ResourceTest, OtelResourceDetectorServiceNameOverride)
{
std::map<std::string, std::string> expected_attributes = {{"service.name", "new_name"}};

setenv("OTEL_RESOURCE_ATTRIBUTES", "service.name=old_name", 1);
setenv("OTEL_SERVICE_NAME", "new_name", 1);

OTELResourceDetector detector;
auto resource = detector.Detect();
auto received_attributes = resource.GetAttributes();
for (auto &e : received_attributes)
{
EXPECT_TRUE(expected_attributes.find(e.first) != expected_attributes.end());
if (expected_attributes.find(e.first) != expected_attributes.end())
{
EXPECT_EQ(expected_attributes.find(e.first)->second, nostd::get<std::string>(e.second));
}
}
EXPECT_EQ(received_attributes.size(), expected_attributes.size());

unsetenv("OTEL_SERVICE_NAME");
unsetenv("OTEL_RESOURCE_ATTRIBUTES");
}

TEST(ResourceTest, OtelResourceDetectorEmptyEnv)
{
std::map<std::string, std::string> expected_attributes = {};
Expand Down

0 comments on commit 00be6ee

Please sign in to comment.