From f1add519e67781bd767e051fc942ee12390f1677 Mon Sep 17 00:00:00 2001 From: Gregor Zeitlinger Date: Tue, 30 Apr 2024 21:23:09 +0200 Subject: [PATCH 1/5] support otel.instrumentation.kafka.experimental-span-attributes in spring starter --- ...urrentKafkaListenerContainerFactoryPostProcessor.java | 8 ++++++-- .../kafka/KafkaInstrumentationAutoConfiguration.java | 9 +++++++-- .../additional-spring-configuration-metadata.json | 6 ++++++ 3 files changed, 19 insertions(+), 4 deletions(-) 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 d55e1667d3ab..e2fadd47477a 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 @@ -14,10 +14,12 @@ class ConcurrentKafkaListenerContainerFactoryPostProcessor implements BeanPostProcessor { private final ObjectProvider openTelemetryProvider; + private final boolean experimentalSpanAttributes; ConcurrentKafkaListenerContainerFactoryPostProcessor( - ObjectProvider openTelemetryProvider) { + ObjectProvider openTelemetryProvider, boolean experimentalSpanAttributes) { this.openTelemetryProvider = openTelemetryProvider; + this.experimentalSpanAttributes = experimentalSpanAttributes; } @Override @@ -29,7 +31,9 @@ public Object postProcessAfterInitialization(Object bean, String beanName) { ConcurrentKafkaListenerContainerFactory listenerContainerFactory = (ConcurrentKafkaListenerContainerFactory) bean; SpringKafkaTelemetry springKafkaTelemetry = - SpringKafkaTelemetry.create(openTelemetryProvider.getObject()); + SpringKafkaTelemetry.builder(openTelemetryProvider.getObject()) + .setCaptureExperimentalSpanAttributes(experimentalSpanAttributes) + .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 1f351df640cd..c8a037070f8b 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 @@ -9,6 +9,8 @@ import io.opentelemetry.instrumentation.kafkaclients.v2_6.KafkaTelemetry; import io.opentelemetry.instrumentation.spring.autoconfigure.internal.ConditionalOnEnabledInstrumentation; 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.kafka.DefaultKafkaProducerFactoryCustomizer; import org.springframework.context.annotation.Bean; @@ -32,7 +34,10 @@ DefaultKafkaProducerFactoryCustomizer otelKafkaProducerFactoryCustomizer( @Bean static ConcurrentKafkaListenerContainerFactoryPostProcessor otelKafkaListenerContainerFactoryBeanPostProcessor( - ObjectProvider openTelemetryProvider) { - return new ConcurrentKafkaListenerContainerFactoryPostProcessor(openTelemetryProvider); + ObjectProvider openTelemetryProvider, + @Value("${otel.instrumentation.kafka.experimental-span-attributes:false}") + boolean experimentalSpanAttributes) { + return new ConcurrentKafkaListenerContainerFactoryPostProcessor( + openTelemetryProvider, experimentalSpanAttributes); } } diff --git a/instrumentation/spring/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/instrumentation/spring/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json index 0c42e391be62..1fcd995fad36 100644 --- a/instrumentation/spring/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/instrumentation/spring/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -285,6 +285,12 @@ "description": "Enable the Kafka instrumentation.", "defaultValue": true }, + { + "name": "otel.instrumentation.kafka.experimental-span-attributes", + "type": "java.lang.Boolean", + "description": "Enable the capture of experimental Kafka span attributes.", + "defaultValue": false + }, { "name": "otel.instrumentation.log4j-appender.enabled", "type": "java.lang.Boolean", From 54cddcc979aa7bf165dd3374349eba358fdc1597 Mon Sep 17 00:00:00 2001 From: Gregor Zeitlinger Date: Fri, 3 May 2024 10:59:00 +0200 Subject: [PATCH 2/5] use ConfigProperties instead of spring style injection, because it's compatible with all spring versions, honors properties customizers, and easier to find usages --- .../OpenTelemetryAutoConfiguration.java | 64 +++++++++++++------ ...ListenerContainerFactoryPostProcessor.java | 13 ++-- ...KafkaInstrumentationAutoConfiguration.java | 7 +- .../OpenTelemetryAutoConfigurationTest.java | 5 +- 4 files changed, 60 insertions(+), 29 deletions(-) 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 5d0ce13ecbe1..22db303bbc40 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 @@ -19,10 +19,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; @@ -80,31 +84,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); } } @@ -117,6 +127,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 c8a037070f8b..7da392a1795f 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.ConditionalOnEnabledInstrumentation; +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.kafka.DefaultKafkaProducerFactoryCustomizer; @@ -35,9 +35,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 From 82714143e1d99a103d564750cfed0e5fc8b11f82 Mon Sep 17 00:00:00 2001 From: Gregor Zeitlinger Date: Tue, 14 May 2024 11:32:18 +0200 Subject: [PATCH 3/5] add test for experimental kafka span attributes --- .../spring/autoconfigure/OpenTelemetryAutoConfiguration.java | 1 - .../kafka/KafkaInstrumentationAutoConfiguration.java | 1 - .../smoketest/AbstractJvmKafkaSpringStarterSmokeTest.java | 1 + .../spring/smoketest/AbstractKafkaSpringStarterSmokeTest.java | 4 ++++ .../spring-boot-common/src/main/resources/application.yaml | 2 ++ 5 files changed, 7 insertions(+), 2 deletions(-) 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 22db303bbc40..d02f53fb1be0 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 @@ -138,7 +138,6 @@ public ConfigProperties configProperties() { @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/KafkaInstrumentationAutoConfiguration.java b/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/instrumentation/kafka/KafkaInstrumentationAutoConfiguration.java index 7da392a1795f..1eab4c4e6346 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 @@ -10,7 +10,6 @@ import io.opentelemetry.instrumentation.spring.autoconfigure.internal.ConditionalOnEnabledInstrumentation; import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; import org.springframework.beans.factory.ObjectProvider; -import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.kafka.DefaultKafkaProducerFactoryCustomizer; import org.springframework.context.annotation.Bean; diff --git a/smoke-tests-otel-starter/spring-boot-common/src/main/java/io/opentelemetry/spring/smoketest/AbstractJvmKafkaSpringStarterSmokeTest.java b/smoke-tests-otel-starter/spring-boot-common/src/main/java/io/opentelemetry/spring/smoketest/AbstractJvmKafkaSpringStarterSmokeTest.java index 6a141eafe5a1..8be2b86e9539 100644 --- a/smoke-tests-otel-starter/spring-boot-common/src/main/java/io/opentelemetry/spring/smoketest/AbstractJvmKafkaSpringStarterSmokeTest.java +++ b/smoke-tests-otel-starter/spring-boot-common/src/main/java/io/opentelemetry/spring/smoketest/AbstractJvmKafkaSpringStarterSmokeTest.java @@ -55,6 +55,7 @@ void setUpContext() { KafkaInstrumentationAutoConfiguration.class, KafkaConfig.class)) .withPropertyValues( + "otel.instrumentation.kafka.experimental-span-attributes=true", "spring.kafka.bootstrap-servers=" + kafka.getBootstrapServers(), "spring.kafka.consumer.auto-offset-reset=earliest", "spring.kafka.consumer.linger-ms=10", diff --git a/smoke-tests-otel-starter/spring-boot-common/src/main/java/io/opentelemetry/spring/smoketest/AbstractKafkaSpringStarterSmokeTest.java b/smoke-tests-otel-starter/spring-boot-common/src/main/java/io/opentelemetry/spring/smoketest/AbstractKafkaSpringStarterSmokeTest.java index de88bef46e5b..a5375e86d1ac 100644 --- a/smoke-tests-otel-starter/spring-boot-common/src/main/java/io/opentelemetry/spring/smoketest/AbstractKafkaSpringStarterSmokeTest.java +++ b/smoke-tests-otel-starter/spring-boot-common/src/main/java/io/opentelemetry/spring/smoketest/AbstractKafkaSpringStarterSmokeTest.java @@ -9,6 +9,7 @@ import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.satisfies; import io.opentelemetry.api.OpenTelemetry; +import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.api.trace.SpanKind; import io.opentelemetry.semconv.incubating.MessagingIncubatingAttributes; import org.apache.kafka.clients.admin.NewTopic; @@ -97,6 +98,9 @@ void shouldInstrumentProducerAndConsumer() { equalTo( MessagingIncubatingAttributes.MESSAGING_KAFKA_CONSUMER_GROUP, "testListener"), + satisfies( + AttributeKey.longKey("kafka.record.queue_time_ms"), + AbstractLongAssert::isNotNegative), satisfies( MessagingIncubatingAttributes.MESSAGING_CLIENT_ID, stringAssert -> stringAssert.startsWith("consumer"))), diff --git a/smoke-tests-otel-starter/spring-boot-common/src/main/resources/application.yaml b/smoke-tests-otel-starter/spring-boot-common/src/main/resources/application.yaml index d94e9336a6b9..46cb9437492b 100644 --- a/smoke-tests-otel-starter/spring-boot-common/src/main/resources/application.yaml +++ b/smoke-tests-otel-starter/spring-boot-common/src/main/resources/application.yaml @@ -1,5 +1,7 @@ otel: instrumentation: + kafka: + experimental-span-attributes: true logback-appender: experimental: capture-code-attributes: true From 00b442421befd95893c9de49112f2025ff8aff0f Mon Sep 17 00:00:00 2001 From: Gregor Zeitlinger Date: Wed, 15 May 2024 15:21:08 +0200 Subject: [PATCH 4/5] better bean name --- .../OpenTelemetryAutoConfiguration.java | 12 ++++++------ .../OpenTelemetryAutoConfigurationTest.java | 4 ++-- 2 files changed, 8 insertions(+), 8 deletions(-) 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 d02f53fb1be0..6ac051a0ce1e 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 @@ -111,8 +111,8 @@ public OpenTelemetry openTelemetry( return autoConfiguredOpenTelemetrySdk.getOpenTelemetrySdk(); } - @Bean("configProperties") - public ConfigProperties configProperties( + @Bean + public ConfigProperties otelProperties( AutoConfiguredOpenTelemetrySdk autoConfiguredOpenTelemetrySdk) { return AutoConfigureUtil.getConfig(autoConfiguredOpenTelemetrySdk); } @@ -128,8 +128,8 @@ public OpenTelemetry openTelemetry() { return OpenTelemetry.noop(); } - @Bean("configProperties") - public ConfigProperties configProperties() { + @Bean + public ConfigProperties otelProperties() { return DefaultConfigProperties.createFromMap(Collections.emptyMap()); } } @@ -138,8 +138,8 @@ public ConfigProperties configProperties() { @ConditionalOnBean(OpenTelemetry.class) @ConditionalOnMissingBean({ConfigProperties.class}) public static class FallbackConfigProperties { - @Bean("configProperties") - public ConfigProperties configProperties() { + @Bean + public ConfigProperties otelProperties() { return DefaultConfigProperties.create(Collections.emptyMap()); } } 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 c11e86460ab8..c13eb6e2fce7 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 @@ -49,7 +49,7 @@ void customOpenTelemetry() { assertThat(context) .hasBean("customOpenTelemetry") .doesNotHaveBean("openTelemetry") - .hasBean("configProperties")); + .hasBean("otelProperties")); } @Test @@ -58,7 +58,7 @@ void customOpenTelemetry() { void initializeProvidersAndOpenTelemetry() { this.contextRunner .withConfiguration(AutoConfigurations.of(OpenTelemetryAutoConfiguration.class)) - .run(context -> assertThat(context).hasBean("openTelemetry").hasBean("configProperties")); + .run(context -> assertThat(context).hasBean("openTelemetry").hasBean("otelProperties")); } @Test From 06d9404c6d9fa5997c8d4475e5ebc4523d5c3d5c Mon Sep 17 00:00:00 2001 From: Gregor Zeitlinger Date: Wed, 15 May 2024 19:49:03 +0200 Subject: [PATCH 5/5] comment --- .../OpenTelemetryAutoConfiguration.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) 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 6ac051a0ce1e..a850938821c3 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 @@ -25,6 +25,8 @@ import java.util.Collections; import java.util.List; import java.util.Optional; +import java.util.function.Function; +import java.util.function.Supplier; import java.util.stream.Collectors; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; @@ -111,6 +113,16 @@ public OpenTelemetry openTelemetry( return autoConfiguredOpenTelemetrySdk.getOpenTelemetrySdk(); } + /** + * Expose the {@link ConfigProperties} bean for use in other auto-configurations. + * + *

Why not use spring boot properties directly?
+ * 1. issues with older spring boot versions
+ * 2. support for {@link + * io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizer#addPropertiesCustomizer(Function)} + * and {@link + * io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizer#addPropertiesSupplier(Supplier)} + */ @Bean public ConfigProperties otelProperties( AutoConfiguredOpenTelemetrySdk autoConfiguredOpenTelemetrySdk) {