Skip to content

Commit

Permalink
[SDK] Support for OTEL_SERVICE_NAME (#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

1 comment on commit 14f7542

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'OpenTelemetry-cpp sdk Benchmark'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 2.

Benchmark suite Current: 14f7542 Previous: c82306f Ratio
BM_SumAggregation 8858256.86984592 ns/iter 4410833.120346069 ns/iter 2.01
BM_BaselineBuffer/4 15688900.9475708 ns/iter 6132779.121398926 ns/iter 2.56

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.