Skip to content

Commit

Permalink
Replace OpenTelemetrySupplier with ObjectProvider<OpenTelemetry> (#10145
Browse files Browse the repository at this point in the history
)
  • Loading branch information
laurit authored Jan 2, 2024
1 parent d8ba39a commit 4ac6238
Show file tree
Hide file tree
Showing 13 changed files with 55 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
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;
Expand All @@ -35,17 +34,14 @@
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;
import org.springframework.boot.context.properties.ConfigurationPropertiesBinding;
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;

Expand Down Expand Up @@ -193,12 +189,4 @@ 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@

package io.opentelemetry.instrumentation.spring.autoconfigure.instrumentation.kafka;

import io.opentelemetry.instrumentation.spring.autoconfigure.internal.OpenTelemetrySupplier;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.instrumentation.spring.kafka.v2_7.SpringKafkaTelemetry;
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 OpenTelemetrySupplier openTelemetrySupplier;
private final ObjectProvider<OpenTelemetry> openTelemetryProvider;

ConcurrentKafkaListenerContainerFactoryPostProcessor(
OpenTelemetrySupplier openTelemetrySupplier) {
this.openTelemetrySupplier = openTelemetrySupplier;
ObjectProvider<OpenTelemetry> openTelemetryProvider) {
this.openTelemetryProvider = openTelemetryProvider;
}

@Override
Expand All @@ -28,7 +29,7 @@ public Object postProcessAfterInitialization(Object bean, String beanName) {
ConcurrentKafkaListenerContainerFactory<?, ?> listenerContainerFactory =
(ConcurrentKafkaListenerContainerFactory<?, ?>) bean;
SpringKafkaTelemetry springKafkaTelemetry =
SpringKafkaTelemetry.create(openTelemetrySupplier.get());
SpringKafkaTelemetry.create(openTelemetryProvider.getObject());
listenerContainerFactory.setBatchInterceptor(springKafkaTelemetry.createBatchInterceptor());
listenerContainerFactory.setRecordInterceptor(springKafkaTelemetry.createRecordInterceptor());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +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.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
Expand All @@ -33,7 +33,7 @@ DefaultKafkaProducerFactoryCustomizer otelKafkaProducerFactoryCustomizer(
@Bean
static ConcurrentKafkaListenerContainerFactoryPostProcessor
otelKafkaListenerContainerFactoryBeanPostProcessor(
OpenTelemetrySupplier openTelemetrySupplier) {
return new ConcurrentKafkaListenerContainerFactoryPostProcessor(openTelemetrySupplier);
ObjectProvider<OpenTelemetry> openTelemetryProvider) {
return new ConcurrentKafkaListenerContainerFactoryPostProcessor(openTelemetryProvider);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,20 @@

package io.opentelemetry.instrumentation.spring.autoconfigure.instrumentation.web;

import io.opentelemetry.instrumentation.spring.autoconfigure.internal.OpenTelemetrySupplier;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.instrumentation.spring.web.v3_1.SpringWebTelemetry;
import java.util.List;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.web.client.RestTemplate;

final class RestTemplateBeanPostProcessor implements BeanPostProcessor {

private final OpenTelemetrySupplier openTelemetrySupplier;
private final ObjectProvider<OpenTelemetry> openTelemetryProvider;

RestTemplateBeanPostProcessor(OpenTelemetrySupplier openTelemetrySupplier) {
this.openTelemetrySupplier = openTelemetrySupplier;
RestTemplateBeanPostProcessor(ObjectProvider<OpenTelemetry> openTelemetryProvider) {
this.openTelemetryProvider = openTelemetryProvider;
}

@Override
Expand All @@ -28,7 +29,7 @@ public Object postProcessAfterInitialization(Object bean, String beanName) {

RestTemplate restTemplate = (RestTemplate) bean;
ClientHttpRequestInterceptor interceptor =
SpringWebTelemetry.create(openTelemetrySupplier.get()).newInterceptor();
SpringWebTelemetry.create(openTelemetryProvider.getObject()).newInterceptor();
addRestTemplateInterceptorIfNotPresent(restTemplate, interceptor);
return restTemplate;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +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.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
Expand All @@ -29,7 +29,7 @@ public SpringWebInstrumentationAutoConfiguration() {}

@Bean
static RestTemplateBeanPostProcessor otelRestTemplateBeanPostProcessor(
OpenTelemetrySupplier openTelemetrySupplier) {
return new RestTemplateBeanPostProcessor(openTelemetrySupplier);
ObjectProvider<OpenTelemetry> openTelemetryProvider) {
return new RestTemplateBeanPostProcessor(openTelemetryProvider);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +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.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
Expand All @@ -29,7 +29,7 @@ public SpringWebfluxInstrumentationAutoConfiguration() {}

@Bean
static WebClientBeanPostProcessor otelWebClientBeanPostProcessor(
OpenTelemetrySupplier openTelemetrySupplier) {
return new WebClientBeanPostProcessor(openTelemetrySupplier);
ObjectProvider<OpenTelemetry> openTelemetryProvider) {
return new WebClientBeanPostProcessor(openTelemetryProvider);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@

package io.opentelemetry.instrumentation.spring.autoconfigure.instrumentation.webflux;

import io.opentelemetry.instrumentation.spring.autoconfigure.internal.OpenTelemetrySupplier;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.instrumentation.spring.webflux.v5_3.SpringWebfluxTelemetry;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.web.reactive.function.client.WebClient;

Expand All @@ -17,10 +18,10 @@
*/
final class WebClientBeanPostProcessor implements BeanPostProcessor {

private final OpenTelemetrySupplier openTelemetrySupplier;
private final ObjectProvider<OpenTelemetry> openTelemetryProvider;

WebClientBeanPostProcessor(OpenTelemetrySupplier openTelemetrySupplier) {
this.openTelemetrySupplier = openTelemetrySupplier;
WebClientBeanPostProcessor(ObjectProvider<OpenTelemetry> openTelemetryProvider) {
this.openTelemetryProvider = openTelemetryProvider;
}

@Override
Expand All @@ -37,7 +38,7 @@ public Object postProcessAfterInitialization(Object bean, String beanName) {

private WebClient.Builder wrapBuilder(WebClient.Builder webClientBuilder) {
SpringWebfluxTelemetry instrumentation =
SpringWebfluxTelemetry.create(openTelemetrySupplier.get());
SpringWebfluxTelemetry.create(openTelemetryProvider.getObject());
return webClientBuilder.filters(instrumentation::addClientTracingFilter);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.instrumentation.spring.autoconfigure.internal.OpenTelemetrySupplier;
import io.opentelemetry.instrumentation.testing.junit.LibraryInstrumentationExtension;
import io.opentelemetry.semconv.SemanticAttributes;
import java.time.Duration;
Expand Down Expand Up @@ -69,10 +68,6 @@ void setUpContext() {
KafkaInstrumentationAutoConfiguration.class,
TestConfig.class))
.withBean("openTelemetry", OpenTelemetry.class, testing::getOpenTelemetry)
.withBean(
"openTelemetrySupplier",
OpenTelemetrySupplier.class,
() -> testing::getOpenTelemetry)
.withPropertyValues(
"spring.kafka.bootstrap-servers=" + kafka.getBootstrapServers(),
"spring.kafka.consumer.auto-offset-reset=earliest",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,22 @@
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.web.client.RestTemplate;

class RestTemplateBeanPostProcessorTest {
private static final DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();

static {
beanFactory.registerSingleton("openTelemetry", OpenTelemetry.noop());
}

@Test
@DisplayName("when processed bean is not of type RestTemplate should return object")
void returnsObject() {
BeanPostProcessor underTest = new RestTemplateBeanPostProcessor(() -> OpenTelemetry.noop());
BeanPostProcessor underTest =
new RestTemplateBeanPostProcessor(beanFactory.getBeanProvider(OpenTelemetry.class));

assertThat(underTest.postProcessAfterInitialization(new Object(), "testObject"))
.isExactlyInstanceOf(Object.class);
Expand All @@ -28,7 +35,8 @@ void returnsObject() {
@Test
@DisplayName("when processed bean is of type RestTemplate should return RestTemplate")
void returnsRestTemplate() {
BeanPostProcessor underTest = new RestTemplateBeanPostProcessor(() -> OpenTelemetry.noop());
BeanPostProcessor underTest =
new RestTemplateBeanPostProcessor(beanFactory.getBeanProvider(OpenTelemetry.class));

assertThat(underTest.postProcessAfterInitialization(new RestTemplate(), "testRestTemplate"))
.isInstanceOf(RestTemplate.class);
Expand All @@ -37,7 +45,8 @@ void returnsRestTemplate() {
@Test
@DisplayName("when processed bean is of type RestTemplate should add ONE RestTemplateInterceptor")
void addsRestTemplateInterceptor() {
BeanPostProcessor underTest = new RestTemplateBeanPostProcessor(() -> OpenTelemetry.noop());
BeanPostProcessor underTest =
new RestTemplateBeanPostProcessor(beanFactory.getBeanProvider(OpenTelemetry.class));

RestTemplate restTemplate = new RestTemplate();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import static org.assertj.core.api.Assertions.assertThat;

import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.instrumentation.spring.autoconfigure.internal.OpenTelemetrySupplier;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
Expand All @@ -18,7 +17,6 @@ class SpringWebInstrumentationAutoConfigurationTest {
private final ApplicationContextRunner contextRunner =
new ApplicationContextRunner()
.withBean(OpenTelemetry.class, OpenTelemetry::noop)
.withBean(OpenTelemetrySupplier.class, () -> OpenTelemetry::noop)
.withConfiguration(
AutoConfigurations.of(SpringWebInstrumentationAutoConfiguration.class));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import static org.assertj.core.api.Assertions.assertThat;

import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.instrumentation.spring.autoconfigure.internal.OpenTelemetrySupplier;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
Expand All @@ -18,7 +17,6 @@ class SpringWebfluxInstrumentationAutoConfigurationTest {
private final ApplicationContextRunner contextRunner =
new ApplicationContextRunner()
.withBean(OpenTelemetry.class, OpenTelemetry::noop)
.withBean(OpenTelemetrySupplier.class, () -> OpenTelemetry::noop)
.withConfiguration(
AutoConfigurations.of(SpringWebfluxInstrumentationAutoConfiguration.class));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,23 @@
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.web.reactive.function.client.ExchangeFilterFunction;
import org.springframework.web.reactive.function.client.WebClient;

class WebClientBeanPostProcessorTest {
private static final DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();

static {
beanFactory.registerSingleton("openTelemetry", OpenTelemetry.noop());
}

@Test
@DisplayName(
"when processed bean is NOT of type WebClient or WebClientBuilder should return Object")
void returnsObject() {
BeanPostProcessor underTest = new WebClientBeanPostProcessor(() -> OpenTelemetry.noop());
BeanPostProcessor underTest =
new WebClientBeanPostProcessor(beanFactory.getBeanProvider(OpenTelemetry.class));

assertThat(underTest.postProcessAfterInitialization(new Object(), "testObject"))
.isExactlyInstanceOf(Object.class);
Expand All @@ -29,7 +36,8 @@ void returnsObject() {
@Test
@DisplayName("when processed bean is of type WebClient should return WebClient")
void returnsWebClient() {
BeanPostProcessor underTest = new WebClientBeanPostProcessor(() -> OpenTelemetry.noop());
BeanPostProcessor underTest =
new WebClientBeanPostProcessor(beanFactory.getBeanProvider(OpenTelemetry.class));

assertThat(underTest.postProcessAfterInitialization(WebClient.create(), "testWebClient"))
.isInstanceOf(WebClient.class);
Expand All @@ -38,7 +46,8 @@ void returnsWebClient() {
@Test
@DisplayName("when processed bean is of type WebClientBuilder should return WebClientBuilder")
void returnsWebClientBuilder() {
BeanPostProcessor underTest = new WebClientBeanPostProcessor(() -> OpenTelemetry.noop());
BeanPostProcessor underTest =
new WebClientBeanPostProcessor(beanFactory.getBeanProvider(OpenTelemetry.class));

assertThat(
underTest.postProcessAfterInitialization(WebClient.builder(), "testWebClientBuilder"))
Expand All @@ -48,7 +57,8 @@ void returnsWebClientBuilder() {
@Test
@DisplayName("when processed bean is of type WebClient should add exchange filter to WebClient")
void addsExchangeFilterWebClient() {
BeanPostProcessor underTest = new WebClientBeanPostProcessor(() -> OpenTelemetry.noop());
BeanPostProcessor underTest =
new WebClientBeanPostProcessor(beanFactory.getBeanProvider(OpenTelemetry.class));

WebClient webClient = WebClient.create();
Object processedWebClient =
Expand All @@ -70,7 +80,8 @@ void addsExchangeFilterWebClient() {
@DisplayName(
"when processed bean is of type WebClientBuilder should add ONE exchange filter to WebClientBuilder")
void addsExchangeFilterWebClientBuilder() {
BeanPostProcessor underTest = new WebClientBeanPostProcessor(() -> OpenTelemetry.noop());
BeanPostProcessor underTest =
new WebClientBeanPostProcessor(beanFactory.getBeanProvider(OpenTelemetry.class));

WebClient.Builder webClientBuilder = WebClient.builder();
underTest.postProcessAfterInitialization(webClientBuilder, "testWebClientBuilder");
Expand Down

0 comments on commit 4ac6238

Please sign in to comment.