Skip to content

Commit c543d91

Browse files
committed
Polish "Add auto-configuration for OTLP span exporter"
See gh-34508
1 parent ceaafec commit c543d91

File tree

4 files changed

+12
-17
lines changed

4 files changed

+12
-17
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/tracing/otlp/OtlpAutoConfiguration.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import io.opentelemetry.api.OpenTelemetry;
2323
import io.opentelemetry.exporter.otlp.http.trace.OtlpHttpSpanExporter;
2424
import io.opentelemetry.exporter.otlp.http.trace.OtlpHttpSpanExporterBuilder;
25+
import io.opentelemetry.exporter.otlp.trace.OtlpGrpcSpanExporter;
2526
import io.opentelemetry.sdk.trace.SdkTracerProvider;
2627

2728
import org.springframework.boot.actuate.autoconfigure.tracing.ConditionalOnEnabledTracing;
@@ -41,7 +42,7 @@
4142
* "https://github.com/open-telemetry/opentelemetry-java/issues/3651">opentelemetry-java#3651</a>.
4243
* Because this class configures components from the OTel SDK, it can't support HTTP/JSON.
4344
* To keep things simple, we only auto-configure HTTP/protobuf. If you want to use gRPC,
44-
* please disable this auto-configuration and create a bean.
45+
* define an {@link OtlpGrpcSpanExporter} and this auto-configuration will back off.
4546
*
4647
* @author Jonatan Ivanov
4748
* @since 3.1.0
@@ -53,19 +54,17 @@
5354
public class OtlpAutoConfiguration {
5455

5556
@Bean
56-
@ConditionalOnMissingBean
57+
@ConditionalOnMissingBean(value = OtlpHttpSpanExporter.class,
58+
type = "io.opentelemetry.exporter.otlp.trace.OtlpGrpcSpanExporter")
5759
OtlpHttpSpanExporter otlpHttpSpanExporter(OtlpProperties properties) {
5860
OtlpHttpSpanExporterBuilder builder = OtlpHttpSpanExporter.builder()
5961
.setEndpoint(properties.getEndpoint())
6062
.setTimeout(properties.getTimeout())
6163
.setCompression(properties.getCompression().name().toLowerCase());
62-
6364
for (Entry<String, String> header : properties.getHeaders().entrySet()) {
6465
builder.addHeader(header.getKey(), header.getValue());
6566
}
66-
6767
return builder.build();
68-
6968
}
7069

7170
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/tracing/otlp/OtlpProperties.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import org.springframework.boot.context.properties.ConfigurationProperties;
2424

2525
/**
26-
* Configuration properties for {@link OtlpAutoConfiguration}.
26+
* Configuration properties for exporting traces using OTLP.
2727
*
2828
* @author Jonatan Ivanov
2929
* @since 3.1.0

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/tracing/otlp/OtlpAutoConfigurationIntegrationTests.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,10 @@ class OtlpAutoConfigurationIntegrationTests {
5454
AutoConfigurations.of(ObservationAutoConfiguration.class, MicrometerTracingAutoConfiguration.class,
5555
OpenTelemetryAutoConfiguration.class, OtlpAutoConfiguration.class));
5656

57-
private MockWebServer mockWebServer;
57+
private final MockWebServer mockWebServer = new MockWebServer();
5858

5959
@BeforeEach
6060
void setUp() throws IOException {
61-
this.mockWebServer = new MockWebServer();
6261
this.mockWebServer.start();
6362
}
6463

@@ -68,7 +67,7 @@ void tearDown() throws IOException {
6867
}
6968

7069
@Test
71-
void httpSpanExporterShouldUseProtoBufAndNoCompression() {
70+
void httpSpanExporterShouldUseProtoBufAndNoCompressionByDefault() {
7271
this.mockWebServer.enqueue(new MockResponse());
7372
this.contextRunner
7473
.withPropertyValues("management.otlp.tracing.endpoint=http://localhost:%d/v1/traces"
@@ -77,7 +76,6 @@ void httpSpanExporterShouldUseProtoBufAndNoCompression() {
7776
context.getBean(Tracer.class).nextSpan().name("test").end();
7877
assertThat(context.getBean(OtlpHttpSpanExporter.class).flush())
7978
.isSameAs(CompletableResultCode.ofSuccess());
80-
8179
RecordedRequest request = this.mockWebServer.takeRequest(10, TimeUnit.SECONDS);
8280
assertThat(request).isNotNull();
8381
assertThat(request.getRequestLine()).contains("/v1/traces");
@@ -91,17 +89,16 @@ void httpSpanExporterShouldUseProtoBufAndNoCompression() {
9189
}
9290

9391
@Test
94-
void httpSpanExporterShouldUseProtoBufAndGzip() {
92+
void httpSpanExporterCanBeConfiguredToUseGzipCompression() {
9593
this.mockWebServer.enqueue(new MockResponse());
9694
this.contextRunner
97-
.withPropertyValues("management.otlp.tracing.compression=GZIP",
95+
.withPropertyValues("management.otlp.tracing.compression=gzip",
9896
"management.otlp.tracing.endpoint=http://localhost:%d/test".formatted(this.mockWebServer.getPort()))
9997
.run((context) -> {
10098
assertThat(context).hasSingleBean(OtlpHttpSpanExporter.class).hasSingleBean(SpanExporter.class);
10199
context.getBean(Tracer.class).nextSpan().name("test").end();
102100
assertThat(context.getBean(OtlpHttpSpanExporter.class).flush())
103101
.isSameAs(CompletableResultCode.ofSuccess());
104-
105102
RecordedRequest request = this.mockWebServer.takeRequest(10, TimeUnit.SECONDS);
106103
assertThat(request).isNotNull();
107104
assertThat(request.getRequestLine()).contains("/test");

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/tracing/otlp/OtlpAutoConfigurationTests.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,15 @@ void shouldNotSupplyBeansIfExporterIsMissing() {
7676
}
7777

7878
@Test
79-
void shouldSupplyCustomHttpExporter() {
79+
void shouldBackOffWhenCustomHttpExporterIsDefined() {
8080
this.contextRunner.withUserConfiguration(CustomHttpExporterConfiguration.class)
8181
.run((context) -> assertThat(context).hasBean("customOtlpHttpSpanExporter")
8282
.hasSingleBean(SpanExporter.class));
8383
}
8484

8585
@Test
86-
void shouldSupplyCustomGrpcExporter() {
87-
this.contextRunner.withClassLoader(new FilteredClassLoader("io.opentelemetry.exporter"))
88-
.withUserConfiguration(CustomGrpcExporterConfiguration.class)
86+
void shouldBackOffWhenCustomGrpcExporterIsDefined() {
87+
this.contextRunner.withUserConfiguration(CustomGrpcExporterConfiguration.class)
8988
.run((context) -> assertThat(context).hasBean("customOtlpGrpcSpanExporter")
9089
.hasSingleBean(SpanExporter.class));
9190
}

0 commit comments

Comments
 (0)