Polish "Add support for OpenTelemetry's service.namespace"
See gh-44499
This commit is contained in:
parent
cf2353bdb3
commit
7fd29cf7c6
|
@ -86,7 +86,7 @@ public final class OpenTelemetryResourceAttributes {
|
||||||
* <p>
|
* <p>
|
||||||
* Additionally, {@code spring.application.name} or {@code unknown_service} will be
|
* Additionally, {@code spring.application.name} or {@code unknown_service} will be
|
||||||
* used as the default for {@code service.name}, and {@code spring.application.group}
|
* used as the default for {@code service.name}, and {@code spring.application.group}
|
||||||
* will serve as the default for {@code service.group}.
|
* will serve as the default for {@code service.group} and {@code service.namespace}.
|
||||||
* @param consumer the {@link BiConsumer} to apply
|
* @param consumer the {@link BiConsumer} to apply
|
||||||
*/
|
*/
|
||||||
public void applyTo(BiConsumer<String, String> consumer) {
|
public void applyTo(BiConsumer<String, String> consumer) {
|
||||||
|
@ -97,8 +97,8 @@ public final class OpenTelemetryResourceAttributes {
|
||||||
attributes.put(name, value);
|
attributes.put(name, value);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
attributes.computeIfAbsent("service.name", (k) -> getApplicationName());
|
attributes.computeIfAbsent("service.name", (key) -> getApplicationName());
|
||||||
attributes.computeIfAbsent("service.group", (k) -> getApplicationGroup());
|
attributes.computeIfAbsent("service.group", (key) -> getApplicationGroup());
|
||||||
attributes.computeIfAbsent("service.namespace", (key) -> getServiceNamespace());
|
attributes.computeIfAbsent("service.namespace", (key) -> getServiceNamespace());
|
||||||
attributes.forEach(consumer);
|
attributes.forEach(consumer);
|
||||||
}
|
}
|
||||||
|
@ -107,9 +107,12 @@ public final class OpenTelemetryResourceAttributes {
|
||||||
return this.environment.getProperty("spring.application.name", DEFAULT_SERVICE_NAME);
|
return this.environment.getProperty("spring.application.name", DEFAULT_SERVICE_NAME);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the application group.
|
||||||
|
* @return the application group
|
||||||
|
* @deprecated since 3.5.0 for removal in 3.7.0
|
||||||
|
*/
|
||||||
@Deprecated(since = "3.5.0", forRemoval = true)
|
@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() {
|
private String getApplicationGroup() {
|
||||||
String applicationGroup = this.environment.getProperty("spring.application.group");
|
String applicationGroup = this.environment.getProperty("spring.application.group");
|
||||||
return (StringUtils.hasLength(applicationGroup)) ? applicationGroup : null;
|
return (StringUtils.hasLength(applicationGroup)) ? applicationGroup : null;
|
||||||
|
|
|
@ -110,13 +110,14 @@ class OpenTelemetryAutoConfigurationTests {
|
||||||
void shouldApplyServiceNamespaceIfApplicationGroupIsSet() {
|
void shouldApplyServiceNamespaceIfApplicationGroupIsSet() {
|
||||||
this.runner.withPropertyValues("spring.application.group=my-group").run((context) -> {
|
this.runner.withPropertyValues("spring.application.group=my-group").run((context) -> {
|
||||||
Resource resource = context.getBean(Resource.class);
|
Resource resource = context.getBean(Resource.class);
|
||||||
assertThat(resource.getAttributes().asMap()).containsEntry(AttributeKey.stringKey("service.namespace"), "my-group");
|
assertThat(resource.getAttributes().asMap()).containsEntry(AttributeKey.stringKey("service.namespace"),
|
||||||
|
"my-group");
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void shouldNOtApplyServiceNamespaceIfApplicationGroupIsNotSet() {
|
void shouldNotApplyServiceNamespaceIfApplicationGroupIsNotSet() {
|
||||||
this.runner.run((context -> {
|
this.runner.run(((context) -> {
|
||||||
Resource resource = context.getBean(Resource.class);
|
Resource resource = context.getBean(Resource.class);
|
||||||
assertThat(resource.getAttributes().asMap()).doesNotContainKey(AttributeKey.stringKey("service.namespace"));
|
assertThat(resource.getAttributes().asMap()).doesNotContainKey(AttributeKey.stringKey("service.namespace"));
|
||||||
}));
|
}));
|
||||||
|
@ -172,7 +173,7 @@ class OpenTelemetryAutoConfigurationTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Configuration(proxyBeanMethods = false)
|
@Configuration(proxyBeanMethods = false)
|
||||||
private static final class UserConfiguration {
|
static class UserConfiguration {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
OpenTelemetry customOpenTelemetry() {
|
OpenTelemetry customOpenTelemetry() {
|
||||||
|
|
|
@ -227,7 +227,6 @@ class OpenTelemetryResourceAttributesTests {
|
||||||
void otelResourceAttributesShouldTakePrecedenceOverSpringApplicationGroupNameForServiceNamespace() {
|
void otelResourceAttributesShouldTakePrecedenceOverSpringApplicationGroupNameForServiceNamespace() {
|
||||||
this.environmentVariables.put("OTEL_RESOURCE_ATTRIBUTES", "service.namespace=spring-boot");
|
this.environmentVariables.put("OTEL_RESOURCE_ATTRIBUTES", "service.namespace=spring-boot");
|
||||||
this.environment.setProperty("spring.application.group", "overriden");
|
this.environment.setProperty("spring.application.group", "overriden");
|
||||||
;
|
|
||||||
assertThat(getAttributes()).hasSize(3)
|
assertThat(getAttributes()).hasSize(3)
|
||||||
.containsEntry("service.group", "overriden")
|
.containsEntry("service.group", "overriden")
|
||||||
.containsEntry("service.namespace", "spring-boot");
|
.containsEntry("service.namespace", "spring-boot");
|
||||||
|
|
Loading…
Reference in New Issue