Skip to content

Commit c860a6d

Browse files
committed
Update OpenTelemetryResourceAttributes to exclude blank keys and trim keys/values
Signed-off-by: Dmytro Nosan <[email protected]>
1 parent e886785 commit c860a6d

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import org.springframework.util.StringUtils;
2727

2828
/**
29-
* OpenTelemetryResourceAttributes retrieves information from the
29+
* {@link OpenTelemetryResourceAttributes} retrieves information from the
3030
* {@code OTEL_RESOURCE_ATTRIBUTES} and {@code OTEL_SERVICE_NAME} environment variables
3131
* and merges it with the resource attributes provided by the user.
3232
* <p>
@@ -70,14 +70,16 @@ public OpenTelemetryResourceAttributes(Map<String, String> resourceAttributes) {
7070
* If a key exists in both environment variables and user-defined resources, the value
7171
* from the user-defined resource takes precedence, even if it is empty.
7272
* <p>
73-
* <b>Null keys and values are ignored.</b>
73+
* <b>Keys that are null or empty will be ignored, and all keys will be trimmed.</b>
74+
* <p>
75+
* <b>Values that are null will be ignored, and all values will be trimmed.</b>
7476
* @return the resource attributes
7577
*/
7678
public Map<String, String> asMap() {
7779
Map<String, String> attributes = getResourceAttributesFromEnv();
7880
this.resourceAttributes.forEach((name, value) -> {
79-
if (name != null && value != null) {
80-
attributes.put(name, value);
81+
if (StringUtils.hasText(name) && value != null) {
82+
attributes.put(name.trim(), value.trim());
8183
}
8284
});
8385
return attributes;

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ class OpenTelemetryResourceAttributesTests {
4848
@BeforeAll
4949
static void beforeAll() {
5050
long seed = new Random().nextLong();
51-
System.out.println("Seed: " + seed);
5251
random = new Random(seed);
5352
}
5453

@@ -85,7 +84,7 @@ void otelResourceAttributesShouldBeUsed() {
8584
@Test
8685
void resourceAttributesShouldBeMergedWithEnvironmentVariables() {
8786
this.resourceAttributes.put("service.group", "custom-group");
88-
this.resourceAttributes.put("key2", "");
87+
this.resourceAttributes.put(" key2 ", " ");
8988
this.environmentVariables.put("OTEL_SERVICE_NAME", "custom-service");
9089
this.environmentVariables.put("OTEL_RESOURCE_ATTRIBUTES", "key1=value1,key2=value2");
9190
OpenTelemetryResourceAttributes attributes = getAttributes();
@@ -97,10 +96,11 @@ void resourceAttributesShouldBeMergedWithEnvironmentVariables() {
9796
}
9897

9998
@Test
100-
void resourceAttributesWithNullKeyOrValueShouldBeIgnored() {
99+
void resourceAttributesWithBlankKeyOrValueShouldBeIgnored() {
101100
this.resourceAttributes.put("service.group", null);
102101
this.resourceAttributes.put("service.name", null);
103-
this.resourceAttributes.put(null, "value");
102+
this.resourceAttributes.put(null, "null-key");
103+
this.resourceAttributes.put(" ", "blank-key");
104104
this.environmentVariables.put("OTEL_SERVICE_NAME", "custom-service");
105105
this.environmentVariables.put("OTEL_RESOURCE_ATTRIBUTES", "key1=value1,key2=value2");
106106
OpenTelemetryResourceAttributes attributes = getAttributes();

0 commit comments

Comments
 (0)