19
19
import java .util .LinkedHashMap ;
20
20
import java .util .Map ;
21
21
import java .util .Random ;
22
- import java .util .function .Function ;
23
22
import java .util .stream .Stream ;
24
23
25
24
import io .opentelemetry .api .internal .PercentEscaper ;
26
- import org .assertj .core .api .InstanceOfAssertFactories ;
27
25
import org .junit .jupiter .api .BeforeAll ;
28
26
import org .junit .jupiter .api .Test ;
29
27
@@ -48,47 +46,52 @@ class OpenTelemetryResourceAttributesTests {
48
46
@ BeforeAll
49
47
static void beforeAll () {
50
48
long seed = new Random ().nextLong ();
51
- System .out .println ("Seed: " + seed );
52
49
random = new Random (seed );
53
50
}
54
51
55
52
@ Test
56
53
void otelServiceNameShouldTakePrecedenceOverOtelResourceAttributes () {
57
54
this .environmentVariables .put ("OTEL_RESOURCE_ATTRIBUTES" , "service.name=ignored" );
58
- this .environmentVariables .put ("OTEL_SERVICE_NAME" , "otel-service" );
59
- OpenTelemetryResourceAttributes attributes = getAttributes ();
55
+ this .environmentVariables .put ("OTEL_SERVICE_NAME" , " otel-service " );
56
+ OpenTelemetryResourceAttributes attributes = OpenTelemetryResourceAttributes
57
+ .fromEnv (this .environmentVariables ::get );
60
58
assertThat (attributes .asMap ()).hasSize (1 ).containsEntry ("service.name" , "otel-service" );
61
59
}
62
60
63
61
@ Test
64
62
void otelServiceNameWhenEmptyShouldTakePrecedenceOverOtelResourceAttributes () {
65
63
this .environmentVariables .put ("OTEL_RESOURCE_ATTRIBUTES" , "service.name=ignored" );
66
64
this .environmentVariables .put ("OTEL_SERVICE_NAME" , "" );
67
- OpenTelemetryResourceAttributes attributes = getAttributes ();
65
+ OpenTelemetryResourceAttributes attributes = OpenTelemetryResourceAttributes
66
+ .fromEnv (this .environmentVariables ::get );
68
67
assertThat (attributes .asMap ()).hasSize (1 ).containsEntry ("service.name" , "" );
69
68
}
70
69
71
70
@ Test
72
- void otelResourceAttributesShouldBeUsed () {
71
+ void resourceAttributesShouldBeCreatedFromEnvironmentVariables () {
73
72
this .environmentVariables .put ("OTEL_RESOURCE_ATTRIBUTES" ,
74
- ", ,,key1=value1,key2= value2, key3=value3,key4=,=value5,key6,=,key7=spring+boot,key8=ś" );
75
- OpenTelemetryResourceAttributes attributes = getAttributes ();
76
- assertThat (attributes .asMap ()).hasSize (6 )
73
+ ", ,,key1=value1,key2= value2, key3=value3,key4=,=value5,key6,=,key7=spring+boot,key8=ś,key9=%20A%20" );
74
+ OpenTelemetryResourceAttributes attributes = OpenTelemetryResourceAttributes
75
+ .fromEnv (this .environmentVariables ::get );
76
+ assertThat (attributes .asMap ()).hasSize (7 )
77
77
.containsEntry ("key1" , "value1" )
78
78
.containsEntry ("key2" , "value2" )
79
79
.containsEntry ("key3" , "value3" )
80
80
.containsEntry ("key4" , "" )
81
81
.containsEntry ("key7" , "spring+boot" )
82
- .containsEntry ("key8" , "ś" );
82
+ .containsEntry ("key8" , "ś" )
83
+ .containsEntry ("key9" , "A" );
83
84
}
84
85
85
86
@ Test
86
87
void resourceAttributesShouldBeMergedWithEnvironmentVariables () {
87
88
this .resourceAttributes .put ("service.group" , "custom-group" );
88
- this .resourceAttributes .put ("key2" , "" );
89
+ this .resourceAttributes .put (" key2 " , " " );
89
90
this .environmentVariables .put ("OTEL_SERVICE_NAME" , "custom-service" );
90
91
this .environmentVariables .put ("OTEL_RESOURCE_ATTRIBUTES" , "key1=value1,key2=value2" );
91
- OpenTelemetryResourceAttributes attributes = getAttributes ();
92
+ OpenTelemetryResourceAttributes attributes = OpenTelemetryResourceAttributes
93
+ .fromEnv (this .environmentVariables ::get );
94
+ attributes .putAll (this .resourceAttributes );
92
95
assertThat (attributes .asMap ()).hasSize (4 )
93
96
.containsEntry ("service.name" , "custom-service" )
94
97
.containsEntry ("service.group" , "custom-group" )
@@ -97,66 +100,67 @@ void resourceAttributesShouldBeMergedWithEnvironmentVariables() {
97
100
}
98
101
99
102
@ Test
100
- void resourceAttributesWithNullKeyOrValueShouldBeIgnored () {
103
+ void resourceAttributesWithBlankKeyAndNullValueShouldBeIgnoredWhenMergingWithEnvironmentVariables () {
101
104
this .resourceAttributes .put ("service.group" , null );
102
105
this .resourceAttributes .put ("service.name" , null );
103
- this .resourceAttributes .put (null , "value" );
106
+ this .resourceAttributes .put (null , "null-key" );
107
+ this .resourceAttributes .put (" " , "blank-key" );
104
108
this .environmentVariables .put ("OTEL_SERVICE_NAME" , "custom-service" );
105
109
this .environmentVariables .put ("OTEL_RESOURCE_ATTRIBUTES" , "key1=value1,key2=value2" );
106
- OpenTelemetryResourceAttributes attributes = getAttributes ();
110
+ OpenTelemetryResourceAttributes attributes = OpenTelemetryResourceAttributes
111
+ .fromEnv (this .environmentVariables ::get );
112
+ attributes .putAll (this .resourceAttributes );
107
113
assertThat (attributes .asMap ()).hasSize (3 )
108
114
.containsEntry ("service.name" , "custom-service" )
109
115
.containsEntry ("key1" , "value1" )
110
116
.containsEntry ("key2" , "value2" );
111
117
}
112
118
113
119
@ Test
114
- @ SuppressWarnings ("unchecked" )
115
- void systemGetEnvShouldBeUsedAsDefaultEnvFunctionAndResourceAttributesAreEmpty () {
116
- OpenTelemetryResourceAttributes attributes = new OpenTelemetryResourceAttributes (null );
117
- assertThat (attributes ).extracting ("resourceAttributes" )
118
- .asInstanceOf (InstanceOfAssertFactories .MAP )
119
- .isNotNull ()
120
- .isEmpty ();
121
- Function <String , String > getEnv = assertThat (attributes ).extracting ("getEnv" )
122
- .asInstanceOf (InstanceOfAssertFactories .type (Function .class ))
123
- .actual ();
124
- System .getenv ().forEach ((key , value ) -> assertThat (getEnv .apply (key )).isEqualTo (value ));
125
- }
126
-
127
- @ Test
128
- void shouldDecodeOtelResourceAttributeValues () {
120
+ void otelResourceAttributesShouldBeDecoded () {
129
121
Stream .generate (this ::generateRandomString ).limit (10000 ).forEach ((value ) -> {
130
122
String key = "key" ;
131
123
this .environmentVariables .put ("OTEL_RESOURCE_ATTRIBUTES" , key + "=" + escaper .escape (value ));
132
- OpenTelemetryResourceAttributes attributes = getAttributes ();
133
- assertThat (attributes .asMap ()).hasSize (1 ).containsEntry (key , value );
124
+ OpenTelemetryResourceAttributes attributes = OpenTelemetryResourceAttributes
125
+ .fromEnv (this .environmentVariables ::get );
126
+ assertThat (attributes .asMap ()).hasSize (1 ).containsEntry (key , value .trim ());
134
127
});
135
128
}
136
129
137
130
@ Test
138
131
void shouldThrowIllegalArgumentExceptionWhenDecodingPercentIllegalHexChar () {
139
132
this .environmentVariables .put ("OTEL_RESOURCE_ATTRIBUTES" , "key=abc%ß" );
140
- assertThatIllegalArgumentException ().isThrownBy (() -> getAttributes ().asMap ())
133
+ assertThatIllegalArgumentException ()
134
+ .isThrownBy (() -> OpenTelemetryResourceAttributes .fromEnv (this .environmentVariables ::get ))
141
135
.withMessage ("Failed to decode percent-encoded characters at index 3 in the value: 'abc%ß'" );
142
136
}
143
137
144
138
@ Test
145
139
void shouldUseReplacementCharWhenDecodingNonUtf8Character () {
146
140
this .environmentVariables .put ("OTEL_RESOURCE_ATTRIBUTES" , "key=%a3%3e" );
147
- OpenTelemetryResourceAttributes attributes = getAttributes ();
141
+ OpenTelemetryResourceAttributes attributes = OpenTelemetryResourceAttributes
142
+ .fromEnv (this .environmentVariables ::get );
148
143
assertThat (attributes .asMap ()).containsEntry ("key" , "\ufffd >" );
149
144
}
150
145
151
146
@ Test
152
147
void shouldThrowIllegalArgumentExceptionWhenDecodingPercent () {
153
148
this .environmentVariables .put ("OTEL_RESOURCE_ATTRIBUTES" , "key=%" );
154
- assertThatIllegalArgumentException ().isThrownBy (() -> getAttributes ().asMap ())
149
+ assertThatIllegalArgumentException ()
150
+ .isThrownBy (() -> OpenTelemetryResourceAttributes .fromEnv (this .environmentVariables ::get ))
155
151
.withMessage ("Failed to decode percent-encoded characters at index 0 in the value: '%'" );
156
152
}
157
153
158
- private OpenTelemetryResourceAttributes getAttributes () {
159
- return new OpenTelemetryResourceAttributes (this .resourceAttributes , this .environmentVariables ::get );
154
+ @ Test
155
+ void shouldPutValueIfKeyIsAbsentAndNotBlankAndValueNotNull () {
156
+ OpenTelemetryResourceAttributes attributes = new OpenTelemetryResourceAttributes ();
157
+ attributes .putIfAbsent ("key" , () -> "value" );
158
+ attributes .putIfAbsent (" key " , () -> "value1" );
159
+ attributes .putIfAbsent (" key1 " , () -> "value1" );
160
+ attributes .putIfAbsent (" key2 " , () -> null );
161
+ attributes .putIfAbsent (null , () -> "value3" );
162
+ attributes .putIfAbsent (" " , () -> "value4" );
163
+ assertThat (attributes .asMap ()).hasSize (2 ).containsEntry ("key" , "value" ).containsEntry ("key1" , "value1" );
160
164
}
161
165
162
166
private String generateRandomString () {
0 commit comments