Skip to content

Commit 44c7014

Browse files
committed
more tests + simplify a bit
1 parent 3e2aeb8 commit 44c7014

File tree

2 files changed

+41
-13
lines changed

2 files changed

+41
-13
lines changed

custom/src/main/java/co/elastic/otel/dynamicconfig/CentralConfig.java

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -117,26 +117,22 @@ static String getServiceName(ConfigProperties properties) {
117117
return serviceName;
118118
}
119119
Map<String, String> resourceMap = properties.getMap("otel.resource.attributes");
120-
if (resourceMap != null) {
121-
serviceName = resourceMap.get("service.name");
122-
if (serviceName != null) {
123-
return serviceName;
124-
}
120+
serviceName = resourceMap.get("service.name");
121+
if (serviceName != null) {
122+
return serviceName;
125123
}
126124
return "unknown_service:java"; // Specified default
127125
}
128126

127+
// package private for testing
129128
@Nullable
130-
private static String getServiceEnvironment(ConfigProperties properties) {
129+
static String getServiceEnvironment(ConfigProperties properties) {
131130
Map<String, String> resourceMap = properties.getMap("otel.resource.attributes");
132-
if (resourceMap != null) {
133-
String environment = resourceMap.get("deployment.environment.name"); // semconv
134-
if (environment != null) {
135-
return environment;
136-
}
137-
return resourceMap.get("deployment.environment"); // backward compatible, can be null
131+
String environment = resourceMap.get("deployment.environment.name"); // semconv
132+
if (environment != null) {
133+
return environment;
138134
}
139-
return null;
135+
return resourceMap.get("deployment.environment"); // backward compatible, can be null
140136
}
141137

142138
public static class Configs {

custom/src/test/java/co/elastic/otel/dynamicconfig/CentralConfigTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,14 @@ void getServiceName() {
7373
map.put("otel.service.name", "my-service-3");
7474
map.put("otel.resource.attributes", "service.name=my-service-4");
7575
testServiceName(map, "my-service-3", "service name takes precedence over resource attributes");
76+
77+
map.clear();
78+
map.put("otel.resource.attributes", "");
79+
testServiceName(map, "unknown_service:java", "default service name should be provided");
80+
81+
map.clear();
82+
map.put("otel.resource.attributes", "service.name=");
83+
testServiceName(map, "unknown_service:java", "default service name should be provided");
7684
}
7785

7886
private static void testServiceName(
@@ -83,4 +91,28 @@ private static void testServiceName(
8391
.describedAs(description)
8492
.isEqualTo(expectedServiceName);
8593
}
94+
95+
@Test
96+
void getServiceEnvironment() {
97+
Map<String, String> map = Collections.emptyMap();
98+
testServiceEnvironment(map, null, "no environment by default");
99+
100+
map = Collections.singletonMap("otel.resource.attributes", "deployment.environment.name=test1");
101+
testServiceEnvironment(map, "test1", "environment set through resource attribute");
102+
103+
map = Collections.singletonMap("otel.resource.attributes", "deployment.environment=test2");
104+
testServiceEnvironment(map, "test2", "environment set through legacy resource attribute");
105+
106+
map = Collections.singletonMap("otel.resource.attributes", "deployment.environment=test3,deployment.environment.name=test4");
107+
testServiceEnvironment(map, "test4", "when both set semconv attribute takes precedence");
108+
109+
}
110+
111+
private static void testServiceEnvironment(
112+
Map<String, String> map, String expectedEnvironment, String description) {
113+
ConfigProperties configProperties = DefaultConfigProperties.createFromMap(map);
114+
assertThat(CentralConfig.getServiceEnvironment(configProperties))
115+
.describedAs(description)
116+
.isEqualTo(expectedEnvironment);
117+
}
86118
}

0 commit comments

Comments
 (0)