SdkTracerProvider auto closed as separate Context Bean
MODIFY
instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/OpenTelemetryAutoConfiguration.java
instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/OpenTelemetryAutoConfiguration.java
@@ -33,11 +33,14 @@ import org.springframework.context.annotation.Configuration;
@EnableConfigurationProperties(SamplerProperties.class)
public class OpenTelemetryAutoConfiguration {
- @Bean
+ @Configuration
+ @ConditionalOnMissingBean(OpenTelemetry.class)
+ public static class OpenTelemetryBeanConfig {
+
+ @Bean(destroyMethod = "close")
@ConditionalOnMissingBean
- public OpenTelemetry openTelemetry(
+ public SdkTracerProvider sdkTracerProvider(
SamplerProperties samplerProperties,
- ObjectProvider<ContextPropagators> propagatorsProvider,
ObjectProvider<List<SpanExporter>> spanExportersProvider) {
SdkTracerProviderBuilder tracerProviderBuilder = SdkTracerProvider.builder();
@@ -46,10 +49,14 @@ public class OpenTelemetryAutoConfiguration {
.map(SimpleSpanProcessor::create)
.forEach(tracerProviderBuilder::addSpanProcessor);
- SdkTracerProvider tracerProvider =
- tracerProviderBuilder
+ return tracerProviderBuilder
.setSampler(Sampler.traceIdRatioBased(samplerProperties.getProbability()))
.build();
+ }
+
+ @Bean
+ public OpenTelemetry openTelemetry(
+ ObjectProvider<ContextPropagators> propagatorsProvider, SdkTracerProvider tracerProvider) {
ContextPropagators propagators = propagatorsProvider.getIfAvailable(ContextPropagators::noop);
@@ -59,3 +66,4 @@ public class OpenTelemetryAutoConfiguration {
.build();
}
}
+}

MODIFY
instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/OpenTelemetryAutoConfigurationTest.java
instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/OpenTelemetryAutoConfigurationTest.java
@@ -9,6 +9,7 @@ import static org.assertj.core.api.Assertions.assertThat;
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.api.OpenTelemetry;
+import io.opentelemetry.sdk.trace.SdkTracerProvider;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
@@ -37,23 +38,42 @@ class OpenTelemetryAutoConfigurationTest {
@Test
@DisplayName(
"when Application Context contains OpenTelemetry bean should NOT initialize openTelemetry")
- void customTracer() {
+ void customOpenTelemetry() {
this.contextRunner
.withUserConfiguration(CustomTracerConfiguration.class)
.withConfiguration(AutoConfigurations.of(OpenTelemetryAutoConfiguration.class))
.run(
- context -> {
- assertThat(context.containsBean("customOpenTelemetry")).isTrue();
- assertThat(context.containsBean("openTelemetry")).isFalse();
- });
+ context ->
+ assertThat(context)
+ .hasBean("customOpenTelemetry")
+ .doesNotHaveBean("openTelemetry")
+ .doesNotHaveBean("sdkTracerProvider"));
}
@Test
@DisplayName(
"when Application Context DOES NOT contain OpenTelemetry bean should initialize openTelemetry")
- void initializeTracer() {
+ void initializeTracerProviderAndOpenTelemetry() {
this.contextRunner
.withConfiguration(AutoConfigurations.of(OpenTelemetryAutoConfiguration.class))
- .run(context -> assertThat(context.containsBean("openTelemetry")).isTrue());
+ .run(context -> assertThat(context).hasBean("openTelemetry").hasBean("sdkTracerProvider"));
+ }
+
+ @Test
+ @DisplayName(
+ "when Application Context DOES NOT contain OpenTelemetry bean but TracerProvider should initialize openTelemetry")
+ void initializeOpenTelemetry() {
+ this.contextRunner
+ .withBean(
+ "customTracerProvider",
+ SdkTracerProvider.class,
+ () -> SdkTracerProvider.builder().build())
+ .withConfiguration(AutoConfigurations.of(OpenTelemetryAutoConfiguration.class))
+ .run(
+ context ->
+ assertThat(context)
+ .hasBean("openTelemetry")
+ .hasBean("customTracerProvider")
+ .doesNotHaveBean("sdkTracerProvider"));
}
}

SdkTracerProvider auto closed as separate Context Bean (#5124) (#5125)