diff --git a/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/resources/DistroVersionResourceProvider.java b/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/resources/DistroVersionResourceProvider.java new file mode 100644 index 000000000000..57c163b1ac01 --- /dev/null +++ b/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/resources/DistroVersionResourceProvider.java @@ -0,0 +1,34 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.instrumentation.spring.autoconfigure.resources; + +import io.opentelemetry.api.common.AttributeKey; +import io.opentelemetry.api.common.Attributes; +import io.opentelemetry.instrumentation.api.internal.EmbeddedInstrumentationProperties; +import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; +import io.opentelemetry.sdk.autoconfigure.spi.ResourceProvider; +import io.opentelemetry.sdk.resources.Resource; + +public class DistroVersionResourceProvider implements ResourceProvider { + + public static final String VERSION = + EmbeddedInstrumentationProperties.findVersion("io.opentelemetry.spring-boot-autoconfigure"); + + private static final AttributeKey TELEMETRY_DISTRO_NAME = + AttributeKey.stringKey("telemetry.distro.name"); + private static final AttributeKey TELEMETRY_DISTRO_VERSION = + AttributeKey.stringKey("telemetry.distro.version"); + + @Override + public Resource createResource(ConfigProperties config) { + return Resource.create( + Attributes.of( + TELEMETRY_DISTRO_NAME, + "opentelemetry-spring-boot-starter", + TELEMETRY_DISTRO_VERSION, + VERSION)); + } +} diff --git a/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/resources/OtelResourceAutoConfiguration.java b/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/resources/OtelResourceAutoConfiguration.java index 7cc961b4aea8..d9abc775b12e 100644 --- a/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/resources/OtelResourceAutoConfiguration.java +++ b/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/resources/OtelResourceAutoConfiguration.java @@ -37,6 +37,11 @@ public ResourceProvider otelResourceProvider( return new SpringResourceProvider(otelSpringResourceProperties, otelResourceProperties); } + @Bean + public ResourceProvider otelDistroVersionResourceProvider() { + return new DistroVersionResourceProvider(); + } + @Bean @ConditionalOnClass(OsResource.class) public ResourceProvider otelOsResourceProvider() { diff --git a/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/resources/DistroVersionResourceProviderTest.java b/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/resources/DistroVersionResourceProviderTest.java new file mode 100644 index 000000000000..011502ff9614 --- /dev/null +++ b/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/resources/DistroVersionResourceProviderTest.java @@ -0,0 +1,48 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.instrumentation.spring.autoconfigure.resources; + +import static org.assertj.core.api.Assertions.assertThat; + +import com.google.common.collect.ImmutableMap; +import io.opentelemetry.api.common.AttributeKey; +import io.opentelemetry.sdk.autoconfigure.spi.ResourceProvider; +import io.opentelemetry.sdk.autoconfigure.spi.internal.DefaultConfigProperties; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.boot.autoconfigure.AutoConfigurations; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; + +public class DistroVersionResourceProviderTest { + private final ApplicationContextRunner contextRunner = + new ApplicationContextRunner() + .withConfiguration(AutoConfigurations.of(OtelResourceAutoConfiguration.class)); + + @Test + @DisplayName("distro version should be set") + void hasAttributes() { + + this.contextRunner.run( + context -> { + ResourceProvider resource = + context.getBean("otelDistroVersionResourceProvider", ResourceProvider.class); + + assertThat( + resource + .createResource(DefaultConfigProperties.createFromMap(ImmutableMap.of())) + .getAttributes() + .asMap()) + .containsEntry( + AttributeKey.stringKey("telemetry.distro.name"), + "opentelemetry-spring-boot-starter") + .anySatisfy( + (key, val) -> { + assertThat(key.getKey()).isEqualTo("telemetry.distro.version"); + assertThat(val).asString().isNotBlank(); + }); + }); + } +}