Skip to content

Commit

Permalink
Update OpenTelemetryResourceAttributes to exclude blank keys and trim…
Browse files Browse the repository at this point in the history
… keys/values

Signed-off-by: Dmytro Nosan <[email protected]>
  • Loading branch information
nosan committed Feb 28, 2025
1 parent e886785 commit c860a6d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
* <p>
Expand Down Expand Up @@ -70,14 +70,16 @@ public OpenTelemetryResourceAttributes(Map<String, String> 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.
* <p>
* <b>Null keys and values are ignored.</b>
* <b>Keys that are null or empty will be ignored, and all keys will be trimmed.</b>
* <p>
* <b>Values that are null will be ignored, and all values will be trimmed.</b>
* @return the resource attributes
*/
public Map<String, String> asMap() {
Map<String, String> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ class OpenTelemetryResourceAttributesTests {
@BeforeAll
static void beforeAll() {
long seed = new Random().nextLong();
System.out.println("Seed: " + seed);
random = new Random(seed);
}

Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand Down

0 comments on commit c860a6d

Please sign in to comment.