18
18
19
19
import java .util .LinkedHashMap ;
20
20
import java .util .Map ;
21
- import java .util .Random ;
22
21
import java .util .function .Function ;
23
- import java .util .stream .Stream ;
22
+ import java .util .stream .IntStream ;
24
23
25
24
import io .opentelemetry .api .internal .PercentEscaper ;
26
25
import org .assertj .core .api .InstanceOfAssertFactories ;
27
- import org .junit .jupiter .api .BeforeAll ;
28
26
import org .junit .jupiter .api .Test ;
29
27
30
28
import org .springframework .mock .env .MockEnvironment ;
39
37
*/
40
38
class OpenTelemetryResourceAttributesTests {
41
39
42
- private static Random random ;
43
-
44
- private static final PercentEscaper escaper = PercentEscaper .create ();
45
-
46
40
private final MockEnvironment environment = new MockEnvironment ();
47
41
48
42
private final Map <String , String > environmentVariables = new LinkedHashMap <>();
49
43
50
44
private final Map <String , String > resourceAttributes = new LinkedHashMap <>();
51
45
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
-
59
46
@ Test
60
47
void otelServiceNameShouldTakePrecedenceOverOtelResourceAttributes () {
61
48
this .environmentVariables .put ("OTEL_RESOURCE_ATTRIBUTES" , "service.name=ignored" );
@@ -73,13 +60,13 @@ void otelServiceNameWhenEmptyShouldTakePrecedenceOverOtelResourceAttributes() {
73
60
@ Test
74
61
void otelResourceAttributes () {
75
62
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=ś" );
77
64
assertThat (getAttributes ()).hasSize (7 )
78
65
.containsEntry ("key1" , "value1" )
79
66
.containsEntry ("key2" , "value2" )
80
67
.containsEntry ("key3" , "value3" )
81
68
.containsEntry ("key4" , "" )
82
- .containsEntry ("key7" , "spring+boot" )
69
+ .containsEntry ("key7" , " spring+boot " )
83
70
.containsEntry ("key8" , "ś" )
84
71
.containsEntry ("service.name" , "unknown_service" );
85
72
}
@@ -120,12 +107,14 @@ void systemGetEnvShouldBeUsedAsDefaultEnvFunction() {
120
107
121
108
@ Test
122
109
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 );
129
118
}
130
119
131
120
@ Test
@@ -206,10 +195,10 @@ void resourceAttributesShouldTakePrecedenceOverSpringApplicationGroupName() {
206
195
@ Test
207
196
void resourceAttributesShouldTakePrecedenceOverApplicationGroupNameForPopulatingServiceNamespace () {
208
197
this .resourceAttributes .put ("service.namespace" , "spring-boot-app" );
209
- this .environment .setProperty ("spring.application.group" , "overriden " );
198
+ this .environment .setProperty ("spring.application.group" , "overridden " );
210
199
assertThat (getAttributes ()).hasSize (3 )
211
200
.containsEntry ("service.name" , "unknown_service" )
212
- .containsEntry ("service.group" , "overriden " )
201
+ .containsEntry ("service.group" , "overridden " )
213
202
.containsEntry ("service.namespace" , "spring-boot-app" );
214
203
}
215
204
@@ -226,9 +215,9 @@ void otelResourceAttributesShouldTakePrecedenceOverSpringApplicationGroupName()
226
215
@ Test
227
216
void otelResourceAttributesShouldTakePrecedenceOverSpringApplicationGroupNameForServiceNamespace () {
228
217
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 " );
230
219
assertThat (getAttributes ()).hasSize (3 )
231
- .containsEntry ("service.group" , "overriden " )
220
+ .containsEntry ("service.group" , "overridden " )
232
221
.containsEntry ("service.namespace" , "spring-boot" );
233
222
}
234
223
@@ -250,11 +239,4 @@ private Map<String, String> getAttributes() {
250
239
return attributes ;
251
240
}
252
241
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
-
260
242
}
0 commit comments