Don't back off on user-supplied SpanHandlers

Instead the auto-configuration now backs off only on ZipkinSpanHandler.

Closes gh-31273
pull/31327/head
Moritz Halbritter 2 years ago
parent 91bb5aa222
commit d1647cf68c

@ -16,7 +16,6 @@
package org.springframework.boot.actuate.autoconfigure.tracing.zipkin; package org.springframework.boot.actuate.autoconfigure.tracing.zipkin;
import brave.handler.SpanHandler;
import io.opentelemetry.exporter.zipkin.ZipkinSpanExporter; import io.opentelemetry.exporter.zipkin.ZipkinSpanExporter;
import zipkin2.Span; import zipkin2.Span;
import zipkin2.codec.BytesEncoder; import zipkin2.codec.BytesEncoder;
@ -87,8 +86,8 @@ class ZipkinConfigurations {
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
@ConditionalOnBean(Reporter.class) @ConditionalOnBean(Reporter.class)
SpanHandler zipkinSpanHandler(Reporter<Span> spanReporter) { ZipkinSpanHandler zipkinSpanHandler(Reporter<Span> spanReporter) {
return ZipkinSpanHandler.newBuilder(spanReporter).build(); return (ZipkinSpanHandler) ZipkinSpanHandler.newBuilder(spanReporter).build();
} }
} }

@ -21,6 +21,7 @@ import org.junit.jupiter.api.Test;
import org.mockito.Mockito; import org.mockito.Mockito;
import zipkin2.Span; import zipkin2.Span;
import zipkin2.reporter.Reporter; import zipkin2.reporter.Reporter;
import zipkin2.reporter.brave.ZipkinSpanHandler;
import org.springframework.boot.actuate.autoconfigure.tracing.zipkin.ZipkinConfigurations.BraveConfiguration; import org.springframework.boot.actuate.autoconfigure.tracing.zipkin.ZipkinConfigurations.BraveConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.AutoConfigurations;
@ -44,28 +45,38 @@ class ZipkinConfigurationsBraveConfigurationTests {
@Test @Test
void shouldSupplyBeans() { void shouldSupplyBeans() {
this.contextRunner.withUserConfiguration(ReporterConfiguration.class) this.contextRunner.withUserConfiguration(ReporterConfiguration.class)
.run((context) -> assertThat(context).hasSingleBean(SpanHandler.class)); .run((context) -> assertThat(context).hasSingleBean(ZipkinSpanHandler.class));
} }
@Test @Test
void shouldNotSupplySpanHandlerIfReporterIsMissing() { void shouldNotSupplySpanHandlerIfReporterIsMissing() {
this.contextRunner.run((context) -> assertThat(context).doesNotHaveBean(SpanHandler.class)); this.contextRunner.run((context) -> assertThat(context).doesNotHaveBean(ZipkinSpanHandler.class));
} }
@Test @Test
void shouldNotSupplyIfZipkinReporterBraveIsNotOnClasspath() { void shouldNotSupplyIfZipkinReporterBraveIsNotOnClasspath() {
this.contextRunner.withClassLoader(new FilteredClassLoader("zipkin2.reporter.brave")) this.contextRunner.withClassLoader(new FilteredClassLoader("zipkin2.reporter.brave"))
.withUserConfiguration(ReporterConfiguration.class) .withUserConfiguration(ReporterConfiguration.class)
.run((context) -> assertThat(context).doesNotHaveBean(SpanHandler.class)); .run((context) -> assertThat(context).doesNotHaveBean(ZipkinSpanHandler.class));
} }
@Test @Test
void shouldBackOffOnCustomBeans() { void shouldBackOffOnCustomBeans() {
this.contextRunner.withUserConfiguration(CustomConfiguration.class).run((context) -> { this.contextRunner.withUserConfiguration(ReporterConfiguration.class, CustomConfiguration.class)
assertThat(context).hasBean("customSpanHandler"); .run((context) -> {
assertThat(context).hasSingleBean(SpanHandler.class); assertThat(context).hasBean("customZipkinSpanHandler");
}); assertThat(context).hasSingleBean(ZipkinSpanHandler.class);
});
}
@Test
void shouldSupplyZipkinSpanHandlerWithCustomSpanHandler() {
this.contextRunner.withUserConfiguration(ReporterConfiguration.class, CustomSpanHandlerConfiguration.class)
.run((context) -> {
assertThat(context).hasBean("customSpanHandler");
assertThat(context).hasSingleBean(ZipkinSpanHandler.class);
});
} }
@Configuration(proxyBeanMethods = false) @Configuration(proxyBeanMethods = false)
@ -82,6 +93,17 @@ class ZipkinConfigurationsBraveConfigurationTests {
@Configuration(proxyBeanMethods = false) @Configuration(proxyBeanMethods = false)
private static class CustomConfiguration { private static class CustomConfiguration {
@Bean
@SuppressWarnings("unchecked")
ZipkinSpanHandler customZipkinSpanHandler() {
return (ZipkinSpanHandler) ZipkinSpanHandler.create(Mockito.mock(Reporter.class));
}
}
@Configuration(proxyBeanMethods = false)
private static class CustomSpanHandlerConfiguration {
@Bean @Bean
SpanHandler customSpanHandler() { SpanHandler customSpanHandler() {
return Mockito.mock(SpanHandler.class); return Mockito.mock(SpanHandler.class);

Loading…
Cancel
Save