diff --git a/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/OpenTelemetryAutoConfiguration.java b/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/OpenTelemetryAutoConfiguration.java index 17299f1d24a1..586937e1bf1f 100644 --- a/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/OpenTelemetryAutoConfiguration.java +++ b/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/OpenTelemetryAutoConfiguration.java @@ -12,6 +12,7 @@ import io.opentelemetry.instrumentation.spring.autoconfigure.exporters.otlp.OtlpMetricExporterAutoConfiguration; import io.opentelemetry.instrumentation.spring.autoconfigure.exporters.otlp.OtlpSpanExporterAutoConfiguration; import io.opentelemetry.instrumentation.spring.autoconfigure.internal.MapConverter; +import io.opentelemetry.instrumentation.spring.autoconfigure.internal.OpenTelemetrySupplier; import io.opentelemetry.instrumentation.spring.autoconfigure.resources.OtelResourceAutoConfiguration; import io.opentelemetry.instrumentation.spring.autoconfigure.resources.SpringResourceConfigProperties; import io.opentelemetry.sdk.OpenTelemetrySdk; @@ -34,7 +35,9 @@ import io.opentelemetry.sdk.trace.samplers.Sampler; import java.util.Collections; import java.util.List; +import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.ObjectProvider; +import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; @@ -42,6 +45,7 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Role; import org.springframework.core.env.Environment; import org.springframework.expression.spel.standard.SpelExpressionParser; @@ -189,4 +193,12 @@ public OpenTelemetry openTelemetry() { return OpenTelemetry.noop(); } } + + @Bean + // we declared this bean as an infrastructure bean to avoid warning from BeanPostProcessorChecker + // when it is injected into a BeanPostProcessor + @Role(BeanDefinition.ROLE_INFRASTRUCTURE) + public static OpenTelemetrySupplier openTelemetrySupplier(BeanFactory beanFactory) { + return () -> beanFactory.getBean(OpenTelemetry.class); + } } diff --git a/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/instrumentation/kafka/ConcurrentKafkaListenerContainerFactoryPostProcessor.java b/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/instrumentation/kafka/ConcurrentKafkaListenerContainerFactoryPostProcessor.java index 0987475db2d5..bff5ab86b6d3 100644 --- a/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/instrumentation/kafka/ConcurrentKafkaListenerContainerFactoryPostProcessor.java +++ b/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/instrumentation/kafka/ConcurrentKafkaListenerContainerFactoryPostProcessor.java @@ -5,17 +5,18 @@ package io.opentelemetry.instrumentation.spring.autoconfigure.instrumentation.kafka; -import io.opentelemetry.api.OpenTelemetry; +import io.opentelemetry.instrumentation.spring.autoconfigure.internal.OpenTelemetrySupplier; import io.opentelemetry.instrumentation.spring.kafka.v2_7.SpringKafkaTelemetry; import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory; class ConcurrentKafkaListenerContainerFactoryPostProcessor implements BeanPostProcessor { - private final OpenTelemetry openTelemetry; + private final OpenTelemetrySupplier openTelemetrySupplier; - ConcurrentKafkaListenerContainerFactoryPostProcessor(OpenTelemetry openTelemetry) { - this.openTelemetry = openTelemetry; + ConcurrentKafkaListenerContainerFactoryPostProcessor( + OpenTelemetrySupplier openTelemetrySupplier) { + this.openTelemetrySupplier = openTelemetrySupplier; } @Override @@ -26,7 +27,8 @@ public Object postProcessAfterInitialization(Object bean, String beanName) { ConcurrentKafkaListenerContainerFactory, ?> listenerContainerFactory = (ConcurrentKafkaListenerContainerFactory, ?>) bean; - SpringKafkaTelemetry springKafkaTelemetry = SpringKafkaTelemetry.create(openTelemetry); + SpringKafkaTelemetry springKafkaTelemetry = + SpringKafkaTelemetry.create(openTelemetrySupplier.get()); listenerContainerFactory.setBatchInterceptor(springKafkaTelemetry.createBatchInterceptor()); listenerContainerFactory.setRecordInterceptor(springKafkaTelemetry.createRecordInterceptor()); diff --git a/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/instrumentation/kafka/KafkaInstrumentationAutoConfiguration.java b/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/instrumentation/kafka/KafkaInstrumentationAutoConfiguration.java index c34686f6d48f..deeabb80761c 100644 --- a/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/instrumentation/kafka/KafkaInstrumentationAutoConfiguration.java +++ b/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/instrumentation/kafka/KafkaInstrumentationAutoConfiguration.java @@ -7,6 +7,7 @@ import io.opentelemetry.api.OpenTelemetry; import io.opentelemetry.instrumentation.kafkaclients.v2_6.KafkaTelemetry; +import io.opentelemetry.instrumentation.spring.autoconfigure.internal.OpenTelemetrySupplier; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; @@ -30,8 +31,9 @@ DefaultKafkaProducerFactoryCustomizer otelKafkaProducerFactoryCustomizer( } @Bean - ConcurrentKafkaListenerContainerFactoryPostProcessor - otelKafkaListenerContainerFactoryBeanPostProcessor(OpenTelemetry openTelemetry) { - return new ConcurrentKafkaListenerContainerFactoryPostProcessor(openTelemetry); + static ConcurrentKafkaListenerContainerFactoryPostProcessor + otelKafkaListenerContainerFactoryBeanPostProcessor( + OpenTelemetrySupplier openTelemetrySupplier) { + return new ConcurrentKafkaListenerContainerFactoryPostProcessor(openTelemetrySupplier); } } diff --git a/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/instrumentation/web/RestTemplateBeanPostProcessor.java b/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/instrumentation/web/RestTemplateBeanPostProcessor.java index c8f213f241ad..ac6cda69d29b 100644 --- a/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/instrumentation/web/RestTemplateBeanPostProcessor.java +++ b/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/instrumentation/web/RestTemplateBeanPostProcessor.java @@ -5,7 +5,7 @@ package io.opentelemetry.instrumentation.spring.autoconfigure.instrumentation.web; -import io.opentelemetry.api.OpenTelemetry; +import io.opentelemetry.instrumentation.spring.autoconfigure.internal.OpenTelemetrySupplier; import io.opentelemetry.instrumentation.spring.web.v3_1.SpringWebTelemetry; import java.util.List; import org.springframework.beans.factory.config.BeanPostProcessor; @@ -14,10 +14,10 @@ final class RestTemplateBeanPostProcessor implements BeanPostProcessor { - private final OpenTelemetry openTelemetry; + private final OpenTelemetrySupplier openTelemetrySupplier; - RestTemplateBeanPostProcessor(OpenTelemetry openTelemetry) { - this.openTelemetry = openTelemetry; + RestTemplateBeanPostProcessor(OpenTelemetrySupplier openTelemetrySupplier) { + this.openTelemetrySupplier = openTelemetrySupplier; } @Override @@ -28,7 +28,7 @@ public Object postProcessAfterInitialization(Object bean, String beanName) { RestTemplate restTemplate = (RestTemplate) bean; ClientHttpRequestInterceptor interceptor = - SpringWebTelemetry.create(openTelemetry).newInterceptor(); + SpringWebTelemetry.create(openTelemetrySupplier.get()).newInterceptor(); addRestTemplateInterceptorIfNotPresent(restTemplate, interceptor); return restTemplate; } diff --git a/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/instrumentation/web/SpringWebInstrumentationAutoConfiguration.java b/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/instrumentation/web/SpringWebInstrumentationAutoConfiguration.java index 491af6b51e96..7a04f8c87f2c 100644 --- a/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/instrumentation/web/SpringWebInstrumentationAutoConfiguration.java +++ b/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/instrumentation/web/SpringWebInstrumentationAutoConfiguration.java @@ -6,6 +6,7 @@ package io.opentelemetry.instrumentation.spring.autoconfigure.instrumentation.web; import io.opentelemetry.api.OpenTelemetry; +import io.opentelemetry.instrumentation.spring.autoconfigure.internal.OpenTelemetrySupplier; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; @@ -24,8 +25,11 @@ @Configuration public class SpringWebInstrumentationAutoConfiguration { + public SpringWebInstrumentationAutoConfiguration() {} + @Bean - RestTemplateBeanPostProcessor otelRestTemplateBeanPostProcessor(OpenTelemetry openTelemetry) { - return new RestTemplateBeanPostProcessor(openTelemetry); + static RestTemplateBeanPostProcessor otelRestTemplateBeanPostProcessor( + OpenTelemetrySupplier openTelemetrySupplier) { + return new RestTemplateBeanPostProcessor(openTelemetrySupplier); } } diff --git a/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/instrumentation/webflux/SpringWebfluxInstrumentationAutoConfiguration.java b/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/instrumentation/webflux/SpringWebfluxInstrumentationAutoConfiguration.java index 249b8a7906bf..d5113fcfe1de 100644 --- a/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/instrumentation/webflux/SpringWebfluxInstrumentationAutoConfiguration.java +++ b/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/instrumentation/webflux/SpringWebfluxInstrumentationAutoConfiguration.java @@ -6,6 +6,7 @@ package io.opentelemetry.instrumentation.spring.autoconfigure.instrumentation.webflux; import io.opentelemetry.api.OpenTelemetry; +import io.opentelemetry.instrumentation.spring.autoconfigure.internal.OpenTelemetrySupplier; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; @@ -24,8 +25,11 @@ @Configuration public class SpringWebfluxInstrumentationAutoConfiguration { + public SpringWebfluxInstrumentationAutoConfiguration() {} + @Bean - WebClientBeanPostProcessor otelWebClientBeanPostProcessor(OpenTelemetry openTelemetry) { - return new WebClientBeanPostProcessor(openTelemetry); + static WebClientBeanPostProcessor otelWebClientBeanPostProcessor( + OpenTelemetrySupplier openTelemetrySupplier) { + return new WebClientBeanPostProcessor(openTelemetrySupplier); } } diff --git a/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/instrumentation/webflux/WebClientBeanPostProcessor.java b/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/instrumentation/webflux/WebClientBeanPostProcessor.java index b066045750a4..d85970c35486 100644 --- a/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/instrumentation/webflux/WebClientBeanPostProcessor.java +++ b/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/instrumentation/webflux/WebClientBeanPostProcessor.java @@ -5,7 +5,7 @@ package io.opentelemetry.instrumentation.spring.autoconfigure.instrumentation.webflux; -import io.opentelemetry.api.OpenTelemetry; +import io.opentelemetry.instrumentation.spring.autoconfigure.internal.OpenTelemetrySupplier; import io.opentelemetry.instrumentation.spring.webflux.v5_3.SpringWebfluxTelemetry; import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.web.reactive.function.client.WebClient; @@ -17,10 +17,10 @@ */ final class WebClientBeanPostProcessor implements BeanPostProcessor { - private final OpenTelemetry openTelemetry; + private final OpenTelemetrySupplier openTelemetrySupplier; - WebClientBeanPostProcessor(OpenTelemetry openTelemetry) { - this.openTelemetry = openTelemetry; + WebClientBeanPostProcessor(OpenTelemetrySupplier openTelemetrySupplier) { + this.openTelemetrySupplier = openTelemetrySupplier; } @Override @@ -36,7 +36,8 @@ public Object postProcessAfterInitialization(Object bean, String beanName) { } private WebClient.Builder wrapBuilder(WebClient.Builder webClientBuilder) { - SpringWebfluxTelemetry instrumentation = SpringWebfluxTelemetry.create(openTelemetry); + SpringWebfluxTelemetry instrumentation = + SpringWebfluxTelemetry.create(openTelemetrySupplier.get()); return webClientBuilder.filters(instrumentation::addClientTracingFilter); } } diff --git a/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/OpenTelemetrySupplier.java b/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/OpenTelemetrySupplier.java new file mode 100644 index 000000000000..87f64c8fda5e --- /dev/null +++ b/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/OpenTelemetrySupplier.java @@ -0,0 +1,17 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.instrumentation.spring.autoconfigure.internal; + +import io.opentelemetry.api.OpenTelemetry; +import java.util.function.Supplier; + +/** + * Used to provide access to the OpenTelemetry instance in bean post processors. + * + *
This class is internal and is hence not for public use. Its APIs are unstable and can change
+ * at any time.
+ */
+public interface OpenTelemetrySupplier extends Supplier