diff --git a/sdk-extensions/autoconfigure-spi/src/main/java/io/opentelemetry/sdk/autoconfigure/spi/internal/ResourceDetector.java b/sdk-extensions/autoconfigure-spi/src/main/java/io/opentelemetry/sdk/autoconfigure/spi/internal/ResourceDetector.java new file mode 100644 index 00000000000..4529356e57d --- /dev/null +++ b/sdk-extensions/autoconfigure-spi/src/main/java/io/opentelemetry/sdk/autoconfigure/spi/internal/ResourceDetector.java @@ -0,0 +1,51 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.sdk.autoconfigure.spi.internal; + +import io.opentelemetry.api.common.AttributeKey; +import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; +import io.opentelemetry.sdk.autoconfigure.spi.Ordered; +import java.util.Optional; +import java.util.function.Function; + +public interface ResourceDetector extends Ordered { + /** Read the data for the resource attributes. */ + Optional readData(ConfigProperties config); + + /** Registers the attributes that this resource detector can provide. */ + void registerAttributes(Builder builder); + + /** Greater order means lower priority. The default order is 0. */ + @Override + default int order() { + return 0; + } + + /** Returns the name of this resource detector. */ + String name(); + + /** + * Returns whether this resource detector is enabled by default. If not, it will only be used if + * explicitly enabled in the configuration. + */ + default boolean defaultEnabled() { + return true; + } + + /** A builder for registering attributes that a resource detector can provide. */ + interface Builder { + /** + * Adds an attribute to the resource. + * + * @param key the attribute key + * @param getter a function that returns the value of the attribute from the data that is read + * by {@link ResourceDetector#readData(ConfigProperties)} + * @return this builder + * @param the type of the attribute + */ + Builder add(AttributeKey key, Function> getter); + } +} diff --git a/sdk-extensions/autoconfigure-spi/src/test/java/io/opentelemetry/sdk/autoconfigure/spi/internal/ConfigPropertiesTest.java b/sdk-extensions/autoconfigure-spi/src/test/java/io/opentelemetry/sdk/autoconfigure/spi/internal/ConfigPropertiesTest.java index e0fe198dc9e..14235a6e4a4 100644 --- a/sdk-extensions/autoconfigure-spi/src/test/java/io/opentelemetry/sdk/autoconfigure/spi/internal/ConfigPropertiesTest.java +++ b/sdk-extensions/autoconfigure-spi/src/test/java/io/opentelemetry/sdk/autoconfigure/spi/internal/ConfigPropertiesTest.java @@ -230,7 +230,7 @@ void defaultMethodsDelegate() { expectedMap.put("bear", "growl"); Map map = makeTestProps(); - ConfigProperties properties = DefaultConfigProperties.create(map); + ConfigProperties properties = DefaultConfigProperties.createFromMap(map); assertThat(properties.getBoolean("test.boolean", false)).isTrue(); assertThat(properties.getString("test.string", "nah")).isEqualTo("str"); assertThat(properties.getDouble("test.double", 65.535)).isEqualTo(5.4); @@ -244,7 +244,7 @@ void defaultMethodsDelegate() { @Test void defaultMethodsFallBack() { - ConfigProperties properties = DefaultConfigProperties.create(emptyMap()); + ConfigProperties properties = DefaultConfigProperties.createFromMap(emptyMap()); assertThat(properties.getBoolean("foo", true)).isTrue(); assertThat(properties.getString("foo", "bar")).isEqualTo("bar"); assertThat(properties.getDouble("foo", 65.535)).isEqualTo(65.535); @@ -255,7 +255,7 @@ void defaultMethodsFallBack() { @Test void defaultCollectionTypes() { - ConfigProperties properties = DefaultConfigProperties.create(emptyMap()); + ConfigProperties properties = DefaultConfigProperties.createFromMap(emptyMap()); assertThat(properties.getList("foo", Arrays.asList("1", "2", "3"))) .containsExactly("1", "2", "3"); assertThat(properties.getList("foo")).isEmpty(); diff --git a/sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/ResourceConfiguration.java b/sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/ResourceConfiguration.java index 010d219acba..5431adce918 100644 --- a/sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/ResourceConfiguration.java +++ b/sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/ResourceConfiguration.java @@ -8,22 +8,28 @@ import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.api.common.Attributes; import io.opentelemetry.api.common.AttributesBuilder; +import io.opentelemetry.sdk.autoconfigure.internal.ResourceDetectorReader; import io.opentelemetry.sdk.autoconfigure.internal.SpiHelper; import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; import io.opentelemetry.sdk.autoconfigure.spi.ConfigurationException; +import io.opentelemetry.sdk.autoconfigure.spi.Ordered; import io.opentelemetry.sdk.autoconfigure.spi.ResourceProvider; import io.opentelemetry.sdk.autoconfigure.spi.internal.ConditionalResourceProvider; import io.opentelemetry.sdk.autoconfigure.spi.internal.DefaultConfigProperties; +import io.opentelemetry.sdk.autoconfigure.spi.internal.ResourceDetector; import io.opentelemetry.sdk.resources.Resource; import io.opentelemetry.sdk.resources.ResourceBuilder; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.nio.charset.StandardCharsets; import java.util.Collections; +import java.util.Comparator; import java.util.HashSet; +import java.util.List; import java.util.Map; import java.util.Set; import java.util.function.BiFunction; +import javax.annotation.Nullable; /** * Auto-configuration for the OpenTelemetry {@link Resource}. @@ -81,6 +87,7 @@ public static Resource createEnvironmentResource(ConfigProperties config) { return Resource.create(resourceAttributes.build()); } + @SuppressWarnings({"rawtypes", "unchecked"}) static Resource configureResource( ConfigProperties config, SpiHelper spiHelper, @@ -91,19 +98,45 @@ static Resource configureResource( new HashSet<>(config.getList("otel.java.enabled.resource.providers")); Set disabledProviders = new HashSet<>(config.getList("otel.java.disabled.resource.providers")); - for (ResourceProvider resourceProvider : spiHelper.loadOrdered(ResourceProvider.class)) { - if (!enabledProviders.isEmpty() - && !enabledProviders.contains(resourceProvider.getClass().getName())) { - continue; + List providers = (List) spiHelper.load(ResourceProvider.class); + providers.addAll(spiHelper.load(ResourceDetector.class)); + providers.sort(Comparator.comparingInt(Ordered::order)); + for (Ordered ordered : providers) { + if (ordered instanceof ResourceProvider) { + ResourceProvider provider = (ResourceProvider) ordered; + + if (!isEnabled( + provider.getClass().getName(), + enabledProviders, + disabledProviders, + /* defaultEnabled= */ true, + /* explicitEnabled= */ null)) { + continue; + } + if (provider instanceof ConditionalResourceProvider + && !((ConditionalResourceProvider) provider).shouldApply(config, result)) { + continue; + } + result = result.merge(provider.createResource(config)); + } else { + ResourceDetector detector = (ResourceDetector) ordered; + + Boolean explictEnabled = + config.getBoolean(String.format("otel.resource.provider.%s.enabled", detector.name())); + if (!isEnabled( + detector.getClass().getName(), + enabledProviders, + disabledProviders, + detector.defaultEnabled(), + explictEnabled)) { + continue; + } + + ResourceDetectorReader reader = new ResourceDetectorReader<>(detector); + if (reader.shouldApply(config, result)) { + result = result.merge(reader.createResource(config, result)); + } } - if (disabledProviders.contains(resourceProvider.getClass().getName())) { - continue; - } - if (resourceProvider instanceof ConditionalResourceProvider - && !((ConditionalResourceProvider) resourceProvider).shouldApply(config, result)) { - continue; - } - result = result.merge(resourceProvider.createResource(config)); } result = filterAttributes(result, config); @@ -111,6 +144,24 @@ static Resource configureResource( return resourceCustomizer.apply(result, config); } + static boolean isEnabled( + String className, + Set enabledProviders, + Set disabledProviders, + boolean defaultEnabled, + @Nullable Boolean explicitEnabled) { + if (explicitEnabled != null) { + return explicitEnabled; + } + if (!enabledProviders.isEmpty() && !enabledProviders.contains(className)) { + return false; + } + if (disabledProviders.contains(className)) { + return false; + } + return defaultEnabled; + } + // visible for testing static Resource filterAttributes(Resource resource, ConfigProperties configProperties) { Set disabledKeys = new HashSet<>(configProperties.getList(DISABLED_ATTRIBUTE_KEYS)); diff --git a/sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/internal/ResourceDetectorReader.java b/sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/internal/ResourceDetectorReader.java new file mode 100644 index 00000000000..35cb72eacdb --- /dev/null +++ b/sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/internal/ResourceDetectorReader.java @@ -0,0 +1,94 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.sdk.autoconfigure.internal; + +import io.opentelemetry.api.common.AttributeKey; +import io.opentelemetry.api.common.Attributes; +import io.opentelemetry.api.common.AttributesBuilder; +import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; +import io.opentelemetry.sdk.autoconfigure.spi.internal.ResourceDetector; +import io.opentelemetry.sdk.resources.Resource; +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.function.Function; + +@SuppressWarnings({"unchecked", "rawtypes"}) +public final class ResourceDetectorReader { + + private final ResourceDetector resourceDetector; + + public class AttributeBuilder implements ResourceDetector.Builder { + + private AttributeBuilder() {} + + @Override + public AttributeBuilder add(AttributeKey key, Function> getter) { + attributeGetters.put((AttributeKey) key, Objects.requireNonNull((Function) getter)); + return this; + } + } + + private final Map, Function>> attributeGetters = + new HashMap<>(); + + public ResourceDetectorReader(ResourceDetector resourceDetector) { + this.resourceDetector = resourceDetector; + resourceDetector.registerAttributes(new AttributeBuilder()); + } + + public boolean shouldApply(ConfigProperties config, Resource existing) { + Map resourceAttributes = getResourceAttributes(config); + return attributeGetters.keySet().stream() + .allMatch(key -> shouldUpdate(config, existing, key, resourceAttributes)); + } + + public Resource createResource(ConfigProperties config, Resource existing) { + return resourceDetector + .readData(config) + .map( + data -> { + Map resourceAttributes = getResourceAttributes(config); + AttributesBuilder builder = Attributes.builder(); + attributeGetters.entrySet().stream() + .filter(e -> shouldUpdate(config, existing, e.getKey(), resourceAttributes)) + .forEach( + e -> + e.getValue() + .apply(data) + .ifPresent(value -> putAttribute(builder, e.getKey(), value))); + return Resource.create(builder.build()); + }) + .orElse(Resource.empty()); + } + + private static void putAttribute(AttributesBuilder builder, AttributeKey key, T value) { + builder.put(key, value); + } + + private static Map getResourceAttributes(ConfigProperties config) { + return config.getMap("otel.resource.attributes"); + } + + private static boolean shouldUpdate( + ConfigProperties config, + Resource existing, + AttributeKey key, + Map resourceAttributes) { + if (resourceAttributes.containsKey(key.getKey())) { + return false; + } + + Object value = existing.getAttribute(key); + + if (key.getKey().equals("service.name")) { + return config.getString("otel.service.name") == null && "unknown_service:java".equals(value); + } + + return value == null; + } +} diff --git a/sdk-extensions/autoconfigure/src/test/java/io/opentelemetry/sdk/autoconfigure/ResourceConfigurationTest.java b/sdk-extensions/autoconfigure/src/test/java/io/opentelemetry/sdk/autoconfigure/ResourceConfigurationTest.java index 33cc9740639..a758f486983 100644 --- a/sdk-extensions/autoconfigure/src/test/java/io/opentelemetry/sdk/autoconfigure/ResourceConfigurationTest.java +++ b/sdk-extensions/autoconfigure/src/test/java/io/opentelemetry/sdk/autoconfigure/ResourceConfigurationTest.java @@ -16,9 +16,15 @@ import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; import io.opentelemetry.sdk.autoconfigure.spi.internal.DefaultConfigProperties; import io.opentelemetry.sdk.resources.Resource; +import java.util.Collections; import java.util.HashMap; import java.util.Map; +import java.util.Set; +import java.util.stream.Stream; +import javax.annotation.Nullable; +import org.junit.jupiter.api.DynamicTest; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestFactory; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.junit.jupiter.MockitoExtension; @@ -35,7 +41,7 @@ void customConfigResource() { assertThat( ResourceConfiguration.configureResource( - DefaultConfigProperties.create(props), + DefaultConfigProperties.createFromMap(props), SpiHelper.create(ResourceConfigurationTest.class.getClassLoader()), (r, c) -> r)) .isEqualTo( @@ -137,4 +143,115 @@ void filterAttributes() { assertThat(resource.getAttributes().get(stringKey("bar"))).isNull(); }); } + + private static class EnabledTestCase { + private final String name; + private final boolean result; + private final String className; + private final Set enabledProviders; + private final Set disabledProviders; + private final boolean defaultEnabled; + private final Boolean explicitEnabled; + + private EnabledTestCase( + String name, + boolean result, + String className, + Set enabledProviders, + Set disabledProviders, + boolean defaultEnabled, + @Nullable Boolean explicitEnabled) { + this.name = name; + this.result = result; + this.className = className; + this.enabledProviders = enabledProviders; + this.disabledProviders = disabledProviders; + this.defaultEnabled = defaultEnabled; + this.explicitEnabled = explicitEnabled; + } + } + + @SuppressWarnings("BooleanParameter") + @TestFactory + Stream enabledTestCases() { + return Stream.of( + new EnabledTestCase( + "explicitEnabled", + true, + "className", + Collections.emptySet(), + Collections.emptySet(), + true, + true), + new EnabledTestCase( + "explicitEnabledFalse", + false, + "className", + Collections.emptySet(), + Collections.emptySet(), + true, + false), + new EnabledTestCase( + "enabledProvidersEmpty", + true, + "className", + Collections.emptySet(), + Collections.emptySet(), + true, + null), + new EnabledTestCase( + "enabledProvidersContains", + true, + "className", + Collections.singleton("className"), + Collections.emptySet(), + true, + null), + new EnabledTestCase( + "enabledProvidersNotContains", + false, + "className", + Collections.singleton("otherClassName"), + Collections.emptySet(), + true, + null), + new EnabledTestCase( + "disabledProvidersContains", + false, + "className", + Collections.emptySet(), + Collections.singleton("className"), + true, + null), + new EnabledTestCase( + "disabledProvidersNotContains", + true, + "className", + Collections.emptySet(), + Collections.singleton("otherClassName"), + true, + null), + new EnabledTestCase( + "defaultEnabledFalse", + false, + "className", + Collections.emptySet(), + Collections.emptySet(), + false, + null)) + .map( + tc -> + DynamicTest.dynamicTest( + tc.name, + () -> { + assertThat( + ResourceConfiguration.isEnabled( + tc.className, + tc.enabledProviders, + tc.disabledProviders, + tc.defaultEnabled, + tc.explicitEnabled)) + .isEqualTo(tc.result); + })); + } } diff --git a/sdk-extensions/autoconfigure/src/testFullConfig/java/io/opentelemetry/sdk/autoconfigure/ResourceConfigurationTest.java b/sdk-extensions/autoconfigure/src/testFullConfig/java/io/opentelemetry/sdk/autoconfigure/ResourceConfigurationFullTest.java similarity index 53% rename from sdk-extensions/autoconfigure/src/testFullConfig/java/io/opentelemetry/sdk/autoconfigure/ResourceConfigurationTest.java rename to sdk-extensions/autoconfigure/src/testFullConfig/java/io/opentelemetry/sdk/autoconfigure/ResourceConfigurationFullTest.java index 722d80b8704..ff6ffde09ba 100644 --- a/sdk-extensions/autoconfigure/src/testFullConfig/java/io/opentelemetry/sdk/autoconfigure/ResourceConfigurationTest.java +++ b/sdk-extensions/autoconfigure/src/testFullConfig/java/io/opentelemetry/sdk/autoconfigure/ResourceConfigurationFullTest.java @@ -18,27 +18,25 @@ import java.util.Map; import org.junit.jupiter.api.Test; -class ResourceConfigurationTest { +class ResourceConfigurationFullTest { private final SpiHelper spiHelper = - SpiHelper.create(ResourceConfigurationTest.class.getClassLoader()); + SpiHelper.create(ResourceConfigurationFullTest.class.getClassLoader()); @Test - void configureResource() { - Attributes attributes = - ResourceConfiguration.configureResource( - DefaultConfigProperties.create(Collections.emptyMap()), spiHelper, (r, c) -> r) - .getAttributes(); + void configureResource_Default() { + Attributes attributes = configureResource(Collections.emptyMap()); - assertThat(attributes.get(AttributeKey.stringKey("animal"))).isNotNull(); - assertThat(attributes.get(AttributeKey.stringKey("color"))).isNotNull(); + assertThat(attributes.get(AttributeKey.stringKey("animal"))).isEqualTo("cat"); + assertThat(attributes.get(AttributeKey.stringKey("color"))).isEqualTo("blue"); + assertThat(attributes.get(AttributeKey.stringKey("service.name"))).isEqualTo("cart"); } @Test void configureResource_EmptyClassLoader() { Attributes attributes = ResourceConfiguration.configureResource( - DefaultConfigProperties.create(Collections.emptyMap()), + DefaultConfigProperties.createFromMap(Collections.emptyMap()), SpiHelper.create(new URLClassLoader(new URL[0], null)), (r, c) -> r) .getAttributes(); @@ -53,10 +51,7 @@ void configureResource_OnlyEnabled() { customConfigs.put( "otel.java.enabled.resource.providers", "io.opentelemetry.sdk.autoconfigure.provider.TestAnimalResourceProvider"); - Attributes attributes = - ResourceConfiguration.configureResource( - DefaultConfigProperties.create(customConfigs), spiHelper, (r, c) -> r) - .getAttributes(); + Attributes attributes = configureResource(customConfigs); assertThat(attributes.get(AttributeKey.stringKey("animal"))).isEqualTo("cat"); assertThat(attributes.get(AttributeKey.stringKey("color"))).isNull(); @@ -68,13 +63,8 @@ void configureResource_EnabledAndDisabled() { customConfigs.put( "otel.java.enabled.resource.providers", "io.opentelemetry.sdk.autoconfigure.provider.TestAnimalResourceProvider"); - customConfigs.put( - "otel.java.disabled.resource.providers", - "io.opentelemetry.sdk.extension.resources.TestColorResourceProvider"); - Attributes attributes = - ResourceConfiguration.configureResource( - DefaultConfigProperties.create(customConfigs), spiHelper, (r, c) -> r) - .getAttributes(); + customConfigs.put("otel.resource.provider.color.enabled", "false"); + Attributes attributes = configureResource(customConfigs); assertThat(attributes.get(AttributeKey.stringKey("animal"))).isEqualTo("cat"); assertThat(attributes.get(AttributeKey.stringKey("color"))).isNull(); @@ -83,15 +73,43 @@ void configureResource_EnabledAndDisabled() { @Test void configureResource_OnlyDisabled() { Map customConfigs = new HashMap<>(1); - customConfigs.put( - "otel.java.disabled.resource.providers", - "io.opentelemetry.sdk.autoconfigure.provider.TestColorResourceProvider"); - Attributes attributes = - ResourceConfiguration.configureResource( - DefaultConfigProperties.create(customConfigs), spiHelper, (r, c) -> r) - .getAttributes(); + customConfigs.put("otel.resource.provider.color.enabled", "false"); + Attributes attributes = configureResource(customConfigs); assertThat(attributes.get(AttributeKey.stringKey("animal"))).isEqualTo("cat"); assertThat(attributes.get(AttributeKey.stringKey("color"))).isNull(); } + + @Test + void configureResource_UserConfiguredService() { + Map customConfigs = new HashMap<>(1); + customConfigs.put("otel.resource.attributes", "service.name=user"); + Attributes attributes = configureResource(customConfigs); + + assertThat(attributes.get(AttributeKey.stringKey("service.name"))).isEqualTo("user"); + } + + @Test + void configureResource_UserConfiguredServiceUsingService() { + Map customConfigs = new HashMap<>(1); + customConfigs.put("otel.service.name", "user"); + Attributes attributes = configureResource(customConfigs); + + assertThat(attributes.get(AttributeKey.stringKey("service.name"))).isEqualTo("user"); + } + + @Test + void configureResource_UserConfiguredColor() { + Map customConfigs = new HashMap<>(1); + customConfigs.put("otel.resource.attributes", "color=red"); + Attributes attributes = configureResource(customConfigs); + + assertThat(attributes.get(AttributeKey.stringKey("color"))).isEqualTo("red"); + } + + private Attributes configureResource(Map customConfigs) { + return ResourceConfiguration.configureResource( + DefaultConfigProperties.createFromMap(customConfigs), spiHelper, (r, c) -> r) + .getAttributes(); + } } diff --git a/sdk-extensions/autoconfigure/src/testFullConfig/java/io/opentelemetry/sdk/autoconfigure/provider/TestColorResourceDetector.java b/sdk-extensions/autoconfigure/src/testFullConfig/java/io/opentelemetry/sdk/autoconfigure/provider/TestColorResourceDetector.java new file mode 100644 index 00000000000..0daa6f34262 --- /dev/null +++ b/sdk-extensions/autoconfigure/src/testFullConfig/java/io/opentelemetry/sdk/autoconfigure/provider/TestColorResourceDetector.java @@ -0,0 +1,29 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.sdk.autoconfigure.provider; + +import io.opentelemetry.api.common.AttributeKey; +import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; +import io.opentelemetry.sdk.autoconfigure.spi.internal.ResourceDetector; +import java.util.Optional; + +public class TestColorResourceDetector implements ResourceDetector { + + @Override + public Optional readData(ConfigProperties config) { + return Optional.of("blue"); + } + + @Override + public void registerAttributes(Builder builder) { + builder.add(AttributeKey.stringKey("color"), Optional::of); + } + + @Override + public String name() { + return "color"; + } +} diff --git a/sdk-extensions/autoconfigure/src/testFullConfig/java/io/opentelemetry/sdk/autoconfigure/provider/TestColorResourceProvider.java b/sdk-extensions/autoconfigure/src/testFullConfig/java/io/opentelemetry/sdk/autoconfigure/provider/TestColorResourceProvider.java deleted file mode 100644 index 2d071ed4649..00000000000 --- a/sdk-extensions/autoconfigure/src/testFullConfig/java/io/opentelemetry/sdk/autoconfigure/provider/TestColorResourceProvider.java +++ /dev/null @@ -1,19 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package io.opentelemetry.sdk.autoconfigure.provider; - -import io.opentelemetry.api.common.AttributeKey; -import io.opentelemetry.api.common.Attributes; -import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; -import io.opentelemetry.sdk.autoconfigure.spi.ResourceProvider; -import io.opentelemetry.sdk.resources.Resource; - -public class TestColorResourceProvider implements ResourceProvider { - @Override - public Resource createResource(ConfigProperties config) { - return Resource.create(Attributes.of(AttributeKey.stringKey("color"), "blue")); - } -} diff --git a/sdk-extensions/autoconfigure/src/testFullConfig/java/io/opentelemetry/sdk/autoconfigure/provider/TestServiceNameResourceDetector.java b/sdk-extensions/autoconfigure/src/testFullConfig/java/io/opentelemetry/sdk/autoconfigure/provider/TestServiceNameResourceDetector.java new file mode 100644 index 00000000000..27333ee9375 --- /dev/null +++ b/sdk-extensions/autoconfigure/src/testFullConfig/java/io/opentelemetry/sdk/autoconfigure/provider/TestServiceNameResourceDetector.java @@ -0,0 +1,29 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.sdk.autoconfigure.provider; + +import io.opentelemetry.api.common.AttributeKey; +import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; +import io.opentelemetry.sdk.autoconfigure.spi.internal.ResourceDetector; +import java.util.Optional; + +public class TestServiceNameResourceDetector implements ResourceDetector { + + @Override + public Optional readData(ConfigProperties config) { + return Optional.of("cart"); + } + + @Override + public void registerAttributes(Builder builder) { + builder.add(AttributeKey.stringKey("service.name"), Optional::of); + } + + @Override + public String name() { + return "name"; + } +} diff --git a/sdk-extensions/autoconfigure/src/testFullConfig/resources/META-INF/services/io.opentelemetry.sdk.autoconfigure.spi.ResourceProvider b/sdk-extensions/autoconfigure/src/testFullConfig/resources/META-INF/services/io.opentelemetry.sdk.autoconfigure.spi.ResourceProvider index b2e50b0682f..32517bf16a9 100644 --- a/sdk-extensions/autoconfigure/src/testFullConfig/resources/META-INF/services/io.opentelemetry.sdk.autoconfigure.spi.ResourceProvider +++ b/sdk-extensions/autoconfigure/src/testFullConfig/resources/META-INF/services/io.opentelemetry.sdk.autoconfigure.spi.ResourceProvider @@ -1,2 +1 @@ io.opentelemetry.sdk.autoconfigure.provider.TestAnimalResourceProvider -io.opentelemetry.sdk.autoconfigure.provider.TestColorResourceProvider \ No newline at end of file diff --git a/sdk-extensions/autoconfigure/src/testFullConfig/resources/META-INF/services/io.opentelemetry.sdk.autoconfigure.spi.internal.ResourceDetector b/sdk-extensions/autoconfigure/src/testFullConfig/resources/META-INF/services/io.opentelemetry.sdk.autoconfigure.spi.internal.ResourceDetector new file mode 100644 index 00000000000..707737cf9bf --- /dev/null +++ b/sdk-extensions/autoconfigure/src/testFullConfig/resources/META-INF/services/io.opentelemetry.sdk.autoconfigure.spi.internal.ResourceDetector @@ -0,0 +1,2 @@ +io.opentelemetry.sdk.autoconfigure.provider.TestColorResourceDetector +io.opentelemetry.sdk.autoconfigure.provider.TestServiceNameResourceDetector