diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/opentelemetry/OpenTelemetryResourceAttributes.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/opentelemetry/OpenTelemetryResourceAttributes.java index d6ba172e0dcc..3ee1d34d6452 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/opentelemetry/OpenTelemetryResourceAttributes.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/opentelemetry/OpenTelemetryResourceAttributes.java @@ -26,7 +26,7 @@ import org.springframework.util.StringUtils; /** - * OpenTelemetryResourceAttributes retrieves information from the + * {@link OpenTelemetryResourceAttributes} retrieves information from the * {@code OTEL_RESOURCE_ATTRIBUTES} and {@code OTEL_SERVICE_NAME} environment variables * and merges it with the resource attributes provided by the user. *

@@ -70,14 +70,16 @@ public OpenTelemetryResourceAttributes(Map resourceAttributes) { * If a key exists in both environment variables and user-defined resources, the value * from the user-defined resource takes precedence, even if it is empty. *

- * Null keys and values are ignored. + * Keys that are null or empty will be ignored, and all keys will be trimmed. + *

+ * Values that are null will be ignored, and all values will be trimmed. * @return the resource attributes */ public Map asMap() { Map attributes = getResourceAttributesFromEnv(); this.resourceAttributes.forEach((name, value) -> { - if (name != null && value != null) { - attributes.put(name, value); + if (StringUtils.hasText(name) && value != null) { + attributes.put(name.trim(), value.trim()); } }); return attributes; @@ -105,7 +107,7 @@ private Map getResourceAttributesFromEnv() { } String otelServiceName = getEnv("OTEL_SERVICE_NAME"); if (otelServiceName != null) { - attributes.put("service.name", otelServiceName); + attributes.put("service.name", otelServiceName.trim()); } return attributes; } diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/opentelemetry/OpenTelemetryResourceAttributesTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/opentelemetry/OpenTelemetryResourceAttributesTests.java index 799ea7bc2605..976d4dabc8d5 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/opentelemetry/OpenTelemetryResourceAttributesTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/opentelemetry/OpenTelemetryResourceAttributesTests.java @@ -48,14 +48,13 @@ class OpenTelemetryResourceAttributesTests { @BeforeAll static void beforeAll() { long seed = new Random().nextLong(); - System.out.println("Seed: " + seed); random = new Random(seed); } @Test void otelServiceNameShouldTakePrecedenceOverOtelResourceAttributes() { this.environmentVariables.put("OTEL_RESOURCE_ATTRIBUTES", "service.name=ignored"); - this.environmentVariables.put("OTEL_SERVICE_NAME", "otel-service"); + this.environmentVariables.put("OTEL_SERVICE_NAME", " otel-service "); OpenTelemetryResourceAttributes attributes = getAttributes(); assertThat(attributes.asMap()).hasSize(1).containsEntry("service.name", "otel-service"); } @@ -85,7 +84,7 @@ void otelResourceAttributesShouldBeUsed() { @Test void resourceAttributesShouldBeMergedWithEnvironmentVariables() { this.resourceAttributes.put("service.group", "custom-group"); - this.resourceAttributes.put("key2", ""); + this.resourceAttributes.put(" key2 ", " "); this.environmentVariables.put("OTEL_SERVICE_NAME", "custom-service"); this.environmentVariables.put("OTEL_RESOURCE_ATTRIBUTES", "key1=value1,key2=value2"); OpenTelemetryResourceAttributes attributes = getAttributes(); @@ -97,10 +96,11 @@ void resourceAttributesShouldBeMergedWithEnvironmentVariables() { } @Test - void resourceAttributesWithNullKeyOrValueShouldBeIgnored() { + void resourceAttributesWithBlankKeyOrValueShouldBeIgnored() { this.resourceAttributes.put("service.group", null); this.resourceAttributes.put("service.name", null); - this.resourceAttributes.put(null, "value"); + this.resourceAttributes.put(null, "null-key"); + this.resourceAttributes.put(" ", "blank-key"); this.environmentVariables.put("OTEL_SERVICE_NAME", "custom-service"); this.environmentVariables.put("OTEL_RESOURCE_ATTRIBUTES", "key1=value1,key2=value2"); OpenTelemetryResourceAttributes attributes = getAttributes();