Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import io.opentelemetry.instrumentation.spring.autoconfigure.internal.properties.InstrumentationConfigUtil;
import io.opentelemetry.instrumentation.spring.web.v3_1.SpringWebTelemetry;
import io.opentelemetry.instrumentation.spring.web.v3_1.internal.WebTelemetryUtil;
import java.util.concurrent.atomic.AtomicBoolean;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.http.client.ClientHttpRequestInterceptor;
Expand Down Expand Up @@ -40,18 +41,26 @@ private static RestClient addRestClientInterceptorIfNotPresent(
RestClient restClient, OpenTelemetry openTelemetry, InstrumentationConfig config) {
ClientHttpRequestInterceptor instrumentationInterceptor = getInterceptor(openTelemetry, config);

return restClient
.mutate()
.requestInterceptors(
interceptors -> {
if (interceptors.stream()
.noneMatch(
interceptor ->
interceptor.getClass() == instrumentationInterceptor.getClass())) {
interceptors.add(0, instrumentationInterceptor);
}
})
.build();
AtomicBoolean shouldAddInterceptor = new AtomicBoolean(false);
RestClient.Builder result =
restClient
.mutate()
.requestInterceptors(
interceptors -> {
if (isInterceptorNotPresent(interceptors, instrumentationInterceptor)) {
interceptors.add(0, instrumentationInterceptor);
shouldAddInterceptor.set(true);
}
});

return shouldAddInterceptor.get() ? result.build() : restClient;
}

private static boolean isInterceptorNotPresent(
java.util.List<ClientHttpRequestInterceptor> interceptors,
ClientHttpRequestInterceptor instrumentationInterceptor) {
return interceptors.stream()
.noneMatch(interceptor -> interceptor.getClass() == instrumentationInterceptor.getClass());
}

static ClientHttpRequestInterceptor getInterceptor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,48 @@ void defaultConfiguration() {
"otelRestClientBeanPostProcessor", RestClientBeanPostProcessor.class))
.isNotNull());
}

@Test
void shouldNotCreateNewBeanWhenInterceptorAlreadyPresent() {
contextRunner
.withPropertyValues("otel.instrumentation.spring-web.enabled=true")
.run(
context -> {
RestClientBeanPostProcessor beanPostProcessor =
context.getBean(
"otelRestClientBeanPostProcessor", RestClientBeanPostProcessor.class);

RestClient restClientWithInterceptor =
RestClient.builder()
.requestInterceptor(
RestClientBeanPostProcessor.getInterceptor(
context.getBean(OpenTelemetry.class),
context.getBean(InstrumentationConfig.class)))
.build();

RestClient processed =
(RestClient)
beanPostProcessor.postProcessAfterInitialization(
restClientWithInterceptor, "testBean");

// Should return the same instance when interceptor is already present
assertThat(processed).isSameAs(restClientWithInterceptor);

// Verify only one interceptor exists
processed
.mutate()
.requestInterceptors(
interceptors -> {
long count =
interceptors.stream()
.filter(
rti ->
rti.getClass()
.getName()
.startsWith("io.opentelemetry.instrumentation"))
.count();
assertThat(count).isEqualTo(1);
});
});
}
}
Loading