Skip to content

Add auto-configuration for OTLP gRPC format when using tracing #41213

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@
* the future, see: <a href=
* "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,
* define an {@link OtlpGrpcSpanExporter} and this auto-configuration will back off.
* By default, we auto-configure HTTP/protobuf. If you want to use gRPC, you need to set
* {@code management.otlp.tracing.exporter=grpc}. If you define a
* {@link OtlpHttpSpanExporter} or {@link OtlpGrpcSpanExporter}, this auto-configuration
* will back off.
*
* @author Jonatan Ivanov
* @author Moritz Halbritter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ public class OtlpProperties {
*/
private Duration timeout = Duration.ofSeconds(10);

/**
* Transport used to send the spans. Defaults to HTTP.
*/
private Transport transport = Transport.HTTP;

/**
* Method used to compress the payload.
*/
Expand All @@ -70,6 +75,14 @@ public void setTimeout(Duration timeout) {
this.timeout = timeout;
}

public Transport getTransport() {
return this.transport;
}

public void setTransport(Transport transport) {
this.transport = transport;
}

public Compression getCompression() {
return this.compression;
}
Expand All @@ -86,6 +99,20 @@ public void setHeaders(Map<String, String> headers) {
this.headers = headers;
}

enum Transport {

/**
* HTTP exporter.
*/
HTTP,

/**
* gRPC exporter.
*/
GRPC

}

enum Compression {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

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.exporter.otlp.trace.OtlpGrpcSpanExporterBuilder;

import org.springframework.boot.actuate.autoconfigure.tracing.ConditionalOnEnabledTracing;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
Expand Down Expand Up @@ -69,10 +71,11 @@ public String getUrl() {
static class Exporters {

@Bean
@ConditionalOnMissingBean(value = OtlpHttpSpanExporter.class,
type = "io.opentelemetry.exporter.otlp.trace.OtlpGrpcSpanExporter")
@ConditionalOnMissingBean({ OtlpGrpcSpanExporter.class, OtlpHttpSpanExporter.class })
@ConditionalOnBean(OtlpTracingConnectionDetails.class)
@ConditionalOnEnabledTracing("otlp")
@ConditionalOnProperty(prefix = "management.otlp.tracing", name = "transport", havingValue = "http",
matchIfMissing = true)
OtlpHttpSpanExporter otlpHttpSpanExporter(OtlpProperties properties,
OtlpTracingConnectionDetails connectionDetails) {
OtlpHttpSpanExporterBuilder builder = OtlpHttpSpanExporter.builder()
Expand All @@ -85,6 +88,23 @@ OtlpHttpSpanExporter otlpHttpSpanExporter(OtlpProperties properties,
return builder.build();
}

@Bean
@ConditionalOnMissingBean({ OtlpGrpcSpanExporter.class, OtlpHttpSpanExporter.class })
@ConditionalOnBean(OtlpTracingConnectionDetails.class)
@ConditionalOnEnabledTracing("otlp")
@ConditionalOnProperty(prefix = "management.otlp.tracing", name = "transport", havingValue = "grpc")
OtlpGrpcSpanExporter otlpGrpcSpanExporter(OtlpProperties properties,
OtlpTracingConnectionDetails connectionDetails) {
OtlpGrpcSpanExporterBuilder builder = OtlpGrpcSpanExporter.builder()
.setEndpoint(connectionDetails.getUrl())
.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 @@ -51,13 +51,28 @@ void shouldNotSupplyBeansIfPropertyIsNotSet() {
this.contextRunner.run((context) -> assertThat(context).doesNotHaveBean(OtlpHttpSpanExporter.class));
}

@Test
void shouldNotSupplyBeansIfGrpcTransportIsEnabledButPropertyIsNotSet() {
this.contextRunner.withPropertyValues("management.otlp.tracing.transport=grpc")
.run((context) -> assertThat(context).doesNotHaveBean(OtlpGrpcSpanExporter.class));
}

@Test
void shouldSupplyBeans() {
this.contextRunner.withPropertyValues("management.otlp.tracing.endpoint=http://localhost:4318/v1/traces")
.run((context) -> assertThat(context).hasSingleBean(OtlpHttpSpanExporter.class)
.hasSingleBean(SpanExporter.class));
}

@Test
void shouldSupplyBeansIfGrpcTransportIsEnabled() {
this.contextRunner
.withPropertyValues("management.otlp.tracing.endpoint=http://localhost:4317/v1/traces",
"management.otlp.tracing.transport=grpc")
.run((context) -> assertThat(context).hasSingleBean(OtlpGrpcSpanExporter.class)
.hasSingleBean(SpanExporter.class));
}

@Test
void shouldNotSupplyBeansIfGlobalTracingIsDisabled() {
this.contextRunner.withPropertyValues("management.tracing.enabled=false")
Expand Down