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 e1cecb298139..7aab13e9a751 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 @@ -18,10 +18,14 @@ import io.opentelemetry.sdk.autoconfigure.internal.AutoConfigureUtil; import io.opentelemetry.sdk.autoconfigure.internal.ComponentLoader; import io.opentelemetry.sdk.autoconfigure.internal.SpiHelper; +import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; import io.opentelemetry.sdk.autoconfigure.spi.ResourceProvider; +import io.opentelemetry.sdk.autoconfigure.spi.internal.DefaultConfigProperties; +import java.util.Collections; import java.util.List; import java.util.Optional; import java.util.stream.Collectors; +import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.ConfigurationPropertiesBinding; @@ -78,31 +82,37 @@ public ResourceProvider otelDistroVersionResourceProvider() { } @Bean - // If you change the bean name, also change it in the OpenTelemetryJdbcDriverAutoConfiguration - // class - public OpenTelemetry openTelemetry( + public AutoConfiguredOpenTelemetrySdk autoConfiguredOpenTelemetrySdk( Environment env, OtlpExporterProperties otlpExporterProperties, OtelResourceProperties resourceProperties, PropagationProperties propagationProperties, OpenTelemetrySdkComponentLoader componentLoader) { - OpenTelemetry openTelemetry = - AutoConfigureUtil.setComponentLoader( - AutoConfigureUtil.setConfigPropertiesCustomizer( - AutoConfiguredOpenTelemetrySdk.builder(), - c -> - SpringConfigProperties.create( - env, - otlpExporterProperties, - resourceProperties, - propagationProperties, - c)), - componentLoader) - .build() - .getOpenTelemetrySdk(); - - return openTelemetry; + return AutoConfigureUtil.setComponentLoader( + AutoConfigureUtil.setConfigPropertiesCustomizer( + AutoConfiguredOpenTelemetrySdk.builder(), + c -> + SpringConfigProperties.create( + env, + otlpExporterProperties, + resourceProperties, + propagationProperties, + c)), + componentLoader) + .build(); + } + + @Bean + public OpenTelemetry openTelemetry( + AutoConfiguredOpenTelemetrySdk autoConfiguredOpenTelemetrySdk) { + return autoConfiguredOpenTelemetrySdk.getOpenTelemetrySdk(); + } + + @Bean("configProperties") + public ConfigProperties configProperties( + AutoConfiguredOpenTelemetrySdk autoConfiguredOpenTelemetrySdk) { + return AutoConfigureUtil.getConfig(autoConfiguredOpenTelemetrySdk); } } @@ -115,6 +125,22 @@ public static class DisabledOpenTelemetrySdkConfig { public OpenTelemetry openTelemetry() { return OpenTelemetry.noop(); } + + @Bean("configProperties") + public ConfigProperties configProperties() { + return DefaultConfigProperties.createFromMap(Collections.emptyMap()); + } + } + + @Configuration + @ConditionalOnBean(OpenTelemetry.class) + @ConditionalOnMissingBean({ConfigProperties.class}) + public static class FallbackConfigProperties { + + @Bean("configProperties") + public ConfigProperties configProperties() { + return DefaultConfigProperties.create(Collections.emptyMap()); + } } /** 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 e2fadd47477a..c746a72014c9 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 @@ -7,6 +7,7 @@ import io.opentelemetry.api.OpenTelemetry; import io.opentelemetry.instrumentation.spring.kafka.v2_7.SpringKafkaTelemetry; +import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; import org.springframework.beans.factory.ObjectProvider; import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory; @@ -14,12 +15,13 @@ class ConcurrentKafkaListenerContainerFactoryPostProcessor implements BeanPostProcessor { private final ObjectProvider openTelemetryProvider; - private final boolean experimentalSpanAttributes; + private final ObjectProvider configPropertiesProvider; ConcurrentKafkaListenerContainerFactoryPostProcessor( - ObjectProvider openTelemetryProvider, boolean experimentalSpanAttributes) { + ObjectProvider openTelemetryProvider, + ObjectProvider configPropertiesProvider) { this.openTelemetryProvider = openTelemetryProvider; - this.experimentalSpanAttributes = experimentalSpanAttributes; + this.configPropertiesProvider = configPropertiesProvider; } @Override @@ -32,7 +34,10 @@ public Object postProcessAfterInitialization(Object bean, String beanName) { (ConcurrentKafkaListenerContainerFactory) bean; SpringKafkaTelemetry springKafkaTelemetry = SpringKafkaTelemetry.builder(openTelemetryProvider.getObject()) - .setCaptureExperimentalSpanAttributes(experimentalSpanAttributes) + .setCaptureExperimentalSpanAttributes( + configPropertiesProvider + .getObject() + .getBoolean("otel.instrumentation.kafka.experimental-span-attributes", false)) .build(); 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 06f89448010a..66c40bf06a0b 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 @@ -8,8 +8,8 @@ import io.opentelemetry.api.OpenTelemetry; import io.opentelemetry.instrumentation.kafkaclients.v2_6.KafkaTelemetry; import io.opentelemetry.instrumentation.spring.autoconfigure.internal.SdkEnabled; +import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; import org.springframework.beans.factory.ObjectProvider; -import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; @@ -20,7 +20,7 @@ import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory; import org.springframework.kafka.core.KafkaTemplate; -@ConditionalOnBean(OpenTelemetry.class) +@ConditionalOnBean({OpenTelemetry.class, ConfigProperties.class}) @ConditionalOnClass({KafkaTemplate.class, ConcurrentKafkaListenerContainerFactory.class}) @ConditionalOnProperty(name = "otel.instrumentation.kafka.enabled", matchIfMissing = true) @Conditional(SdkEnabled.class) @@ -39,9 +39,8 @@ DefaultKafkaProducerFactoryCustomizer otelKafkaProducerFactoryCustomizer( static ConcurrentKafkaListenerContainerFactoryPostProcessor otelKafkaListenerContainerFactoryBeanPostProcessor( ObjectProvider openTelemetryProvider, - @Value("${otel.instrumentation.kafka.experimental-span-attributes:false}") - boolean experimentalSpanAttributes) { + ObjectProvider configPropertiesProvider) { return new ConcurrentKafkaListenerContainerFactoryPostProcessor( - openTelemetryProvider, experimentalSpanAttributes); + openTelemetryProvider, configPropertiesProvider); } } 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 f27fe499c530..c11e86460ab8 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 @@ -48,7 +48,8 @@ void customOpenTelemetry() { context -> assertThat(context) .hasBean("customOpenTelemetry") - .doesNotHaveBean("openTelemetry")); + .doesNotHaveBean("openTelemetry") + .hasBean("configProperties")); } @Test @@ -57,7 +58,7 @@ void customOpenTelemetry() { void initializeProvidersAndOpenTelemetry() { this.contextRunner .withConfiguration(AutoConfigurations.of(OpenTelemetryAutoConfiguration.class)) - .run(context -> assertThat(context).hasBean("openTelemetry")); + .run(context -> assertThat(context).hasBean("openTelemetry").hasBean("configProperties")); } @Test