Skip to content

Commit 17b61f0

Browse files
committed
Polish OpenTelemetryResourceAttributes
Update OpenTelemetryResourceAttributes to use StringUtils.uriDecode(...) as it provides the same functionality as the custom decode(...) method. Signed-off-by: Dmytro Nosan <[email protected]>
1 parent 1c350fb commit 17b61f0

File tree

2 files changed

+9
-49
lines changed

2 files changed

+9
-49
lines changed

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

+7-47
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
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;
2221
import java.util.LinkedHashMap;
@@ -78,8 +77,8 @@ public OpenTelemetryResourceAttributes(Environment environment, Map<String, Stri
7877
}
7978

8079
/**
81-
* Applies resource attributes to the provided BiConsumer after being combined from
82-
* environment variables and user-defined resource attributes.
80+
* Applies resource attributes to the provided {@link BiConsumer} after being combined
81+
* from environment variables and user-defined resource attributes.
8382
* <p>
8483
* If a key exists in both environment variables and user-defined resources, the value
8584
* from the user-defined resource takes precedence, even if it is empty.
@@ -97,13 +96,13 @@ public void applyTo(BiConsumer<String, String> consumer) {
9796
attributes.put(name, value);
9897
}
9998
});
100-
attributes.computeIfAbsent("service.name", (key) -> getApplicationName());
101-
attributes.computeIfAbsent("service.group", (key) -> getApplicationGroup());
99+
attributes.computeIfAbsent("service.name", (key) -> getServiceName());
100+
attributes.computeIfAbsent("service.group", (key) -> getServiceGroup());
102101
attributes.computeIfAbsent("service.namespace", (key) -> getServiceNamespace());
103102
attributes.forEach(consumer);
104103
}
105104

106-
private String getApplicationName() {
105+
private String getServiceName() {
107106
return this.environment.getProperty("spring.application.name", DEFAULT_SERVICE_NAME);
108107
}
109108

@@ -113,7 +112,7 @@ private String getApplicationName() {
113112
* @deprecated since 3.5.0 for removal in 3.7.0
114113
*/
115114
@Deprecated(since = "3.5.0", forRemoval = true)
116-
private String getApplicationGroup() {
115+
private String getServiceGroup() {
117116
String applicationGroup = this.environment.getProperty("spring.application.group");
118117
return (StringUtils.hasLength(applicationGroup)) ? applicationGroup : null;
119118
}
@@ -139,7 +138,7 @@ private Map<String, String> getResourceAttributesFromEnv() {
139138
if (index > 0) {
140139
String key = attribute.substring(0, index);
141140
String value = attribute.substring(index + 1);
142-
attributes.put(key.trim(), decode(value.trim()));
141+
attributes.put(key.trim(), StringUtils.uriDecode(value.trim(), StandardCharsets.UTF_8));
143142
}
144143
}
145144
String otelServiceName = getEnv("OTEL_SERVICE_NAME");
@@ -153,43 +152,4 @@ private String getEnv(String name) {
153152
return this.getEnv.apply(name);
154153
}
155154

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-
*/
164-
private static String decode(String value) {
165-
if (value.indexOf('%') < 0) {
166-
return value;
167-
}
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);
174-
continue;
175-
}
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));
185-
}
186-
i += 2;
187-
}
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;
193-
}
194-
195155
}

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

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

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

140140
@Test

0 commit comments

Comments
 (0)