Skip to content

Commit 6a5f8ac

Browse files
committed
Merge pull request #44636 from nosan
* pr/44636: Polish OpenTelemetryResourceAttributesTests Closes gh-44636
2 parents 6f464e7 + b3f9033 commit 6a5f8ac

File tree

1 file changed

+15
-33
lines changed

1 file changed

+15
-33
lines changed

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

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,11 @@
1818

1919
import java.util.LinkedHashMap;
2020
import java.util.Map;
21-
import java.util.Random;
2221
import java.util.function.Function;
23-
import java.util.stream.Stream;
22+
import java.util.stream.IntStream;
2423

2524
import io.opentelemetry.api.internal.PercentEscaper;
2625
import org.assertj.core.api.InstanceOfAssertFactories;
27-
import org.junit.jupiter.api.BeforeAll;
2826
import org.junit.jupiter.api.Test;
2927

3028
import org.springframework.mock.env.MockEnvironment;
@@ -39,23 +37,12 @@
3937
*/
4038
class OpenTelemetryResourceAttributesTests {
4139

42-
private static Random random;
43-
44-
private static final PercentEscaper escaper = PercentEscaper.create();
45-
4640
private final MockEnvironment environment = new MockEnvironment();
4741

4842
private final Map<String, String> environmentVariables = new LinkedHashMap<>();
4943

5044
private final Map<String, String> resourceAttributes = new LinkedHashMap<>();
5145

52-
@BeforeAll
53-
static void beforeAll() {
54-
long seed = new Random().nextLong();
55-
System.out.println(OpenTelemetryResourceAttributesTests.class.getSimpleName() + " seed: " + seed);
56-
random = new Random(seed);
57-
}
58-
5946
@Test
6047
void otelServiceNameShouldTakePrecedenceOverOtelResourceAttributes() {
6148
this.environmentVariables.put("OTEL_RESOURCE_ATTRIBUTES", "service.name=ignored");
@@ -73,13 +60,13 @@ void otelServiceNameWhenEmptyShouldTakePrecedenceOverOtelResourceAttributes() {
7360
@Test
7461
void otelResourceAttributes() {
7562
this.environmentVariables.put("OTEL_RESOURCE_ATTRIBUTES",
76-
", ,,key1=value1,key2= value2, key3=value3,key4=,=value5,key6,=,key7=spring+boot,key8=ś");
63+
", ,,key1=value1,key2= value2, key3=value3,key4=,=value5,key6,=,key7=%20spring+boot%20,key8=ś");
7764
assertThat(getAttributes()).hasSize(7)
7865
.containsEntry("key1", "value1")
7966
.containsEntry("key2", "value2")
8067
.containsEntry("key3", "value3")
8168
.containsEntry("key4", "")
82-
.containsEntry("key7", "spring+boot")
69+
.containsEntry("key7", " spring+boot ")
8370
.containsEntry("key8", "ś")
8471
.containsEntry("service.name", "unknown_service");
8572
}
@@ -120,12 +107,14 @@ void systemGetEnvShouldBeUsedAsDefaultEnvFunction() {
120107

121108
@Test
122109
void otelResourceAttributeValuesShouldBePercentDecoded() {
123-
Stream.generate(this::generateRandomString).limit(10000).forEach((value) -> {
124-
this.environmentVariables.put("OTEL_RESOURCE_ATTRIBUTES", "key=" + escaper.escape(value));
125-
assertThat(getAttributes()).hasSize(2)
126-
.containsEntry("service.name", "unknown_service")
127-
.containsEntry("key", value);
128-
});
110+
PercentEscaper escaper = PercentEscaper.create();
111+
String value = IntStream.range(32, 127)
112+
.collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append)
113+
.toString();
114+
this.environmentVariables.put("OTEL_RESOURCE_ATTRIBUTES", "key=" + escaper.escape(value));
115+
assertThat(getAttributes()).hasSize(2)
116+
.containsEntry("service.name", "unknown_service")
117+
.containsEntry("key", value);
129118
}
130119

131120
@Test
@@ -206,10 +195,10 @@ void resourceAttributesShouldTakePrecedenceOverSpringApplicationGroupName() {
206195
@Test
207196
void resourceAttributesShouldTakePrecedenceOverApplicationGroupNameForPopulatingServiceNamespace() {
208197
this.resourceAttributes.put("service.namespace", "spring-boot-app");
209-
this.environment.setProperty("spring.application.group", "overriden");
198+
this.environment.setProperty("spring.application.group", "overridden");
210199
assertThat(getAttributes()).hasSize(3)
211200
.containsEntry("service.name", "unknown_service")
212-
.containsEntry("service.group", "overriden")
201+
.containsEntry("service.group", "overridden")
213202
.containsEntry("service.namespace", "spring-boot-app");
214203
}
215204

@@ -226,9 +215,9 @@ void otelResourceAttributesShouldTakePrecedenceOverSpringApplicationGroupName()
226215
@Test
227216
void otelResourceAttributesShouldTakePrecedenceOverSpringApplicationGroupNameForServiceNamespace() {
228217
this.environmentVariables.put("OTEL_RESOURCE_ATTRIBUTES", "service.namespace=spring-boot");
229-
this.environment.setProperty("spring.application.group", "overriden");
218+
this.environment.setProperty("spring.application.group", "overridden");
230219
assertThat(getAttributes()).hasSize(3)
231-
.containsEntry("service.group", "overriden")
220+
.containsEntry("service.group", "overridden")
232221
.containsEntry("service.namespace", "spring-boot");
233222
}
234223

@@ -250,11 +239,4 @@ private Map<String, String> getAttributes() {
250239
return attributes;
251240
}
252241

253-
private String generateRandomString() {
254-
return random.ints(32, 127)
255-
.limit(64)
256-
.collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append)
257-
.toString();
258-
}
259-
260242
}

0 commit comments

Comments
 (0)