-
Notifications
You must be signed in to change notification settings - Fork 881
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
985c0f6
commit f71537a
Showing
11 changed files
with
241 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Why do we need a separate module for Spring Boot 3 auto-configuration? | ||
|
||
`RestClientInstrumentationAutoConfiguration` imports `RestClientCustomizer`, | ||
which is part of Spring Boot 3 (rather than Spring framework). | ||
|
||
If we were to include this in the `spring-boot-autoconfigure` module, we would have to | ||
bump the Spring Boot version to 3, which would break compatibility with Spring Boot 2. |
22 changes: 22 additions & 0 deletions
22
instrumentation/spring/spring-boot-autoconfigure-3/build.gradle.kts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
plugins { | ||
id("otel.library-instrumentation") | ||
} | ||
|
||
// Name the Spring Boot modules in accordance with https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.developing-auto-configuration.custom-starter | ||
base.archivesName.set("opentelemetry-spring-boot-3") | ||
group = "io.opentelemetry.instrumentation" | ||
|
||
otelJava { | ||
minJavaVersionSupported.set(JavaVersion.VERSION_17) | ||
} | ||
|
||
dependencies { | ||
val springBootVersion = "3.2.4" | ||
library("org.springframework.boot:spring-boot-starter-web:$springBootVersion") | ||
compileOnly(project(":instrumentation:spring:spring-boot-autoconfigure")) | ||
implementation(project(":instrumentation:spring:spring-web:spring-web-3.1:library")) | ||
|
||
testLibrary("org.springframework.boot:spring-boot-starter-test:$springBootVersion") { | ||
exclude("org.junit.vintage", "junit-vintage-engine") | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
...instrumentation/spring/autoconfigure/instrumentation/web/RestClientBeanPostProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.spring.autoconfigure.instrumentation.web; | ||
|
||
import io.opentelemetry.api.OpenTelemetry; | ||
import io.opentelemetry.instrumentation.spring.web.v3_1.SpringWebTelemetry; | ||
import org.springframework.beans.factory.ObjectProvider; | ||
import org.springframework.beans.factory.config.BeanPostProcessor; | ||
import org.springframework.http.client.ClientHttpRequestInterceptor; | ||
import org.springframework.web.client.RestClient; | ||
|
||
public final class RestClientBeanPostProcessor implements BeanPostProcessor { | ||
|
||
private final ObjectProvider<OpenTelemetry> openTelemetryProvider; | ||
|
||
public RestClientBeanPostProcessor(ObjectProvider<OpenTelemetry> openTelemetryProvider) { | ||
this.openTelemetryProvider = openTelemetryProvider; | ||
} | ||
|
||
@Override | ||
public Object postProcessAfterInitialization(Object bean, String beanName) { | ||
if (bean instanceof RestClient restClient) { | ||
return addRestClientInterceptorIfNotPresent(restClient, openTelemetryProvider.getObject()); | ||
} | ||
return bean; | ||
} | ||
|
||
private static RestClient addRestClientInterceptorIfNotPresent( | ||
RestClient restClient, OpenTelemetry openTelemetry) { | ||
ClientHttpRequestInterceptor instrumentationInterceptor = | ||
SpringWebTelemetry.create(openTelemetry).newInterceptor(); | ||
|
||
return restClient | ||
.mutate() | ||
.requestInterceptors( | ||
interceptors -> { | ||
if (interceptors.stream() | ||
.noneMatch( | ||
interceptor -> | ||
interceptor.getClass() == instrumentationInterceptor.getClass())) { | ||
interceptors.add(0, instrumentationInterceptor); | ||
} | ||
}) | ||
.build(); | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
.../spring/autoconfigure/instrumentation/web/RestClientInstrumentationAutoConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.spring.autoconfigure.instrumentation.web; | ||
|
||
import io.opentelemetry.api.OpenTelemetry; | ||
import io.opentelemetry.instrumentation.spring.autoconfigure.internal.SdkEnabled; | ||
import io.opentelemetry.instrumentation.spring.web.v3_1.SpringWebTelemetry; | ||
import org.springframework.beans.factory.ObjectProvider; | ||
import org.springframework.boot.autoconfigure.AutoConfiguration; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.boot.autoconfigure.web.client.RestClientAutoConfiguration; | ||
import org.springframework.boot.web.client.RestClientCustomizer; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Conditional; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.client.RestClient; | ||
|
||
/** | ||
* Configures {@link RestClient} for tracing. | ||
* | ||
* <p>Adds Open Telemetry instrumentation to {@link RestClient} beans after initialization | ||
*/ | ||
@ConditionalOnBean(OpenTelemetry.class) | ||
@ConditionalOnProperty(name = "otel.instrumentation.spring-web.enabled", matchIfMissing = true) | ||
@ConditionalOnClass(RestClient.class) | ||
@Conditional(SdkEnabled.class) | ||
@AutoConfiguration(after = RestClientAutoConfiguration.class) | ||
@Configuration | ||
public class RestClientInstrumentationAutoConfiguration { | ||
|
||
@Bean | ||
RestClientBeanPostProcessor otelRestClientBeanPostProcessor( | ||
ObjectProvider<OpenTelemetry> openTelemetryProvider) { | ||
return new RestClientBeanPostProcessor(openTelemetryProvider); | ||
} | ||
|
||
@Bean | ||
RestClientCustomizer otelRestClientCustomizer( | ||
ObjectProvider<OpenTelemetry> openTelemetryProvider) { | ||
return builder -> | ||
builder.requestInterceptor( | ||
SpringWebTelemetry.create(openTelemetryProvider.getObject()).newInterceptor()); | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
...mentation/spring/spring-boot-autoconfigure-3/src/main/resources/META-INF/spring.factories
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ | ||
io.opentelemetry.instrumentation.spring.autoconfigure.instrumentation.web.RestClientInstrumentationAutoConfiguration |
1 change: 1 addition & 0 deletions
1
...esources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
io.opentelemetry.instrumentation.spring.autoconfigure.instrumentation.web.RestClientInstrumentationAutoConfiguration |
81 changes: 81 additions & 0 deletions
81
...ing/autoconfigure/instrumentation/web/RestClientInstrumentationAutoConfigurationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.spring.autoconfigure.instrumentation.web; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import io.opentelemetry.api.OpenTelemetry; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.boot.autoconfigure.AutoConfigurations; | ||
import org.springframework.boot.test.context.runner.ApplicationContextRunner; | ||
import org.springframework.web.client.RestClient; | ||
|
||
class RestClientInstrumentationAutoConfigurationTest { | ||
|
||
private final ApplicationContextRunner contextRunner = | ||
new ApplicationContextRunner() | ||
.withBean(OpenTelemetry.class, OpenTelemetry::noop) | ||
.withBean(RestClient.class, RestClient::create) | ||
.withConfiguration( | ||
AutoConfigurations.of(RestClientInstrumentationAutoConfiguration.class)); | ||
|
||
/** | ||
* Tests the case that users create a {@link RestClient} bean themselves. | ||
* | ||
* <pre>{@code | ||
* @Bean public RestClient restClient() { | ||
* return new RestClient(); | ||
* } | ||
* }</pre> | ||
*/ | ||
@Test | ||
void instrumentationEnabled() { | ||
contextRunner | ||
.withPropertyValues("otel.instrumentation.spring-web.enabled=true") | ||
.run( | ||
context -> { | ||
assertThat( | ||
context.getBean( | ||
"otelRestClientBeanPostProcessor", RestClientBeanPostProcessor.class)) | ||
.isNotNull(); | ||
|
||
context | ||
.getBean(RestClient.class) | ||
.mutate() | ||
.requestInterceptors( | ||
interceptors -> { | ||
long count = | ||
interceptors.stream() | ||
.filter( | ||
rti -> | ||
rti.getClass() | ||
.getName() | ||
.startsWith("io.opentelemetry.instrumentation")) | ||
.count(); | ||
assertThat(count).isEqualTo(1); | ||
}); | ||
}); | ||
} | ||
|
||
@Test | ||
void instrumentationDisabled() { | ||
contextRunner | ||
.withPropertyValues("otel.instrumentation.spring-web.enabled=false") | ||
.run( | ||
context -> | ||
assertThat(context.containsBean("otelRestClientBeanPostProcessor")).isFalse()); | ||
} | ||
|
||
@Test | ||
void defaultConfiguration() { | ||
contextRunner.run( | ||
context -> | ||
assertThat( | ||
context.getBean( | ||
"otelRestClientBeanPostProcessor", RestClientBeanPostProcessor.class)) | ||
.isNotNull()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters