Skip to content

Commit 0dd0829

Browse files
committed
Implement the addition of service.namespace attributes to the OpenTelemetryResourceAttributes
Signed-off-by: Vanio Begic <[email protected]>
1 parent 22c9a8d commit 0dd0829

File tree

2 files changed

+51
-5
lines changed

2 files changed

+51
-5
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/opentelemetry/OpenTelemetryResourceAttributes.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,18 +99,26 @@ public void applyTo(BiConsumer<String, String> consumer) {
9999
});
100100
attributes.computeIfAbsent("service.name", (k) -> getApplicationName());
101101
attributes.computeIfAbsent("service.group", (k) -> getApplicationGroup());
102+
attributes.computeIfAbsent("service.namespace", (key) -> getServiceNamespace());
102103
attributes.forEach(consumer);
103104
}
104105

105106
private String getApplicationName() {
106107
return this.environment.getProperty("spring.application.name", DEFAULT_SERVICE_NAME);
107108
}
108109

110+
@Deprecated(since = "3.5.0", forRemoval = true)
111+
// See https://github.com/spring-projects/spring-boot/issues/44411 for potential
112+
// information about deprecation of "service.group" attribute
109113
private String getApplicationGroup() {
110114
String applicationGroup = this.environment.getProperty("spring.application.group");
111115
return (StringUtils.hasLength(applicationGroup)) ? applicationGroup : null;
112116
}
113117

118+
private String getServiceNamespace() {
119+
return this.environment.getProperty("spring.application.group");
120+
}
121+
114122
/**
115123
* Parses resource attributes from the {@link System#getenv()}. This method fetches
116124
* attributes defined in the {@code OTEL_RESOURCE_ATTRIBUTES} and

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/opentelemetry/OpenTelemetryResourceAttributesTests.java

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,10 @@ void unknownServiceShouldBeUsedAsDefaultServiceName() {
156156
@Test
157157
void springApplicationGroupNameShouldBeUsedAsDefaultServiceGroup() {
158158
this.environment.setProperty("spring.application.group", "spring-boot");
159-
assertThat(getAttributes()).hasSize(2)
159+
assertThat(getAttributes()).hasSize(3)
160160
.containsEntry("service.name", "unknown_service")
161-
.containsEntry("service.group", "spring-boot");
161+
.containsEntry("service.group", "spring-boot")
162+
.containsEntry("service.namespace", "spring-boot");
162163
}
163164

164165
@Test
@@ -167,6 +168,11 @@ void springApplicationNameShouldBeUsedAsDefaultServiceName() {
167168
assertThat(getAttributes()).hasSize(1).containsEntry("service.name", "spring-boot-app");
168169
}
169170

171+
@Test
172+
void serviceNamespaceShouldNotBePresentByDefault() {
173+
assertThat(getAttributes()).hasSize(1).doesNotContainKey("service.namespace");
174+
}
175+
170176
@Test
171177
void resourceAttributesShouldTakePrecedenceOverSpringApplicationName() {
172178
this.resourceAttributes.put("service.name", "spring-boot");
@@ -192,18 +198,50 @@ void otelServiceNameShouldTakePrecedenceOverSpringApplicationName() {
192198
void resourceAttributesShouldTakePrecedenceOverSpringApplicationGroupName() {
193199
this.resourceAttributes.put("service.group", "spring-boot-app");
194200
this.environment.setProperty("spring.application.group", "spring-boot");
195-
assertThat(getAttributes()).hasSize(2)
201+
assertThat(getAttributes()).hasSize(3)
196202
.containsEntry("service.name", "unknown_service")
197203
.containsEntry("service.group", "spring-boot-app");
198204
}
199205

206+
@Test
207+
void resourceAttributesShouldTakePrecedenceOverApplicationGroupNameForPopulatingServiceNamespace() {
208+
this.resourceAttributes.put("service.namespace", "spring-boot-app");
209+
this.environment.setProperty("spring.application.group", "overriden");
210+
assertThat(getAttributes()).hasSize(3)
211+
.containsEntry("service.name", "unknown_service")
212+
.containsEntry("service.group", "overriden")
213+
.containsEntry("service.namespace", "spring-boot-app");
214+
}
215+
200216
@Test
201217
void otelResourceAttributesShouldTakePrecedenceOverSpringApplicationGroupName() {
202218
this.environmentVariables.put("OTEL_RESOURCE_ATTRIBUTES", "service.group=spring-boot");
203219
this.environment.setProperty("spring.application.group", "spring-boot-app");
204-
assertThat(getAttributes()).hasSize(2)
220+
assertThat(getAttributes()).hasSize(3)
205221
.containsEntry("service.name", "unknown_service")
206-
.containsEntry("service.group", "spring-boot");
222+
.containsEntry("service.group", "spring-boot")
223+
.containsEntry("service.namespace", "spring-boot-app");
224+
}
225+
226+
@Test
227+
void otelResourceAttributesShouldTakePrecedenceOverSpringApplicationGroupNameForServiceNamespace() {
228+
this.environmentVariables.put("OTEL_RESOURCE_ATTRIBUTES", "service.namespace=spring-boot");
229+
this.environment.setProperty("spring.application.group", "overriden");
230+
;
231+
assertThat(getAttributes()).hasSize(3)
232+
.containsEntry("service.group", "overriden")
233+
.containsEntry("service.namespace", "spring-boot");
234+
}
235+
236+
@Test
237+
void shouldUseServiceGroupForServiceNamespaceIfServiceGroupIsSet() {
238+
this.environment.setProperty("spring.application.group", "alpha");
239+
assertThat(getAttributes()).containsEntry("service.namespace", "alpha");
240+
}
241+
242+
@Test
243+
void shouldNotSetServiceNamespaceIfServiceGroupIsNotSet() {
244+
assertThat(getAttributes()).doesNotContainKey("service.namespace");
207245
}
208246

209247
private Map<String, String> getAttributes() {

0 commit comments

Comments
 (0)