-
Notifications
You must be signed in to change notification settings - Fork 791
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
0f1090d
commit d355f78
Showing
10 changed files
with
232 additions
and
13 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
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,19 @@ | ||
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" | ||
|
||
dependencies { | ||
val springBootVersion = "3.2.4" | ||
// implementation("org.springframework.boot:spring-boot-autoconfigure:$springBootVersion") | ||
implementation("org.springframework.boot:spring-boot-starter-web:$springBootVersion") | ||
implementation(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") | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
...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,50 @@ | ||
/* | ||
* 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)) { | ||
return bean; | ||
} | ||
return addRestClientInterceptorIfNotPresent( | ||
((RestClient) bean), openTelemetryProvider.getObject()); | ||
} | ||
|
||
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(); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
.../spring/autoconfigure/instrumentation/web/SpringWeb6InstrumentationAutoConfiguration.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,48 @@ | ||
/* | ||
* 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 io.opentelemetry.testing.internal.armeria.client.RestClient; | ||
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; | ||
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; | ||
|
||
/** | ||
* 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) | ||
@Configuration | ||
public class SpringWeb6InstrumentationAutoConfiguration { | ||
|
||
public SpringWeb6InstrumentationAutoConfiguration() {} | ||
|
||
@Bean | ||
static RestClientBeanPostProcessor otelRestClientBeanPostProcessor( | ||
ObjectProvider<OpenTelemetry> openTelemetryProvider) { | ||
return new RestClientBeanPostProcessor(openTelemetryProvider); | ||
} | ||
|
||
@Bean | ||
static 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.SpringWeb6InstrumentationAutoConfiguration |
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.SpringWeb6InstrumentationAutoConfiguration |
81 changes: 81 additions & 0 deletions
81
...ing/autoconfigure/instrumentation/web/SpringWeb6InstrumentationAutoConfigurationTest.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 SpringWeb6InstrumentationAutoConfigurationTest { | ||
|
||
private final ApplicationContextRunner contextRunner = | ||
new ApplicationContextRunner() | ||
.withBean(OpenTelemetry.class, OpenTelemetry::noop) | ||
.withBean(RestClient.class, RestClient::create) | ||
.withConfiguration( | ||
AutoConfigurations.of(SpringWeb6InstrumentationAutoConfiguration.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