Skip to content

Add support for OpenTelemetry's service.namespace #44499

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -99,18 +99,26 @@ public void applyTo(BiConsumer<String, String> consumer) {
});
attributes.computeIfAbsent("service.name", (k) -> getApplicationName());
attributes.computeIfAbsent("service.group", (k) -> getApplicationGroup());
attributes.computeIfAbsent("service.namespace", (key) -> getServiceNamespace());
attributes.forEach(consumer);
}

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

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

private String getServiceNamespace() {
return this.environment.getProperty("spring.application.group");
}

/**
* Parses resource attributes from the {@link System#getenv()}. This method fetches
* attributes defined in the {@code OTEL_RESOURCE_ATTRIBUTES} and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,22 @@ void shouldNotApplySpringApplicationGroupIfNotSet() {
});
}

@Test
void shouldApplyServiceNamespaceIfApplicationGroupIsSet() {
this.runner.withPropertyValues("spring.application.group=my-group").run((context) -> {
Resource resource = context.getBean(Resource.class);
assertThat(resource.getAttributes().asMap()).containsEntry(AttributeKey.stringKey("service.namespace"), "my-group");
});
}

@Test
void shouldNOtApplyServiceNamespaceIfApplicationGroupIsNotSet() {
this.runner.run((context -> {
Resource resource = context.getBean(Resource.class);
assertThat(resource.getAttributes().asMap()).doesNotContainKey(AttributeKey.stringKey("service.namespace"));
}));
}

@Test
void shouldFallbackToDefaultApplicationNameIfSpringApplicationNameIsNotSet() {
this.runner.run((context) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,10 @@ void unknownServiceShouldBeUsedAsDefaultServiceName() {
@Test
void springApplicationGroupNameShouldBeUsedAsDefaultServiceGroup() {
this.environment.setProperty("spring.application.group", "spring-boot");
assertThat(getAttributes()).hasSize(2)
assertThat(getAttributes()).hasSize(3)
.containsEntry("service.name", "unknown_service")
.containsEntry("service.group", "spring-boot");
.containsEntry("service.group", "spring-boot")
.containsEntry("service.namespace", "spring-boot");
}

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

@Test
void serviceNamespaceShouldNotBePresentByDefault() {
assertThat(getAttributes()).hasSize(1).doesNotContainKey("service.namespace");
}

@Test
void resourceAttributesShouldTakePrecedenceOverSpringApplicationName() {
this.resourceAttributes.put("service.name", "spring-boot");
Expand All @@ -192,18 +198,50 @@ void otelServiceNameShouldTakePrecedenceOverSpringApplicationName() {
void resourceAttributesShouldTakePrecedenceOverSpringApplicationGroupName() {
this.resourceAttributes.put("service.group", "spring-boot-app");
this.environment.setProperty("spring.application.group", "spring-boot");
assertThat(getAttributes()).hasSize(2)
assertThat(getAttributes()).hasSize(3)
.containsEntry("service.name", "unknown_service")
.containsEntry("service.group", "spring-boot-app");
}

@Test
void resourceAttributesShouldTakePrecedenceOverApplicationGroupNameForPopulatingServiceNamespace() {
this.resourceAttributes.put("service.namespace", "spring-boot-app");
this.environment.setProperty("spring.application.group", "overriden");
assertThat(getAttributes()).hasSize(3)
.containsEntry("service.name", "unknown_service")
.containsEntry("service.group", "overriden")
.containsEntry("service.namespace", "spring-boot-app");
}

@Test
void otelResourceAttributesShouldTakePrecedenceOverSpringApplicationGroupName() {
this.environmentVariables.put("OTEL_RESOURCE_ATTRIBUTES", "service.group=spring-boot");
this.environment.setProperty("spring.application.group", "spring-boot-app");
assertThat(getAttributes()).hasSize(2)
assertThat(getAttributes()).hasSize(3)
.containsEntry("service.name", "unknown_service")
.containsEntry("service.group", "spring-boot");
.containsEntry("service.group", "spring-boot")
.containsEntry("service.namespace", "spring-boot-app");
}

@Test
void otelResourceAttributesShouldTakePrecedenceOverSpringApplicationGroupNameForServiceNamespace() {
this.environmentVariables.put("OTEL_RESOURCE_ATTRIBUTES", "service.namespace=spring-boot");
this.environment.setProperty("spring.application.group", "overriden");
;
assertThat(getAttributes()).hasSize(3)
.containsEntry("service.group", "overriden")
.containsEntry("service.namespace", "spring-boot");
}

@Test
void shouldUseServiceGroupForServiceNamespaceIfServiceGroupIsSet() {
this.environment.setProperty("spring.application.group", "alpha");
assertThat(getAttributes()).containsEntry("service.namespace", "alpha");
}

@Test
void shouldNotSetServiceNamespaceIfServiceGroupIsNotSet() {
assertThat(getAttributes()).doesNotContainKey("service.namespace");
}

private Map<String, String> getAttributes() {
Expand Down
Loading