From e08bddafc40cd7239d603f214bfb703add0489ed Mon Sep 17 00:00:00 2001 From: Gregor Zeitlinger Date: Tue, 20 Feb 2024 17:13:32 +0100 Subject: [PATCH] re-use library to read build-info.properties --- .../build.gradle.kts | 1 + .../OtelResourceAutoConfiguration.java | 24 +++++++++++++------ .../resources/SpringResourceProvider.java | 17 ------------- .../OpenTelemetryAutoConfigurationTest.java | 24 ------------------- .../SpringBootServiceVersionDetector.java | 4 ---- .../spring-boot-starter/build.gradle.kts | 1 + 6 files changed, 19 insertions(+), 52 deletions(-) diff --git a/instrumentation/spring/spring-boot-autoconfigure/build.gradle.kts b/instrumentation/spring/spring-boot-autoconfigure/build.gradle.kts index eede37e60510..9c508b7664fc 100644 --- a/instrumentation/spring/spring-boot-autoconfigure/build.gradle.kts +++ b/instrumentation/spring/spring-boot-autoconfigure/build.gradle.kts @@ -47,6 +47,7 @@ dependencies { compileOnly(project(":instrumentation-annotations")) compileOnly(project(":instrumentation:resources:library")) + compileOnly(project(":instrumentation:spring:spring-boot-resources:library")) annotationProcessor("com.google.auto.service:auto-service") compileOnly("com.google.auto.service:auto-service-annotations") 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 0e5e86df5c45..64d79bd8f422 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 @@ -17,15 +17,14 @@ import io.opentelemetry.instrumentation.resources.ProcessRuntimeResource; import io.opentelemetry.instrumentation.resources.ProcessRuntimeResourceProvider; import io.opentelemetry.instrumentation.spring.autoconfigure.OpenTelemetryAutoConfiguration; +import io.opentelemetry.instrumentation.spring.resources.SpringBootServiceNameDetector; +import io.opentelemetry.instrumentation.spring.resources.SpringBootServiceVersionDetector; import io.opentelemetry.sdk.autoconfigure.internal.EnvironmentResourceProvider; import io.opentelemetry.sdk.autoconfigure.spi.ResourceProvider; -import java.util.Optional; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.AutoConfigureBefore; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.EnableConfigurationProperties; -import org.springframework.boot.info.BuildProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -41,9 +40,8 @@ public ResourceProvider otelEnvironmentResourceProvider() { } @Bean - public ResourceProvider otelSpringResourceProvider( - @Autowired(required = false) BuildProperties buildProperties) { - return new SpringResourceProvider(Optional.ofNullable(buildProperties)); + public ResourceProvider otelSpringResourceProvider() { + return new SpringResourceProvider(); } @Bean @@ -53,10 +51,22 @@ public ResourceProvider otelDistroVersionResourceProvider() { @Bean @ConditionalOnClass(JarServiceNameDetector.class) - public ResourceProvider otelJarResourceProvider() { + public ResourceProvider otelJarNameResourceProvider() { return new JarServiceNameDetector(); } + @Bean + @ConditionalOnClass(SpringBootServiceNameDetector.class) + public ResourceProvider otelSpringBootServiceNameResourceProvider() { + return new SpringBootServiceNameDetector(); + } + + @Bean + @ConditionalOnClass(SpringBootServiceVersionDetector.class) + public ResourceProvider otelSpringBootServiceVersionResourceProvider() { + return new SpringBootServiceVersionDetector(); + } + @Bean @ConditionalOnClass(OsResource.class) public ResourceProvider otelOsResourceProvider() { diff --git a/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/resources/SpringResourceProvider.java b/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/resources/SpringResourceProvider.java index baf76a586ce2..756d227f818e 100644 --- a/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/resources/SpringResourceProvider.java +++ b/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/resources/SpringResourceProvider.java @@ -11,33 +11,16 @@ import io.opentelemetry.sdk.autoconfigure.spi.ResourceProvider; import io.opentelemetry.sdk.resources.Resource; import io.opentelemetry.semconv.ResourceAttributes; -import java.util.Optional; -import org.springframework.boot.info.BuildProperties; public class SpringResourceProvider implements ResourceProvider { - private final Optional buildProperties; - - public SpringResourceProvider(Optional buildProperties) { - this.buildProperties = buildProperties; - } - @Override public Resource createResource(ConfigProperties configProperties) { AttributesBuilder attributesBuilder = Attributes.builder(); - buildProperties - .map(BuildProperties::getName) - .ifPresent(v -> attributesBuilder.put(ResourceAttributes.SERVICE_NAME, v)); - String springApplicationName = configProperties.getString("spring.application.name"); if (springApplicationName != null) { attributesBuilder.put(ResourceAttributes.SERVICE_NAME, springApplicationName); } - - buildProperties - .map(BuildProperties::getVersion) - .ifPresent(v -> attributesBuilder.put(ResourceAttributes.SERVICE_VERSION, v)); - return Resource.create(attributesBuilder.build()); } } diff --git a/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/OpenTelemetryAutoConfigurationTest.java b/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/OpenTelemetryAutoConfigurationTest.java index 4c8cdd43ec02..e0bf3f98f849 100644 --- a/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/OpenTelemetryAutoConfigurationTest.java +++ b/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/OpenTelemetryAutoConfigurationTest.java @@ -6,7 +6,6 @@ package io.opentelemetry.instrumentation.spring.autoconfigure; import static io.opentelemetry.semconv.ResourceAttributes.SERVICE_NAME; -import static io.opentelemetry.semconv.ResourceAttributes.SERVICE_VERSION; import static org.assertj.core.api.Assertions.assertThat; import io.opentelemetry.api.OpenTelemetry; @@ -17,11 +16,9 @@ import io.opentelemetry.sdk.metrics.SdkMeterProvider; import io.opentelemetry.sdk.resources.Resource; import io.opentelemetry.sdk.trace.SdkTracerProvider; -import java.util.Properties; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.boot.autoconfigure.AutoConfigurations; -import org.springframework.boot.info.BuildProperties; import org.springframework.boot.test.context.TestConfiguration; import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.context.annotation.Bean; @@ -120,27 +117,6 @@ void shouldDetermineServiceNameBySpringApplicationName() { }); } - @Test - @DisplayName( - "when spring.application.name is set value should be passed to service name attribute") - void shouldDetermineServiceNameAndVersionBySpringApplicationVersion() { - Properties properties = new Properties(); - properties.put("name", "demo"); - properties.put("version", "0.3"); - this.contextRunner - .withBean("buildProperties", BuildProperties.class, () -> new BuildProperties(properties)) - .withConfiguration( - AutoConfigurations.of( - OtelResourceAutoConfiguration.class, OpenTelemetryAutoConfiguration.class)) - .run( - context -> { - Resource otelResource = context.getBean("otelResource", Resource.class); - - assertThat(otelResource.getAttribute(SERVICE_NAME)).isEqualTo("demo"); - assertThat(otelResource.getAttribute(SERVICE_VERSION)).isEqualTo("0.3"); - }); - } - @Test @DisplayName( "when spring application name and otel service name are not set service name should be default") diff --git a/instrumentation/spring/spring-boot-resources/library/src/main/java/io/opentelemetry/instrumentation/spring/resources/SpringBootServiceVersionDetector.java b/instrumentation/spring/spring-boot-resources/library/src/main/java/io/opentelemetry/instrumentation/spring/resources/SpringBootServiceVersionDetector.java index b33c2a9a9167..3c9ddb05fcdd 100644 --- a/instrumentation/spring/spring-boot-resources/library/src/main/java/io/opentelemetry/instrumentation/spring/resources/SpringBootServiceVersionDetector.java +++ b/instrumentation/spring/spring-boot-resources/library/src/main/java/io/opentelemetry/instrumentation/spring/resources/SpringBootServiceVersionDetector.java @@ -18,10 +18,6 @@ import java.util.Properties; import java.util.logging.Logger; -/** - * Note: should not be used inside a spring application, where the spring.application.name is - * already available. - */ @AutoService(ResourceProvider.class) public class SpringBootServiceVersionDetector implements ResourceProvider { diff --git a/instrumentation/spring/starters/spring-boot-starter/build.gradle.kts b/instrumentation/spring/starters/spring-boot-starter/build.gradle.kts index 268247262975..a58d397eb60a 100644 --- a/instrumentation/spring/starters/spring-boot-starter/build.gradle.kts +++ b/instrumentation/spring/starters/spring-boot-starter/build.gradle.kts @@ -13,6 +13,7 @@ dependencies { api("org.springframework.boot:spring-boot-starter-aop:$springBootVersion") api(project(":instrumentation:spring:spring-boot-autoconfigure")) implementation(project(":instrumentation:resources:library")) + implementation(project(":instrumentation:spring:spring-boot-resources:library")) api("io.opentelemetry:opentelemetry-sdk-extension-autoconfigure-spi") api("io.opentelemetry:opentelemetry-api") api("io.opentelemetry:opentelemetry-exporter-logging")