Skip to content

Commit

Permalink
use ConfigProperties instead of spring style injection, because it's …
Browse files Browse the repository at this point in the history
…compatible with all spring versions, honors properties customizers, and easier to find usages
  • Loading branch information
zeitlinger committed May 3, 2024
1 parent b7effb0 commit c8b34e1
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}

Expand All @@ -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());
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,21 @@

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;

class ConcurrentKafkaListenerContainerFactoryPostProcessor implements BeanPostProcessor {

private final ObjectProvider<OpenTelemetry> openTelemetryProvider;
private final boolean experimentalSpanAttributes;
private final ObjectProvider<ConfigProperties> configPropertiesProvider;

ConcurrentKafkaListenerContainerFactoryPostProcessor(
ObjectProvider<OpenTelemetry> openTelemetryProvider, boolean experimentalSpanAttributes) {
ObjectProvider<OpenTelemetry> openTelemetryProvider,
ObjectProvider<ConfigProperties> configPropertiesProvider) {
this.openTelemetryProvider = openTelemetryProvider;
this.experimentalSpanAttributes = experimentalSpanAttributes;
this.configPropertiesProvider = configPropertiesProvider;
}

@Override
Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand All @@ -39,9 +39,8 @@ DefaultKafkaProducerFactoryCustomizer otelKafkaProducerFactoryCustomizer(
static ConcurrentKafkaListenerContainerFactoryPostProcessor
otelKafkaListenerContainerFactoryBeanPostProcessor(
ObjectProvider<OpenTelemetry> openTelemetryProvider,
@Value("${otel.instrumentation.kafka.experimental-span-attributes:false}")
boolean experimentalSpanAttributes) {
ObjectProvider<ConfigProperties> configPropertiesProvider) {
return new ConcurrentKafkaListenerContainerFactoryPostProcessor(
openTelemetryProvider, experimentalSpanAttributes);
openTelemetryProvider, configPropertiesProvider);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ void customOpenTelemetry() {
context ->
assertThat(context)
.hasBean("customOpenTelemetry")
.doesNotHaveBean("openTelemetry"));
.doesNotHaveBean("openTelemetry")
.hasBean("configProperties"));
}

@Test
Expand All @@ -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
Expand Down

0 comments on commit c8b34e1

Please sign in to comment.