Skip to content

Commit 045c3e6

Browse files
authored
Promote EnvironmentResourceProvider to public API (#7052)
1 parent 19e964a commit 045c3e6

File tree

6 files changed

+165
-61
lines changed

6 files changed

+165
-61
lines changed
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
11
Comparing source compatibility of opentelemetry-sdk-extension-autoconfigure-1.47.0-SNAPSHOT.jar against opentelemetry-sdk-extension-autoconfigure-1.46.0.jar
2-
No changes.
2+
+++ NEW CLASS: PUBLIC(+) FINAL(+) io.opentelemetry.sdk.autoconfigure.EnvironmentResourceProvider (not serializable)
3+
+++ CLASS FILE FORMAT VERSION: 52.0 <- n.a.
4+
+++ NEW SUPERCLASS: java.lang.Object
5+
+++ NEW CONSTRUCTOR: PUBLIC(+) EnvironmentResourceProvider()
6+
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.sdk.resources.Resource createResource(io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties)
7+
+++ NEW METHOD: PUBLIC(+) int order()

sdk-extensions/autoconfigure/build.gradle.kts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ testing {
6767
targets {
6868
all {
6969
testTask {
70-
environment("OTEL_RESOURCE_ATTRIBUTES", "service.name=test,cat=meow")
70+
environment("OTEL_SERVICE_NAME", "test")
71+
environment("OTEL_RESOURCE_ATTRIBUTES", "cat=meow")
7172
environment("OTEL_PROPAGATORS", "tracecontext,baggage,b3,b3multi,jaeger,ottrace,test")
7273
environment("OTEL_EXPORTER_OTLP_HEADERS", "cat=meow,dog=bark")
7374
environment("OTEL_EXPORTER_OTLP_TIMEOUT", "5000")
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,15 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
package io.opentelemetry.sdk.autoconfigure.internal;
6+
package io.opentelemetry.sdk.autoconfigure;
77

8-
import io.opentelemetry.sdk.autoconfigure.ResourceConfiguration;
98
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
109
import io.opentelemetry.sdk.autoconfigure.spi.ResourceProvider;
1110
import io.opentelemetry.sdk.resources.Resource;
1211

1312
/**
1413
* {@link ResourceProvider} for automatically configuring {@link
1514
* ResourceConfiguration#createEnvironmentResource(ConfigProperties)}.
16-
*
17-
* <p>This class is internal and is hence not for public use. Its APIs are unstable and can change
18-
* at any time.
1915
*/
2016
public final class EnvironmentResourceProvider implements ResourceProvider {
2117
@Override

sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/ResourceConfiguration.java

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import java.util.Map;
2626
import java.util.Set;
2727
import java.util.function.BiFunction;
28+
import java.util.logging.Level;
29+
import java.util.logging.Logger;
2830

2931
/**
3032
* Auto-configuration for the OpenTelemetry {@link Resource}.
@@ -33,12 +35,21 @@
3335
*/
3436
public final class ResourceConfiguration {
3537

38+
private static final Logger logger = Logger.getLogger(ResourceConfiguration.class.getName());
39+
3640
private static final AttributeKey<String> SERVICE_NAME = AttributeKey.stringKey("service.name");
3741

3842
// Visible for testing
3943
static final String ATTRIBUTE_PROPERTY = "otel.resource.attributes";
4044
static final String SERVICE_NAME_PROPERTY = "otel.service.name";
4145
static final String DISABLED_ATTRIBUTE_KEYS = "otel.resource.disabled.keys";
46+
static final String ENABLED_RESOURCE_PROVIDERS = "otel.java.enabled.resource.providers";
47+
static final String DISABLED_RESOURCE_PROVIDERS = "otel.java.disabled.resource.providers";
48+
49+
private static final String OLD_ENVIRONMENT_DETECTOR_FQCN =
50+
"io.opentelemetry.sdk.autoconfigure.internal.EnvironmentResourceProvider";
51+
private static final String NEW_ENVIRONMENT_DETECT_FQCN =
52+
EnvironmentResourceProvider.class.getName();
4253

4354
/**
4455
* Create a {@link Resource} from the environment. The resource contains attributes parsed from
@@ -88,10 +99,34 @@ static Resource configureResource(
8899
BiFunction<? super Resource, ConfigProperties, ? extends Resource> resourceCustomizer) {
89100
Resource result = Resource.getDefault();
90101

91-
Set<String> enabledProviders =
92-
new HashSet<>(config.getList("otel.java.enabled.resource.providers"));
93-
Set<String> disabledProviders =
94-
new HashSet<>(config.getList("otel.java.disabled.resource.providers"));
102+
Set<String> enabledProviders = new HashSet<>(config.getList(ENABLED_RESOURCE_PROVIDERS));
103+
if (enabledProviders.remove(OLD_ENVIRONMENT_DETECTOR_FQCN)) {
104+
logger.log(
105+
Level.WARNING,
106+
"Found reference to "
107+
+ OLD_ENVIRONMENT_DETECTOR_FQCN
108+
+ " in "
109+
+ ENABLED_RESOURCE_PROVIDERS
110+
+ ". Please update to "
111+
+ NEW_ENVIRONMENT_DETECT_FQCN
112+
+ ". Support for the old provider name will be removed after 1.49.0.");
113+
enabledProviders.add(NEW_ENVIRONMENT_DETECT_FQCN);
114+
}
115+
116+
Set<String> disabledProviders = new HashSet<>(config.getList(DISABLED_RESOURCE_PROVIDERS));
117+
if (disabledProviders.remove(OLD_ENVIRONMENT_DETECTOR_FQCN)) {
118+
logger.log(
119+
Level.WARNING,
120+
"Found reference to "
121+
+ OLD_ENVIRONMENT_DETECTOR_FQCN
122+
+ " in "
123+
+ DISABLED_RESOURCE_PROVIDERS
124+
+ ". Please update to "
125+
+ NEW_ENVIRONMENT_DETECT_FQCN
126+
+ ". Support for the old provider name will be removed after 1.49.0.");
127+
disabledProviders.add(NEW_ENVIRONMENT_DETECT_FQCN);
128+
}
129+
95130
for (ResourceProvider resourceProvider : spiHelper.loadOrdered(ResourceProvider.class)) {
96131
if (!enabledProviders.isEmpty()
97132
&& !enabledProviders.contains(resourceProvider.getClass().getName())) {
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
io.opentelemetry.sdk.autoconfigure.internal.EnvironmentResourceProvider
1+
io.opentelemetry.sdk.autoconfigure.EnvironmentResourceProvider

sdk-extensions/autoconfigure/src/testFullConfig/java/io/opentelemetry/sdk/autoconfigure/ResourceConfigurationTest.java

Lines changed: 116 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,25 @@
1111
import io.opentelemetry.api.common.Attributes;
1212
import io.opentelemetry.sdk.autoconfigure.internal.SpiHelper;
1313
import io.opentelemetry.sdk.autoconfigure.spi.internal.DefaultConfigProperties;
14+
import io.opentelemetry.sdk.testing.assertj.AttributesAssert;
1415
import java.net.URL;
1516
import java.net.URLClassLoader;
1617
import java.util.Collections;
1718
import java.util.HashMap;
1819
import java.util.Map;
20+
import java.util.function.Consumer;
21+
import java.util.stream.Stream;
22+
import javax.annotation.Nullable;
1923
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;
2027

2128
class ResourceConfigurationTest {
2229

2330
private final SpiHelper spiHelper =
2431
SpiHelper.create(ResourceConfigurationTest.class.getClassLoader());
2532

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-
3733
@Test
3834
void configureResource_EmptyClassLoader() {
3935
Attributes attributes =
@@ -43,55 +39,126 @@ void configureResource_EmptyClassLoader() {
4339
(r, c) -> r)
4440
.getAttributes();
4541

42+
assertThat(attributes.get(AttributeKey.stringKey("service.name")))
43+
.isEqualTo("unknown_service:java");
44+
assertThat(attributes.get(AttributeKey.stringKey("cat"))).isNull();
4645
assertThat(attributes.get(AttributeKey.stringKey("animal"))).isNull();
4746
assertThat(attributes.get(AttributeKey.stringKey("color"))).isNull();
4847
}
4948

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+
}
5665
Attributes attributes =
5766
ResourceConfiguration.configureResource(
58-
DefaultConfigProperties.create(customConfigs), spiHelper, (r, c) -> r)
67+
DefaultConfigProperties.create(config), spiHelper, (r, c) -> r)
5968
.getAttributes();
6069

61-
assertThat(attributes.get(AttributeKey.stringKey("animal"))).isEqualTo("cat");
62-
assertThat(attributes.get(AttributeKey.stringKey("color"))).isNull();
70+
attributeAssertion.accept(assertThat(attributes));
6371
}
6472

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"))));
81158
}
82159

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;
96163
}
97164
}

0 commit comments

Comments
 (0)