Skip to content

Commit

Permalink
[SDK] Support for OTEL_SERVICE_NAME (open-telemetry#2577)
Browse files Browse the repository at this point in the history
  • Loading branch information
willie authored Mar 6, 2024
1 parent c82306f commit 14f7542
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 12 deletions.
42 changes: 30 additions & 12 deletions sdk/src/resource/resource_detector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "opentelemetry/sdk/resource/resource_detector.h"
#include "opentelemetry/sdk/common/env_variables.h"
#include "opentelemetry/sdk/resource/resource.h"
#include "opentelemetry/sdk/resource/semantic_conventions.h"

#include <sstream>
#include <string>
Expand All @@ -15,28 +16,45 @@ 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('=');
if (pos != std::string::npos)
{
std::string key = token.substr(0, pos);
std::string value = token.substr(pos + 1);
attributes[key] = value;
}
}
}

if (service_name_exists)
{
attributes[SemanticConventions::kServiceName] = 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 14f7542

Please sign in to comment.