Skip to content

Add OtlpGrpcSpanExporterBuilderCustomizer and OtlpHttpSpanExporterBuilderCustomizer customizers #44900

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
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
@@ -0,0 +1,37 @@
/*
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

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

import io.opentelemetry.exporter.otlp.trace.OtlpGrpcSpanExporterBuilder;

/**
* Callback interface that can be implemented by beans wishing to customize the
* {@link OtlpGrpcSpanExporterBuilder} whilst retaining default auto-configuration.
*
* @author Dmytro Nosan
* @since 3.5.0
*/
@FunctionalInterface
public interface OtlpGrpcSpanExporterBuilderCustomizer {

/**
* Customize the {@link OtlpGrpcSpanExporterBuilder}.
* @param builder the builder to customize
*/
void customize(OtlpGrpcSpanExporterBuilder builder);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

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

import io.opentelemetry.exporter.otlp.http.trace.OtlpHttpSpanExporterBuilder;

/**
* Callback interface that can be implemented by beans wishing to customize the
* {@link OtlpHttpSpanExporterBuilder} whilst retaining default auto-configuration.
*
* @author Dmytro Nosan
* @since 3.5.0
*/
@FunctionalInterface
public interface OtlpHttpSpanExporterBuilderCustomizer {

/**
* Customize the {@link OtlpHttpSpanExporterBuilder}.
* @param builder the builder to customize
*/
void customize(OtlpHttpSpanExporterBuilder builder);

}
Original file line number Diff line number Diff line change
Expand Up @@ -83,28 +83,32 @@ static class Exporters {
@Bean
@ConditionalOnProperty(name = "management.otlp.tracing.transport", havingValue = "http", matchIfMissing = true)
OtlpHttpSpanExporter otlpHttpSpanExporter(OtlpTracingProperties properties,
OtlpTracingConnectionDetails connectionDetails, ObjectProvider<MeterProvider> meterProvider) {
OtlpTracingConnectionDetails connectionDetails, ObjectProvider<MeterProvider> meterProvider,
ObjectProvider<OtlpHttpSpanExporterBuilderCustomizer> customizers) {
OtlpHttpSpanExporterBuilder builder = OtlpHttpSpanExporter.builder()
.setEndpoint(connectionDetails.getUrl(Transport.HTTP))
.setTimeout(properties.getTimeout())
.setConnectTimeout(properties.getConnectTimeout())
.setCompression(properties.getCompression().name().toLowerCase(Locale.ROOT));
properties.getHeaders().forEach(builder::addHeader);
meterProvider.ifAvailable(builder::setMeterProvider);
customizers.orderedStream().forEach((customizer) -> customizer.customize(builder));
return builder.build();
}

@Bean
@ConditionalOnProperty(name = "management.otlp.tracing.transport", havingValue = "grpc")
OtlpGrpcSpanExporter otlpGrpcSpanExporter(OtlpTracingProperties properties,
OtlpTracingConnectionDetails connectionDetails, ObjectProvider<MeterProvider> meterProvider) {
OtlpTracingConnectionDetails connectionDetails, ObjectProvider<MeterProvider> meterProvider,
ObjectProvider<OtlpGrpcSpanExporterBuilderCustomizer> customizers) {
OtlpGrpcSpanExporterBuilder builder = OtlpGrpcSpanExporter.builder()
.setEndpoint(connectionDetails.getUrl(Transport.GRPC))
.setTimeout(properties.getTimeout())
.setConnectTimeout(properties.getConnectTimeout())
.setCompression(properties.getCompression().name().toLowerCase(Locale.ROOT));
properties.getHeaders().forEach(builder::addHeader);
meterProvider.ifAvailable(builder::setMeterProvider);
customizers.orderedStream().forEach((customizer) -> customizer.customize(builder));
return builder.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

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

import java.time.Duration;
import java.util.List;
import java.util.function.Supplier;

Expand Down Expand Up @@ -230,6 +231,45 @@ void grpcShouldUseMeterProviderIfSet() {
});
}

@Test
void shouldCustomizeHttpTransportWithOtlpHttpSpanExporterBuilderCustomizer() {
Duration connectTimeout = Duration.ofMinutes(20);
Duration timeout = Duration.ofMinutes(10);
this.contextRunner
.withBean("httpCustomizer1", OtlpHttpSpanExporterBuilderCustomizer.class,
() -> (builder) -> builder.setConnectTimeout(connectTimeout))
.withBean("httpCustomizer2", OtlpHttpSpanExporterBuilderCustomizer.class,
() -> (builder) -> builder.setTimeout(timeout))
.withPropertyValues("management.otlp.tracing.endpoint=http://localhost:4317/v1/traces")
.run((context) -> {
assertThat(context).hasSingleBean(OtlpHttpSpanExporter.class).hasSingleBean(SpanExporter.class);
OtlpHttpSpanExporter exporter = context.getBean(OtlpHttpSpanExporter.class);
assertThat(exporter).extracting("delegate.httpSender.client")
.hasFieldOrPropertyWithValue("connectTimeoutMillis", (int) connectTimeout.toMillis())
.hasFieldOrPropertyWithValue("callTimeoutMillis", (int) timeout.toMillis());
});
}

@Test
void shouldCustomizeGrpcTransportWhenEnabledWithOtlpGrpcSpanExporterBuilderCustomizer() {
Duration timeout = Duration.ofMinutes(10);
Duration connectTimeout = Duration.ofMinutes(20);
this.contextRunner
.withBean("grpcCustomizer1", OtlpGrpcSpanExporterBuilderCustomizer.class,
() -> (builder) -> builder.setConnectTimeout(connectTimeout))
.withBean("grpcCustomizer2", OtlpGrpcSpanExporterBuilderCustomizer.class,
() -> (builder) -> builder.setTimeout(timeout))
.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);
OtlpGrpcSpanExporter exporter = context.getBean(OtlpGrpcSpanExporter.class);
assertThat(exporter).extracting("delegate.grpcSender.client")
.hasFieldOrPropertyWithValue("connectTimeoutMillis", (int) connectTimeout.toMillis())
.hasFieldOrPropertyWithValue("callTimeoutMillis", (int) timeout.toMillis());
});
}

@Configuration(proxyBeanMethods = false)
private static final class MeterProviderConfiguration {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ Tracing with OpenTelemetry and reporting using OTLP requires the following depen

Use the `management.otlp.tracing.*` configuration properties to configure reporting using OTLP.

NOTE: If you need to apply advanced customizations to OTLP span exporters, consider registering javadoc:org.springframework.boot.actuate.autoconfigure.tracing.otlp.OtlpHttpSpanExporterBuilderCustomizer[] or javadoc:org.springframework.boot.actuate.autoconfigure.tracing.otlp.OtlpGrpcSpanExporterBuilderCustomizer[] beans. These will be invoked **before** the creation of the `OtlpHttpSpanExporter` or `OtlpGrpcSpanExporter`. The customizers take precedence over anything applied by the auto-configuration.



[[actuator.micrometer-tracing.tracer-implementations.brave-zipkin]]
Expand Down