Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Polish OpenTelemetryResourceAttributes #44677

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package org.springframework.boot.actuate.autoconfigure.opentelemetry;

import java.io.ByteArrayOutputStream;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.LinkedHashMap;
Expand Down Expand Up @@ -78,8 +77,8 @@ public OpenTelemetryResourceAttributes(Environment environment, Map<String, Stri
}

/**
* Applies resource attributes to the provided BiConsumer after being combined from
* environment variables and user-defined resource attributes.
* Applies resource attributes to the provided {@link BiConsumer} after being combined
* from environment variables and user-defined resource attributes.
* <p>
* 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.
Expand Down Expand Up @@ -139,7 +138,7 @@ private Map<String, String> getResourceAttributesFromEnv() {
if (index > 0) {
String key = attribute.substring(0, index);
String value = attribute.substring(index + 1);
attributes.put(key.trim(), decode(value.trim()));
attributes.put(key.trim(), StringUtils.uriDecode(value.trim(), StandardCharsets.UTF_8));
}
}
String otelServiceName = getEnv("OTEL_SERVICE_NAME");
Expand All @@ -153,43 +152,4 @@ private String getEnv(String name) {
return this.getEnv.apply(name);
}

/**
* Decodes a percent-encoded string. Converts sequences like '%HH' (where HH
* represents hexadecimal digits) back into their literal representations.
* <p>
* Inspired by {@code org.apache.commons.codec.net.PercentCodec}.
* @param value value to decode
* @return the decoded string
*/
private static String decode(String value) {
if (value.indexOf('%') < 0) {
return value;
}
byte[] bytes = value.getBytes(StandardCharsets.UTF_8);
ByteArrayOutputStream bos = new ByteArrayOutputStream(bytes.length);
for (int i = 0; i < bytes.length; i++) {
byte b = bytes[i];
if (b != '%') {
bos.write(b);
continue;
}
int u = decodeHex(bytes, i + 1);
int l = decodeHex(bytes, i + 2);
if (u >= 0 && l >= 0) {
bos.write((u << 4) + l);
}
else {
throw new IllegalArgumentException(
"Failed to decode percent-encoded characters at index %d in the value: '%s'".formatted(i,
value));
}
i += 2;
}
return bos.toString(StandardCharsets.UTF_8);
}

private static int decodeHex(byte[] bytes, int index) {
return (index < bytes.length) ? Character.digit(bytes[index], 16) : -1;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ void otelResourceAttributeValuesShouldBePercentDecoded() {
void illegalArgumentExceptionShouldBeThrownWhenDecodingIllegalHexCharPercentEncodedValue() {
this.environmentVariables.put("OTEL_RESOURCE_ATTRIBUTES", "key=abc%ß");
assertThatIllegalArgumentException().isThrownBy(this::getAttributes)
.withMessage("Failed to decode percent-encoded characters at index 3 in the value: 'abc%ß'");
.withMessage("Invalid encoded sequence \"\"");
}

@Test
Expand All @@ -134,7 +134,7 @@ void replacementCharShouldBeUsedWhenDecodingNonUtf8Character() {
void illegalArgumentExceptionShouldBeThrownWhenDecodingInvalidPercentEncodedValue() {
this.environmentVariables.put("OTEL_RESOURCE_ATTRIBUTES", "key=%");
assertThatIllegalArgumentException().isThrownBy(this::getAttributes)
.withMessage("Failed to decode percent-encoded characters at index 0 in the value: '%'");
.withMessage("Invalid encoded sequence \"%\"");
}

@Test
Expand Down
Loading