Skip to content

Commit abf923e

Browse files
committed
Polish "Add auto-configuration for OTLP gRPC format when using tracing"
See gh-41213
1 parent 6b50d67 commit abf923e

File tree

4 files changed

+23
-35
lines changed

4 files changed

+23
-35
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public class OtlpProperties {
4545
private Duration timeout = Duration.ofSeconds(10);
4646

4747
/**
48-
* Transport used to send the spans. Defaults to HTTP.
48+
* Transport used to send the spans.
4949
*/
5050
private Transport transport = Transport.HTTP;
5151

@@ -99,21 +99,21 @@ public void setHeaders(Map<String, String> headers) {
9999
this.headers = headers;
100100
}
101101

102-
enum Transport {
102+
public enum Transport {
103103

104104
/**
105-
* HTTP exporter.
105+
* HTTP transport.
106106
*/
107107
HTTP,
108108

109109
/**
110-
* gRPC exporter.
110+
* gRPC transport.
111111
*/
112112
GRPC
113113

114114
}
115115

116-
enum Compression {
116+
public enum Compression {
117117

118118
/**
119119
* Gzip compression.

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,12 @@ public String getUrl() {
6868
}
6969

7070
@Configuration(proxyBeanMethods = false)
71+
@ConditionalOnMissingBean({ OtlpGrpcSpanExporter.class, OtlpHttpSpanExporter.class })
72+
@ConditionalOnBean(OtlpTracingConnectionDetails.class)
73+
@ConditionalOnEnabledTracing("otlp")
7174
static class Exporters {
7275

7376
@Bean
74-
@ConditionalOnMissingBean({ OtlpGrpcSpanExporter.class, OtlpHttpSpanExporter.class })
75-
@ConditionalOnBean(OtlpTracingConnectionDetails.class)
76-
@ConditionalOnEnabledTracing("otlp")
7777
@ConditionalOnProperty(prefix = "management.otlp.tracing", name = "transport", havingValue = "http",
7878
matchIfMissing = true)
7979
OtlpHttpSpanExporter otlpHttpSpanExporter(OtlpProperties properties,
@@ -89,9 +89,6 @@ OtlpHttpSpanExporter otlpHttpSpanExporter(OtlpProperties properties,
8989
}
9090

9191
@Bean
92-
@ConditionalOnMissingBean({ OtlpGrpcSpanExporter.class, OtlpHttpSpanExporter.class })
93-
@ConditionalOnBean(OtlpTracingConnectionDetails.class)
94-
@ConditionalOnEnabledTracing("otlp")
9592
@ConditionalOnProperty(prefix = "management.otlp.tracing", name = "transport", havingValue = "grpc")
9693
OtlpGrpcSpanExporter otlpGrpcSpanExporter(OtlpProperties properties,
9794
OtlpTracingConnectionDetails connectionDetails) {

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2102,6 +2102,10 @@
21022102
"type": "java.lang.Boolean",
21032103
"description": "Whether auto-configuration of tracing is enabled to export OTLP traces."
21042104
},
2105+
{
2106+
"name": "management.otlp.tracing.transport",
2107+
"defaultValue": "http"
2108+
},
21052109
{
21062110
"name": "management.prometheus.metrics.export.histogram-flavor",
21072111
"defaultValue": "prometheus"

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

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package org.springframework.boot.actuate.autoconfigure.tracing.otlp;
1818

19-
import java.io.IOException;
2019
import java.io.InputStream;
2120
import java.nio.charset.StandardCharsets;
2221
import java.util.concurrent.BlockingQueue;
@@ -53,7 +52,6 @@
5352
import org.springframework.boot.actuate.autoconfigure.tracing.otlp.OtlpAutoConfigurationIntegrationTests.MockGrpcServer.RecordedGrpcRequest;
5453
import org.springframework.boot.autoconfigure.AutoConfigurations;
5554
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
56-
import org.springframework.util.StreamUtils;
5755

5856
import static org.assertj.core.api.Assertions.assertThat;
5957

@@ -76,23 +74,15 @@ class OtlpAutoConfigurationIntegrationTests {
7674
private final MockGrpcServer mockGrpcServer = new MockGrpcServer();
7775

7876
@BeforeEach
79-
void startMockWebServer() throws IOException {
77+
void startServers() throws Exception {
8078
this.mockWebServer.start();
81-
}
82-
83-
@BeforeEach
84-
void startMockGrpcServer() throws Exception {
8579
this.mockGrpcServer.start();
8680
}
8781

8882
@AfterEach
89-
void stopMockWebServer() throws IOException {
83+
void stopServers() throws Exception {
9084
this.mockWebServer.close();
91-
}
92-
93-
@AfterEach
94-
void stopMockGrpcServer() throws Exception {
95-
this.mockGrpcServer.stop();
85+
this.mockGrpcServer.close();
9686
}
9787

9888
@Test
@@ -142,7 +132,7 @@ void httpSpanExporterCanBeConfiguredToUseGzipCompression() {
142132
}
143133

144134
@Test
145-
void grpcSpanExporter() {
135+
void grpcSpanExporterShouldExportSpans() {
146136
this.contextRunner
147137
.withPropertyValues(
148138
"management.otlp.tracing.endpoint=http://localhost:%d".formatted(this.mockGrpcServer.getPort()),
@@ -155,7 +145,7 @@ void grpcSpanExporter() {
155145
assertThat(request).isNotNull();
156146
assertThat(request.headers().get("Content-Type")).isEqualTo("application/grpc");
157147
assertThat(request.headers().get("custom")).isEqualTo("42");
158-
assertThat(request.body()).contains("org.springframework.boot");
148+
assertThat(request.bodyAsString()).contains("org.springframework.boot");
159149
});
160150
}
161151

@@ -169,7 +159,7 @@ void start() throws Exception {
169159
this.server.start();
170160
}
171161

172-
void stop() throws Exception {
162+
void close() throws Exception {
173163
this.server.stop();
174164
}
175165

@@ -189,15 +179,13 @@ private Server createServer() {
189179
Server server = new Server();
190180
server.addConnector(createConnector(server));
191181
server.setHandler(new GrpcHandler());
192-
193182
return server;
194183
}
195184

196185
private ServerConnector createConnector(Server server) {
197186
ServerConnector connector = new ServerConnector(server,
198187
new HTTP2CServerConnectionFactory(new HttpConfiguration()));
199188
connector.setPort(0);
200-
201189
return connector;
202190
}
203191

@@ -206,21 +194,20 @@ class GrpcHandler extends Handler.Abstract {
206194
@Override
207195
public boolean handle(Request request, Response response, Callback callback) throws Exception {
208196
try (InputStream in = Content.Source.asInputStream(request)) {
209-
recordRequest(new RecordedGrpcRequest(request.getHeaders(),
210-
StreamUtils.copyToString(in, StandardCharsets.UTF_8)));
197+
recordRequest(new RecordedGrpcRequest(request.getHeaders(), in.readAllBytes()));
211198
}
212-
213199
response.getHeaders().add("Content-Type", "application/grpc");
214200
response.getHeaders().add("Grpc-Status", "0");
215-
216201
callback.succeeded();
217-
218202
return true;
219203
}
220204

221205
}
222206

223-
record RecordedGrpcRequest(HttpFields headers, String body) {
207+
record RecordedGrpcRequest(HttpFields headers, byte[] body) {
208+
String bodyAsString() {
209+
return new String(this.body, StandardCharsets.UTF_8);
210+
}
224211
}
225212

226213
}

0 commit comments

Comments
 (0)