From c860a6d1b20ac440b57da62364cb1d03e4c2f9d8 Mon Sep 17 00:00:00 2001 From: Dmytro Nosan Date: Fri, 28 Feb 2025 17:51:54 +0200 Subject: [PATCH] Update OpenTelemetryResourceAttributes to exclude blank keys and trim keys/values Signed-off-by: Dmytro Nosan --- .../opentelemetry/OpenTelemetryResourceAttributes.java | 10 ++++++---- .../OpenTelemetryResourceAttributesTests.java | 8 ++++---- 2 files changed, 10 insertions(+), 8 deletions(-) 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..1a5c1ea15b8d 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; 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..5a67a876bbe7 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,7 +48,6 @@ class OpenTelemetryResourceAttributesTests { @BeforeAll static void beforeAll() { long seed = new Random().nextLong(); - System.out.println("Seed: " + seed); random = new Random(seed); } @@ -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();