Skip to content

Commit

Permalink
Polish "Add auto-configuration for OTLP span exporter"
Browse files Browse the repository at this point in the history
  • Loading branch information
wilkinsona committed Apr 19, 2023
1 parent ceaafec commit c543d91
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.exporter.otlp.http.trace.OtlpHttpSpanExporter;
import io.opentelemetry.exporter.otlp.http.trace.OtlpHttpSpanExporterBuilder;
import io.opentelemetry.exporter.otlp.trace.OtlpGrpcSpanExporter;
import io.opentelemetry.sdk.trace.SdkTracerProvider;

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

@Bean
@ConditionalOnMissingBean
@ConditionalOnMissingBean(value = OtlpHttpSpanExporter.class,
type = "io.opentelemetry.exporter.otlp.trace.OtlpGrpcSpanExporter")
OtlpHttpSpanExporter otlpHttpSpanExporter(OtlpProperties properties) {
OtlpHttpSpanExporterBuilder builder = OtlpHttpSpanExporter.builder()
.setEndpoint(properties.getEndpoint())
.setTimeout(properties.getTimeout())
.setCompression(properties.getCompression().name().toLowerCase());

for (Entry<String, String> header : properties.getHeaders().entrySet()) {
builder.addHeader(header.getKey(), header.getValue());
}

return builder.build();

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import org.springframework.boot.context.properties.ConfigurationProperties;

/**
* Configuration properties for {@link OtlpAutoConfiguration}.
* Configuration properties for exporting traces using OTLP.
*
* @author Jonatan Ivanov
* @since 3.1.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,10 @@ class OtlpAutoConfigurationIntegrationTests {
AutoConfigurations.of(ObservationAutoConfiguration.class, MicrometerTracingAutoConfiguration.class,
OpenTelemetryAutoConfiguration.class, OtlpAutoConfiguration.class));

private MockWebServer mockWebServer;
private final MockWebServer mockWebServer = new MockWebServer();

@BeforeEach
void setUp() throws IOException {
this.mockWebServer = new MockWebServer();
this.mockWebServer.start();
}

Expand All @@ -68,7 +67,7 @@ void tearDown() throws IOException {
}

@Test
void httpSpanExporterShouldUseProtoBufAndNoCompression() {
void httpSpanExporterShouldUseProtoBufAndNoCompressionByDefault() {
this.mockWebServer.enqueue(new MockResponse());
this.contextRunner
.withPropertyValues("management.otlp.tracing.endpoint=http://localhost:%d/v1/traces"
Expand All @@ -77,7 +76,6 @@ void httpSpanExporterShouldUseProtoBufAndNoCompression() {
context.getBean(Tracer.class).nextSpan().name("test").end();
assertThat(context.getBean(OtlpHttpSpanExporter.class).flush())
.isSameAs(CompletableResultCode.ofSuccess());

RecordedRequest request = this.mockWebServer.takeRequest(10, TimeUnit.SECONDS);
assertThat(request).isNotNull();
assertThat(request.getRequestLine()).contains("/v1/traces");
Expand All @@ -91,17 +89,16 @@ void httpSpanExporterShouldUseProtoBufAndNoCompression() {
}

@Test
void httpSpanExporterShouldUseProtoBufAndGzip() {
void httpSpanExporterCanBeConfiguredToUseGzipCompression() {
this.mockWebServer.enqueue(new MockResponse());
this.contextRunner
.withPropertyValues("management.otlp.tracing.compression=GZIP",
.withPropertyValues("management.otlp.tracing.compression=gzip",
"management.otlp.tracing.endpoint=http://localhost:%d/test".formatted(this.mockWebServer.getPort()))
.run((context) -> {
assertThat(context).hasSingleBean(OtlpHttpSpanExporter.class).hasSingleBean(SpanExporter.class);
context.getBean(Tracer.class).nextSpan().name("test").end();
assertThat(context.getBean(OtlpHttpSpanExporter.class).flush())
.isSameAs(CompletableResultCode.ofSuccess());

RecordedRequest request = this.mockWebServer.takeRequest(10, TimeUnit.SECONDS);
assertThat(request).isNotNull();
assertThat(request.getRequestLine()).contains("/test");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,15 @@ void shouldNotSupplyBeansIfExporterIsMissing() {
}

@Test
void shouldSupplyCustomHttpExporter() {
void shouldBackOffWhenCustomHttpExporterIsDefined() {
this.contextRunner.withUserConfiguration(CustomHttpExporterConfiguration.class)
.run((context) -> assertThat(context).hasBean("customOtlpHttpSpanExporter")
.hasSingleBean(SpanExporter.class));
}

@Test
void shouldSupplyCustomGrpcExporter() {
this.contextRunner.withClassLoader(new FilteredClassLoader("io.opentelemetry.exporter"))
.withUserConfiguration(CustomGrpcExporterConfiguration.class)
void shouldBackOffWhenCustomGrpcExporterIsDefined() {
this.contextRunner.withUserConfiguration(CustomGrpcExporterConfiguration.class)
.run((context) -> assertThat(context).hasBean("customOtlpGrpcSpanExporter")
.hasSingleBean(SpanExporter.class));
}
Expand Down

0 comments on commit c543d91

Please sign in to comment.