16
16
17
17
package org .springframework .boot .actuate .autoconfigure .opentelemetry ;
18
18
19
- import java .io .ByteArrayOutputStream ;
20
19
import java .nio .charset .StandardCharsets ;
21
20
import java .util .Collections ;
22
21
import java .util .LinkedHashMap ;
@@ -78,8 +77,8 @@ public OpenTelemetryResourceAttributes(Environment environment, Map<String, Stri
78
77
}
79
78
80
79
/**
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.
83
82
* <p>
84
83
* If a key exists in both environment variables and user-defined resources, the value
85
84
* from the user-defined resource takes precedence, even if it is empty.
@@ -97,13 +96,13 @@ public void applyTo(BiConsumer<String, String> consumer) {
97
96
attributes .put (name , value );
98
97
}
99
98
});
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 ());
102
101
attributes .computeIfAbsent ("service.namespace" , (key ) -> getServiceNamespace ());
103
102
attributes .forEach (consumer );
104
103
}
105
104
106
- private String getApplicationName () {
105
+ private String getServiceName () {
107
106
return this .environment .getProperty ("spring.application.name" , DEFAULT_SERVICE_NAME );
108
107
}
109
108
@@ -113,7 +112,7 @@ private String getApplicationName() {
113
112
* @deprecated since 3.5.0 for removal in 3.7.0
114
113
*/
115
114
@ Deprecated (since = "3.5.0" , forRemoval = true )
116
- private String getApplicationGroup () {
115
+ private String getServiceGroup () {
117
116
String applicationGroup = this .environment .getProperty ("spring.application.group" );
118
117
return (StringUtils .hasLength (applicationGroup )) ? applicationGroup : null ;
119
118
}
@@ -139,7 +138,7 @@ private Map<String, String> getResourceAttributesFromEnv() {
139
138
if (index > 0 ) {
140
139
String key = attribute .substring (0 , index );
141
140
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 ));
143
142
}
144
143
}
145
144
String otelServiceName = getEnv ("OTEL_SERVICE_NAME" );
@@ -153,43 +152,4 @@ private String getEnv(String name) {
153
152
return this .getEnv .apply (name );
154
153
}
155
154
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
-
195
155
}
0 commit comments