-
Notifications
You must be signed in to change notification settings - Fork 967
Spring rest client #11038
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Spring rest client #11038
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b71c9f6
add instrumentation for RestClient
zeitlinger 3a51a2f
add instrumentation for RestClient
zeitlinger a5cb767
use library dependency for spring boot
zeitlinger 2a651f0
rename auto config class, configure after spring class
zeitlinger 7f080f1
fix dependency
zeitlinger 8245b66
address review comments
zeitlinger 038d31b
bean methods should not be static
zeitlinger 58ad4ea
simplify test
zeitlinger File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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) { | ||
jeanbisutti marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (bean instanceof RestClient restClient) { | ||
return addRestClientInterceptorIfNotPresent(restClient, openTelemetryProvider.getObject()); | ||
} | ||
return bean; | ||
} | ||
zeitlinger marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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 hidden or 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) | ||
zeitlinger marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.