Skip to content

Commit e2d6d23

Browse files
committed
Polish OpenTelemetryResourceAttributes and OpenTelemetryResourceAttributesTests
Signed-off-by: Dmytro Nosan <[email protected]>
1 parent faef7d5 commit e2d6d23

File tree

2 files changed

+23
-31
lines changed

2 files changed

+23
-31
lines changed

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

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616

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

19-
import java.io.ByteArrayOutputStream;
2019
import java.nio.charset.StandardCharsets;
2120
import java.util.Collections;
21+
import java.util.HexFormat;
2222
import java.util.LinkedHashMap;
2323
import java.util.Map;
2424
import java.util.function.BiConsumer;
@@ -43,6 +43,8 @@
4343
*/
4444
public final class OpenTelemetryResourceAttributes {
4545

46+
private static final HexFormat HEX_FORMAT = HexFormat.of();
47+
4648
/**
4749
* Default value for service name if {@code service.name} is not set.
4850
*/
@@ -153,43 +155,25 @@ private String getEnv(String name) {
153155
return this.getEnv.apply(name);
154156
}
155157

156-
/**
157-
* Decodes a percent-encoded string. Converts sequences like '%HH' (where HH
158-
* represents hexadecimal digits) back into their literal representations.
159-
* <p>
160-
* Inspired by {@code org.apache.commons.codec.net.PercentCodec}.
161-
* @param value value to decode
162-
* @return the decoded string
163-
*/
164158
private static String decode(String value) {
165159
if (value.indexOf('%') < 0) {
166160
return value;
167161
}
168-
byte[] bytes = value.getBytes(StandardCharsets.UTF_8);
169-
ByteArrayOutputStream bos = new ByteArrayOutputStream(bytes.length);
170-
for (int i = 0; i < bytes.length; i++) {
171-
byte b = bytes[i];
172-
if (b != '%') {
173-
bos.write(b);
162+
int length = value.length();
163+
StringBuilder result = new StringBuilder(length);
164+
for (int i = 0; i < length; i++) {
165+
char ch = value.charAt(i);
166+
if (ch != '%') {
167+
result.append(ch);
174168
continue;
175169
}
176-
int u = decodeHex(bytes, i + 1);
177-
int l = decodeHex(bytes, i + 2);
178-
if (u >= 0 && l >= 0) {
179-
bos.write((u << 4) + l);
180-
}
181-
else {
182-
throw new IllegalArgumentException(
183-
"Failed to decode percent-encoded characters at index %d in the value: '%s'".formatted(i,
184-
value));
170+
if (i + 2 >= length) {
171+
throw new IllegalArgumentException("Invalid encoded sequence \"%s\"".formatted(value.substring(i)));
185172
}
173+
result.append(new String(HEX_FORMAT.parseHex(value, i + 1, i + 3), StandardCharsets.UTF_8));
186174
i += 2;
187175
}
188-
return bos.toString(StandardCharsets.UTF_8);
189-
}
190-
191-
private static int decodeHex(byte[] bytes, int index) {
192-
return (index < bytes.length) ? Character.digit(bytes[index], 16) : -1;
176+
return result.toString();
193177
}
194178

195179
}

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,19 @@ void otelResourceAttributeValuesShouldBePercentDecoded() {
117117
.containsEntry("key", value);
118118
}
119119

120+
@Test
121+
void otelResourceAttributeValuesShouldBePercentDecodedWhenStringContainsNonAscii() {
122+
this.environmentVariables.put("OTEL_RESOURCE_ATTRIBUTES", "key=%20\u015bp\u0159\u00ec\u0144\u0121%20");
123+
assertThat(getAttributes()).hasSize(2)
124+
.containsEntry("service.name", "unknown_service")
125+
.containsEntry("key", " śpřìńġ ");
126+
}
127+
120128
@Test
121129
void illegalArgumentExceptionShouldBeThrownWhenDecodingIllegalHexCharPercentEncodedValue() {
122130
this.environmentVariables.put("OTEL_RESOURCE_ATTRIBUTES", "key=abc%ß");
123131
assertThatIllegalArgumentException().isThrownBy(this::getAttributes)
124-
.withMessage("Failed to decode percent-encoded characters at index 3 in the value: 'abc%ß'");
132+
.withMessage("Invalid encoded sequence \"\"");
125133
}
126134

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

140148
@Test

0 commit comments

Comments
 (0)