Merge pull request #34945 from quaff
* gh-34945: Polish "Add customizer for SdkTracerProviderBuilder" Add customizer for SdkTracerProviderBuilder Closes gh-34945
This commit is contained in:
@@ -70,6 +70,7 @@ import org.springframework.core.env.Environment;
|
||||
* {@link EnableAutoConfiguration Auto-configuration} for OpenTelemetry.
|
||||
*
|
||||
* @author Moritz Halbritter
|
||||
* @author Yanming Zhou
|
||||
* @since 3.0.0
|
||||
*/
|
||||
@AutoConfiguration(before = MicrometerTracingAutoConfiguration.class)
|
||||
@@ -101,12 +102,13 @@ public class OpenTelemetryAutoConfiguration {
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
SdkTracerProvider otelSdkTracerProvider(Environment environment, ObjectProvider<SpanProcessor> spanProcessors,
|
||||
Sampler sampler) {
|
||||
Sampler sampler, ObjectProvider<SdkTracerProviderBuilderCustomizer> customizers) {
|
||||
String applicationName = environment.getProperty("spring.application.name", DEFAULT_APPLICATION_NAME);
|
||||
SdkTracerProviderBuilder builder = SdkTracerProvider.builder()
|
||||
.setSampler(sampler)
|
||||
.setResource(Resource.create(Attributes.of(ResourceAttributes.SERVICE_NAME, applicationName)));
|
||||
spanProcessors.orderedStream().forEach(builder::addSpanProcessor);
|
||||
customizers.orderedStream().forEach((customizer) -> customizer.customize(builder));
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* Copyright 2012-2023 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;
|
||||
|
||||
import io.opentelemetry.sdk.trace.SdkTracerProvider;
|
||||
import io.opentelemetry.sdk.trace.SdkTracerProviderBuilder;
|
||||
|
||||
/**
|
||||
* Callback interface that can be used to customize the {@link SdkTracerProviderBuilder}
|
||||
* that is used to create the auto-configured {@link SdkTracerProvider}.
|
||||
*
|
||||
* @author Yanming Zhou
|
||||
* @since 3.1.0
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface SdkTracerProviderBuilderCustomizer {
|
||||
|
||||
/**
|
||||
* Customize the given {@code builder}.
|
||||
* @param builder the builder to customize
|
||||
*/
|
||||
void customize(SdkTracerProviderBuilder builder);
|
||||
|
||||
}
|
||||
@@ -34,6 +34,7 @@ import io.opentelemetry.context.propagation.ContextPropagators;
|
||||
import io.opentelemetry.context.propagation.TextMapPropagator;
|
||||
import io.opentelemetry.extension.trace.propagation.B3Propagator;
|
||||
import io.opentelemetry.sdk.trace.SdkTracerProvider;
|
||||
import io.opentelemetry.sdk.trace.SpanLimits;
|
||||
import io.opentelemetry.sdk.trace.SpanProcessor;
|
||||
import io.opentelemetry.sdk.trace.samplers.Sampler;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -45,6 +46,7 @@ import org.springframework.boot.test.context.FilteredClassLoader;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.annotation.Order;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
@@ -54,6 +56,7 @@ import static org.mockito.Mockito.mock;
|
||||
*
|
||||
* @author Moritz Halbritter
|
||||
* @author Andy Wilkinson
|
||||
* @author Yanming Zhou
|
||||
*/
|
||||
class OpenTelemetryAutoConfigurationTests {
|
||||
|
||||
@@ -203,6 +206,15 @@ class OpenTelemetryAutoConfigurationTests {
|
||||
.run((context) -> assertThat(context).hasBean("w3cTextMapPropagatorWithoutBaggage"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldCustomizeSdkTracerProvider() {
|
||||
this.contextRunner.withUserConfiguration(SdkTracerProviderCustomizationConfiguration.class).run((context) -> {
|
||||
SdkTracerProvider tracerProvider = context.getBean(SdkTracerProvider.class);
|
||||
assertThat(tracerProvider.getSpanLimits().getMaxNumberOfEvents()).isEqualTo(42);
|
||||
assertThat(tracerProvider.getSampler()).isEqualTo(Sampler.alwaysOn());
|
||||
});
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
private static class CustomConfiguration {
|
||||
|
||||
@@ -278,4 +290,27 @@ class OpenTelemetryAutoConfigurationTests {
|
||||
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
private static class SdkTracerProviderCustomizationConfiguration {
|
||||
|
||||
@Bean
|
||||
@Order(1)
|
||||
SdkTracerProviderBuilderCustomizer sdkTracerProviderBuilderCustomizerOne() {
|
||||
return (builder) -> {
|
||||
SpanLimits spanLimits = SpanLimits.builder().setMaxNumberOfEvents(42).build();
|
||||
builder.setSpanLimits(spanLimits);
|
||||
};
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Order(0)
|
||||
SdkTracerProviderBuilderCustomizer sdkTracerProviderBuilderCustomizerTwo() {
|
||||
return (builder) -> {
|
||||
SpanLimits spanLimits = SpanLimits.builder().setMaxNumberOfEvents(21).build();
|
||||
builder.setSpanLimits(spanLimits).setSampler(Sampler.alwaysOn());
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user