Skip to content

Commit 00be6ee

Browse files
committed
support for OTEL_SERVICE_NAME
OTEL_SERVICE_NAME overrides OTEL_RESOURCE_ATTRIBUTES when it is defined.
1 parent c82306f commit 00be6ee

File tree

2 files changed

+48
-12
lines changed

2 files changed

+48
-12
lines changed

sdk/src/resource/resource_detector.cc

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,40 @@ namespace resource
1515
{
1616

1717
const char *OTEL_RESOURCE_ATTRIBUTES = "OTEL_RESOURCE_ATTRIBUTES";
18+
const char *OTEL_SERVICE_NAME = "OTEL_SERVICE_NAME";
1819

1920
Resource OTELResourceDetector::Detect() noexcept
2021
{
21-
std::string attributes_str;
22-
bool exists;
22+
std::string attributes_str, service_name;
2323

24-
exists = opentelemetry::sdk::common::GetStringEnvironmentVariable(OTEL_RESOURCE_ATTRIBUTES,
25-
attributes_str);
26-
if (!exists)
24+
bool attributes_exists = opentelemetry::sdk::common::GetStringEnvironmentVariable(OTEL_RESOURCE_ATTRIBUTES, attributes_str);
25+
bool service_name_exists = opentelemetry::sdk::common::GetStringEnvironmentVariable(OTEL_SERVICE_NAME, service_name);
26+
27+
if (!attributes_exists && !service_name_exists)
2728
{
2829
return Resource();
2930
}
31+
3032
ResourceAttributes attributes;
31-
std::istringstream iss(attributes_str);
32-
std::string token;
33-
while (std::getline(iss, token, ','))
33+
34+
if (attributes_exists)
3435
{
35-
size_t pos = token.find('=');
36-
std::string key = token.substr(0, pos);
37-
std::string value = token.substr(pos + 1);
38-
attributes[key] = value;
36+
std::istringstream iss(attributes_str);
37+
std::string token;
38+
while (std::getline(iss, token, ','))
39+
{
40+
size_t pos = token.find('=');
41+
std::string key = token.substr(0, pos);
42+
std::string value = token.substr(pos + 1);
43+
attributes[key] = value;
44+
}
3945
}
46+
47+
if (service_name_exists)
48+
{
49+
attributes["service.name"] = service_name;
50+
}
51+
4052
return Resource(attributes);
4153
}
4254

sdk/test/resource/resource_test.cc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,30 @@ TEST(ResourceTest, OtelResourceDetector)
220220
unsetenv("OTEL_RESOURCE_ATTRIBUTES");
221221
}
222222

223+
TEST(ResourceTest, OtelResourceDetectorServiceNameOverride)
224+
{
225+
std::map<std::string, std::string> expected_attributes = {{"service.name", "new_name"}};
226+
227+
setenv("OTEL_RESOURCE_ATTRIBUTES", "service.name=old_name", 1);
228+
setenv("OTEL_SERVICE_NAME", "new_name", 1);
229+
230+
OTELResourceDetector detector;
231+
auto resource = detector.Detect();
232+
auto received_attributes = resource.GetAttributes();
233+
for (auto &e : received_attributes)
234+
{
235+
EXPECT_TRUE(expected_attributes.find(e.first) != expected_attributes.end());
236+
if (expected_attributes.find(e.first) != expected_attributes.end())
237+
{
238+
EXPECT_EQ(expected_attributes.find(e.first)->second, nostd::get<std::string>(e.second));
239+
}
240+
}
241+
EXPECT_EQ(received_attributes.size(), expected_attributes.size());
242+
243+
unsetenv("OTEL_SERVICE_NAME");
244+
unsetenv("OTEL_RESOURCE_ATTRIBUTES");
245+
}
246+
223247
TEST(ResourceTest, OtelResourceDetectorEmptyEnv)
224248
{
225249
std::map<std::string, std::string> expected_attributes = {};

0 commit comments

Comments
 (0)