Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SDK] Support for OTEL_SERVICE_NAME #2577

Merged
merged 3 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
38 changes: 26 additions & 12 deletions sdk/src/resource/resource_detector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,42 @@ 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;
}
willie marked this conversation as resolved.
Show resolved Hide resolved
}

if (service_name_exists)
{
attributes["service.name"] = service_name;
willie marked this conversation as resolved.
Show resolved Hide resolved
}

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