11
11
import io .opentelemetry .api .common .Attributes ;
12
12
import io .opentelemetry .sdk .autoconfigure .internal .SpiHelper ;
13
13
import io .opentelemetry .sdk .autoconfigure .spi .internal .DefaultConfigProperties ;
14
+ import io .opentelemetry .sdk .testing .assertj .AttributesAssert ;
14
15
import java .net .URL ;
15
16
import java .net .URLClassLoader ;
16
17
import java .util .Collections ;
17
18
import java .util .HashMap ;
18
19
import java .util .Map ;
20
+ import java .util .function .Consumer ;
21
+ import java .util .stream .Stream ;
22
+ import javax .annotation .Nullable ;
19
23
import org .junit .jupiter .api .Test ;
24
+ import org .junit .jupiter .params .ParameterizedTest ;
25
+ import org .junit .jupiter .params .provider .Arguments ;
26
+ import org .junit .jupiter .params .provider .MethodSource ;
20
27
21
28
class ResourceConfigurationTest {
22
29
23
30
private final SpiHelper spiHelper =
24
31
SpiHelper .create (ResourceConfigurationTest .class .getClassLoader ());
25
32
26
- @ Test
27
- void configureResource () {
28
- Attributes attributes =
29
- ResourceConfiguration .configureResource (
30
- DefaultConfigProperties .create (Collections .emptyMap ()), spiHelper , (r , c ) -> r )
31
- .getAttributes ();
32
-
33
- assertThat (attributes .get (AttributeKey .stringKey ("animal" ))).isNotNull ();
34
- assertThat (attributes .get (AttributeKey .stringKey ("color" ))).isNotNull ();
35
- }
36
-
37
33
@ Test
38
34
void configureResource_EmptyClassLoader () {
39
35
Attributes attributes =
@@ -43,55 +39,126 @@ void configureResource_EmptyClassLoader() {
43
39
(r , c ) -> r )
44
40
.getAttributes ();
45
41
42
+ assertThat (attributes .get (AttributeKey .stringKey ("service.name" )))
43
+ .isEqualTo ("unknown_service:java" );
44
+ assertThat (attributes .get (AttributeKey .stringKey ("cat" ))).isNull ();
46
45
assertThat (attributes .get (AttributeKey .stringKey ("animal" ))).isNull ();
47
46
assertThat (attributes .get (AttributeKey .stringKey ("color" ))).isNull ();
48
47
}
49
48
50
- @ Test
51
- void configureResource_OnlyEnabled () {
52
- Map <String , String > customConfigs = new HashMap <>(1 );
53
- customConfigs .put (
54
- "otel.java.enabled.resource.providers" ,
55
- "io.opentelemetry.sdk.autoconfigure.provider.TestAnimalResourceProvider" );
49
+ @ ParameterizedTest
50
+ @ MethodSource ("configureResourceArgs" )
51
+ void configureResource (
52
+ @ Nullable String enabledProviders ,
53
+ @ Nullable String disabledProviders ,
54
+ Consumer <AttributesAssert > attributeAssertion ) {
55
+ // build.gradle.kts sets:
56
+ // OTEL_SERVICE_NAME=test
57
+ // OTEL_RESOURCE_ATTRIBUTES=cat=meow
58
+ Map <String , String > config = new HashMap <>();
59
+ if (enabledProviders != null ) {
60
+ config .put ("otel.java.enabled.resource.providers" , enabledProviders );
61
+ }
62
+ if (disabledProviders != null ) {
63
+ config .put ("otel.java.disabled.resource.providers" , disabledProviders );
64
+ }
56
65
Attributes attributes =
57
66
ResourceConfiguration .configureResource (
58
- DefaultConfigProperties .create (customConfigs ), spiHelper , (r , c ) -> r )
67
+ DefaultConfigProperties .create (config ), spiHelper , (r , c ) -> r )
59
68
.getAttributes ();
60
69
61
- assertThat (attributes .get (AttributeKey .stringKey ("animal" ))).isEqualTo ("cat" );
62
- assertThat (attributes .get (AttributeKey .stringKey ("color" ))).isNull ();
70
+ attributeAssertion .accept (assertThat (attributes ));
63
71
}
64
72
65
- @ Test
66
- void configureResource_EnabledAndDisabled () {
67
- Map <String , String > customConfigs = new HashMap <>(2 );
68
- customConfigs .put (
69
- "otel.java.enabled.resource.providers" ,
70
- "io.opentelemetry.sdk.autoconfigure.provider.TestAnimalResourceProvider" );
71
- customConfigs .put (
72
- "otel.java.disabled.resource.providers" ,
73
- "io.opentelemetry.sdk.extension.resources.TestColorResourceProvider" );
74
- Attributes attributes =
75
- ResourceConfiguration .configureResource (
76
- DefaultConfigProperties .create (customConfigs ), spiHelper , (r , c ) -> r )
77
- .getAttributes ();
78
-
79
- assertThat (attributes .get (AttributeKey .stringKey ("animal" ))).isEqualTo ("cat" );
80
- assertThat (attributes .get (AttributeKey .stringKey ("color" ))).isNull ();
73
+ private static Stream <Arguments > configureResourceArgs () {
74
+ return Stream .of (
75
+ // default
76
+ Arguments .of (
77
+ null ,
78
+ null ,
79
+ attributeConsumer (
80
+ attr -> attr .containsEntry ("service.name" , "test" ).containsEntry ("cat" , "meow" ))),
81
+ // only enabled
82
+ Arguments .of (
83
+ "io.opentelemetry.sdk.autoconfigure.provider.TestAnimalResourceProvider" ,
84
+ null ,
85
+ attributeConsumer (
86
+ attr ->
87
+ attr .containsEntry ("service.name" , "unknown_service:java" )
88
+ .doesNotContainKey ("cat" )
89
+ .containsEntry ("animal" , "cat" )
90
+ .doesNotContainKey ("color" ))),
91
+ // only disabled
92
+ Arguments .of (
93
+ null ,
94
+ "io.opentelemetry.sdk.autoconfigure.provider.TestColorResourceProvider" ,
95
+ attributeConsumer (
96
+ attr ->
97
+ attr .containsEntry ("service.name" , "test" )
98
+ .containsEntry ("cat" , "meow" )
99
+ .containsEntry ("animal" , "cat" )
100
+ .doesNotContainKey ("color" ))),
101
+ // enabled and disabled
102
+ Arguments .of (
103
+ "io.opentelemetry.sdk.autoconfigure.provider.TestAnimalResourceProvider" ,
104
+ "io.opentelemetry.sdk.autoconfigure.provider.TestColorResourceProvider" ,
105
+ attributeConsumer (
106
+ attr ->
107
+ attr .containsEntry ("service.name" , "unknown_service:java" )
108
+ .doesNotContainKey ("cat" )
109
+ .containsEntry ("animal" , "cat" )
110
+ .doesNotContainKey ("color" ))),
111
+ Arguments .of (
112
+ "io.opentelemetry.sdk.autoconfigure.provider.TestAnimalResourceProvider" ,
113
+ "io.opentelemetry.sdk.autoconfigure.provider.TestColorResourceProvider,io.opentelemetry.sdk.autoconfigure.provider.TestAnimalResourceProvider" ,
114
+ attributeConsumer (
115
+ attr ->
116
+ attr .containsEntry ("service.name" , "unknown_service:java" )
117
+ .doesNotContainKey ("cat" )
118
+ .doesNotContainKey ("animal" )
119
+ .doesNotContainKey ("color" ))),
120
+ // environment resource provider
121
+ Arguments .of (
122
+ "io.opentelemetry.sdk.autoconfigure.EnvironmentResourceProvider" ,
123
+ null ,
124
+ attributeConsumer (
125
+ attr ->
126
+ attr .containsEntry ("service.name" , "test" )
127
+ .containsEntry ("cat" , "meow" )
128
+ .doesNotContainKey ("animal" )
129
+ .doesNotContainKey ("color" ))),
130
+ Arguments .of (
131
+ null ,
132
+ "io.opentelemetry.sdk.autoconfigure.EnvironmentResourceProvider" ,
133
+ attributeConsumer (
134
+ attr ->
135
+ attr .containsEntry ("service.name" , "unknown_service:java" )
136
+ .doesNotContainKey ("cat" )
137
+ .containsEntry ("animal" , "cat" )
138
+ .containsEntry ("color" , "blue" ))),
139
+ // old environment resource provider FQCN
140
+ Arguments .of (
141
+ "io.opentelemetry.sdk.autoconfigure.internal.EnvironmentResourceProvider" ,
142
+ null ,
143
+ attributeConsumer (
144
+ attr ->
145
+ attr .containsEntry ("service.name" , "test" )
146
+ .containsEntry ("cat" , "meow" )
147
+ .doesNotContainKey ("animal" )
148
+ .doesNotContainKey ("color" ))),
149
+ Arguments .of (
150
+ null ,
151
+ "io.opentelemetry.sdk.autoconfigure.internal.EnvironmentResourceProvider" ,
152
+ attributeConsumer (
153
+ attr ->
154
+ attr .containsEntry ("service.name" , "unknown_service:java" )
155
+ .doesNotContainKey ("cat" )
156
+ .containsEntry ("animal" , "cat" )
157
+ .containsEntry ("color" , "blue" ))));
81
158
}
82
159
83
- @ Test
84
- void configureResource_OnlyDisabled () {
85
- Map <String , String > customConfigs = new HashMap <>(1 );
86
- customConfigs .put (
87
- "otel.java.disabled.resource.providers" ,
88
- "io.opentelemetry.sdk.autoconfigure.provider.TestColorResourceProvider" );
89
- Attributes attributes =
90
- ResourceConfiguration .configureResource (
91
- DefaultConfigProperties .create (customConfigs ), spiHelper , (r , c ) -> r )
92
- .getAttributes ();
93
-
94
- assertThat (attributes .get (AttributeKey .stringKey ("animal" ))).isEqualTo ("cat" );
95
- assertThat (attributes .get (AttributeKey .stringKey ("color" ))).isNull ();
160
+ private static Consumer <AttributesAssert > attributeConsumer (
161
+ Consumer <AttributesAssert > attributesAssertConsumer ) {
162
+ return attributesAssertConsumer ;
96
163
}
97
164
}
0 commit comments