Skip to content

Commit ceaafec

Browse files
jonatan-ivanovwilkinsona
authored andcommitted
Add auto-configuration for OTLP span exporter
With these changes an OTLP HTTP/protobuf exporter is auto-configured if opentelemetry-exporter-otlp is on the classpath. See gh-34508
1 parent d4f8576 commit ceaafec

File tree

8 files changed

+436
-1
lines changed

8 files changed

+436
-1
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ dependencies {
7070
optional("io.micrometer:micrometer-registry-wavefront")
7171
optional("io.zipkin.reporter2:zipkin-sender-urlconnection")
7272
optional("io.opentelemetry:opentelemetry-exporter-zipkin")
73+
optional("io.opentelemetry:opentelemetry-exporter-otlp")
7374
optional("io.projectreactor.netty:reactor-netty-http")
7475
optional("io.r2dbc:r2dbc-pool")
7576
optional("io.r2dbc:r2dbc-spi")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright 2012-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.actuate.autoconfigure.tracing.otlp;
18+
19+
import java.util.Map.Entry;
20+
21+
import io.micrometer.tracing.otel.bridge.OtelTracer;
22+
import io.opentelemetry.api.OpenTelemetry;
23+
import io.opentelemetry.exporter.otlp.http.trace.OtlpHttpSpanExporter;
24+
import io.opentelemetry.exporter.otlp.http.trace.OtlpHttpSpanExporterBuilder;
25+
import io.opentelemetry.sdk.trace.SdkTracerProvider;
26+
27+
import org.springframework.boot.actuate.autoconfigure.tracing.ConditionalOnEnabledTracing;
28+
import org.springframework.boot.autoconfigure.AutoConfiguration;
29+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
30+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
31+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
32+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
33+
import org.springframework.context.annotation.Bean;
34+
35+
/**
36+
* {@link EnableAutoConfiguration Auto-configuration} for OTLP. Brave does not support
37+
* OTLP, so we only configure it for OpenTelemetry. OTLP defines three transports that are
38+
* supported: gRPC (/protobuf), HTTP/protobuf, HTTP/JSON. From these transports HTTP/JSON
39+
* is not supported by the OTel Java SDK, and it seems there are no plans supporting it in
40+
* the future, see: <a href=
41+
* "https://github.com/open-telemetry/opentelemetry-java/issues/3651">opentelemetry-java#3651</a>.
42+
* Because this class configures components from the OTel SDK, it can't support HTTP/JSON.
43+
* 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+
*
46+
* @author Jonatan Ivanov
47+
* @since 3.1.0
48+
*/
49+
@AutoConfiguration
50+
@ConditionalOnEnabledTracing
51+
@ConditionalOnClass({ OtelTracer.class, SdkTracerProvider.class, OpenTelemetry.class, OtlpHttpSpanExporter.class })
52+
@EnableConfigurationProperties(OtlpProperties.class)
53+
public class OtlpAutoConfiguration {
54+
55+
@Bean
56+
@ConditionalOnMissingBean
57+
OtlpHttpSpanExporter otlpHttpSpanExporter(OtlpProperties properties) {
58+
OtlpHttpSpanExporterBuilder builder = OtlpHttpSpanExporter.builder()
59+
.setEndpoint(properties.getEndpoint())
60+
.setTimeout(properties.getTimeout())
61+
.setCompression(properties.getCompression().name().toLowerCase());
62+
63+
for (Entry<String, String> header : properties.getHeaders().entrySet()) {
64+
builder.addHeader(header.getKey(), header.getValue());
65+
}
66+
67+
return builder.build();
68+
69+
}
70+
71+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* Copyright 2012-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.actuate.autoconfigure.tracing.otlp;
18+
19+
import java.time.Duration;
20+
import java.util.HashMap;
21+
import java.util.Map;
22+
23+
import org.springframework.boot.context.properties.ConfigurationProperties;
24+
25+
/**
26+
* Configuration properties for {@link OtlpAutoConfiguration}.
27+
*
28+
* @author Jonatan Ivanov
29+
* @since 3.1.0
30+
*/
31+
@ConfigurationProperties("management.otlp.tracing")
32+
public class OtlpProperties {
33+
34+
/**
35+
* URL to the OTel collector's HTTP API.
36+
*/
37+
private String endpoint = "http://localhost:4318/v1/traces";
38+
39+
/**
40+
* Call timeout for the OTel Collector to process an exported batch of data. This
41+
* timeout spans the entire call: resolving DNS, connecting, writing the request body,
42+
* server processing, and reading the response body. If the call requires redirects or
43+
* retries all must complete within one timeout period.
44+
*/
45+
private Duration timeout = Duration.ofSeconds(10);
46+
47+
/**
48+
* The method used to compress the payload.
49+
*/
50+
private Compression compression = Compression.NONE;
51+
52+
/**
53+
* Custom HTTP headers you want to pass to the collector, for example auth headers.
54+
*/
55+
private Map<String, String> headers = new HashMap<>();
56+
57+
public String getEndpoint() {
58+
return this.endpoint;
59+
}
60+
61+
public void setEndpoint(String endpoint) {
62+
this.endpoint = endpoint;
63+
}
64+
65+
public Duration getTimeout() {
66+
return this.timeout;
67+
}
68+
69+
public void setTimeout(Duration timeout) {
70+
this.timeout = timeout;
71+
}
72+
73+
public Compression getCompression() {
74+
return this.compression;
75+
}
76+
77+
public void setCompression(Compression compression) {
78+
this.compression = compression;
79+
}
80+
81+
public Map<String, String> getHeaders() {
82+
return this.headers;
83+
}
84+
85+
public void setHeaders(Map<String, String> headers) {
86+
this.headers = headers;
87+
}
88+
89+
enum Compression {
90+
91+
/**
92+
* Gzip compression.
93+
*/
94+
GZIP,
95+
96+
/**
97+
* No compression.
98+
*/
99+
NONE
100+
101+
}
102+
103+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright 2012-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/**
18+
* Auto-configuration for tracing with OTLP.
19+
*/
20+
package org.springframework.boot.actuate.autoconfigure.tracing.otlp;

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ org.springframework.boot.actuate.autoconfigure.system.DiskSpaceHealthContributor
101101
org.springframework.boot.actuate.autoconfigure.tracing.BraveAutoConfiguration
102102
org.springframework.boot.actuate.autoconfigure.tracing.MicrometerTracingAutoConfiguration
103103
org.springframework.boot.actuate.autoconfigure.tracing.OpenTelemetryAutoConfiguration
104+
org.springframework.boot.actuate.autoconfigure.tracing.otlp.OtlpAutoConfiguration
104105
org.springframework.boot.actuate.autoconfigure.tracing.prometheus.PrometheusExemplarsAutoConfiguration
105106
org.springframework.boot.actuate.autoconfigure.tracing.wavefront.WavefrontTracingAutoConfiguration
106107
org.springframework.boot.actuate.autoconfigure.tracing.zipkin.ZipkinAutoConfiguration
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* Copyright 2012-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.actuate.autoconfigure.tracing.otlp;
18+
19+
import java.io.IOException;
20+
import java.nio.charset.StandardCharsets;
21+
import java.util.concurrent.TimeUnit;
22+
23+
import io.micrometer.tracing.Tracer;
24+
import io.opentelemetry.exporter.otlp.http.trace.OtlpHttpSpanExporter;
25+
import io.opentelemetry.sdk.common.CompletableResultCode;
26+
import io.opentelemetry.sdk.trace.export.SpanExporter;
27+
import okhttp3.mockwebserver.MockResponse;
28+
import okhttp3.mockwebserver.MockWebServer;
29+
import okhttp3.mockwebserver.RecordedRequest;
30+
import okio.Buffer;
31+
import okio.GzipSource;
32+
import org.junit.jupiter.api.AfterEach;
33+
import org.junit.jupiter.api.BeforeEach;
34+
import org.junit.jupiter.api.Test;
35+
36+
import org.springframework.boot.actuate.autoconfigure.observation.ObservationAutoConfiguration;
37+
import org.springframework.boot.actuate.autoconfigure.tracing.MicrometerTracingAutoConfiguration;
38+
import org.springframework.boot.actuate.autoconfigure.tracing.OpenTelemetryAutoConfiguration;
39+
import org.springframework.boot.autoconfigure.AutoConfigurations;
40+
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
41+
42+
import static org.assertj.core.api.Assertions.assertThat;
43+
44+
/**
45+
* Integration tests for {@link OtlpAutoConfiguration}.
46+
*
47+
* @author Jonatan Ivanov
48+
*/
49+
class OtlpAutoConfigurationIntegrationTests {
50+
51+
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
52+
.withPropertyValues("management.tracing.sampling.probability=1.0")
53+
.withConfiguration(
54+
AutoConfigurations.of(ObservationAutoConfiguration.class, MicrometerTracingAutoConfiguration.class,
55+
OpenTelemetryAutoConfiguration.class, OtlpAutoConfiguration.class));
56+
57+
private MockWebServer mockWebServer;
58+
59+
@BeforeEach
60+
void setUp() throws IOException {
61+
this.mockWebServer = new MockWebServer();
62+
this.mockWebServer.start();
63+
}
64+
65+
@AfterEach
66+
void tearDown() throws IOException {
67+
this.mockWebServer.close();
68+
}
69+
70+
@Test
71+
void httpSpanExporterShouldUseProtoBufAndNoCompression() {
72+
this.mockWebServer.enqueue(new MockResponse());
73+
this.contextRunner
74+
.withPropertyValues("management.otlp.tracing.endpoint=http://localhost:%d/v1/traces"
75+
.formatted(this.mockWebServer.getPort()), "management.otlp.tracing.headers.custom=42")
76+
.run((context) -> {
77+
context.getBean(Tracer.class).nextSpan().name("test").end();
78+
assertThat(context.getBean(OtlpHttpSpanExporter.class).flush())
79+
.isSameAs(CompletableResultCode.ofSuccess());
80+
81+
RecordedRequest request = this.mockWebServer.takeRequest(10, TimeUnit.SECONDS);
82+
assertThat(request).isNotNull();
83+
assertThat(request.getRequestLine()).contains("/v1/traces");
84+
assertThat(request.getHeader("Content-Type")).isEqualTo("application/x-protobuf");
85+
assertThat(request.getHeader("custom")).isEqualTo("42");
86+
assertThat(request.getBodySize()).isPositive();
87+
try (Buffer body = request.getBody()) {
88+
assertThat(body.readString(StandardCharsets.UTF_8)).contains("org.springframework.boot");
89+
}
90+
});
91+
}
92+
93+
@Test
94+
void httpSpanExporterShouldUseProtoBufAndGzip() {
95+
this.mockWebServer.enqueue(new MockResponse());
96+
this.contextRunner
97+
.withPropertyValues("management.otlp.tracing.compression=GZIP",
98+
"management.otlp.tracing.endpoint=http://localhost:%d/test".formatted(this.mockWebServer.getPort()))
99+
.run((context) -> {
100+
assertThat(context).hasSingleBean(OtlpHttpSpanExporter.class).hasSingleBean(SpanExporter.class);
101+
context.getBean(Tracer.class).nextSpan().name("test").end();
102+
assertThat(context.getBean(OtlpHttpSpanExporter.class).flush())
103+
.isSameAs(CompletableResultCode.ofSuccess());
104+
105+
RecordedRequest request = this.mockWebServer.takeRequest(10, TimeUnit.SECONDS);
106+
assertThat(request).isNotNull();
107+
assertThat(request.getRequestLine()).contains("/test");
108+
assertThat(request.getHeader("Content-Type")).isEqualTo("application/x-protobuf");
109+
assertThat(request.getHeader("Content-Encoding")).isEqualTo("gzip");
110+
assertThat(request.getBodySize()).isPositive();
111+
try (Buffer unCompressed = new Buffer(); Buffer body = request.getBody()) {
112+
unCompressed.writeAll(new GzipSource(body));
113+
assertThat(unCompressed.readString(StandardCharsets.UTF_8)).contains("org.springframework.boot");
114+
}
115+
});
116+
}
117+
118+
}

0 commit comments

Comments
 (0)