Remove cycles from auto configuration (#1196)

This commit is contained in:
Bartosz Polnik
2019-01-28 11:05:04 +01:00
committed by Marcin Grzejszczak
parent 9eae736e12
commit fc257fb2be
3 changed files with 32 additions and 29 deletions

View File

@@ -17,6 +17,7 @@
package org.springframework.cloud.sleuth.autoconfig;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import brave.CurrentSpanCustomizer;
@@ -37,6 +38,7 @@ import zipkin2.reporter.InMemoryReporterMetrics;
import zipkin2.reporter.Reporter;
import zipkin2.reporter.ReporterMetrics;
import org.springframework.lang.Nullable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
@@ -73,9 +75,6 @@ public class TraceAutoConfiguration {
*/
public static final String DEFAULT_SERVICE_NAME = "default";
@Autowired(required = false)
List<Reporter<zipkin2.Span>> spanReporters = new ArrayList<>();
@Autowired(required = false)
List<SpanAdjuster> spanAdjusters = new ArrayList<>();
@@ -94,14 +93,15 @@ public class TraceAutoConfiguration {
Tracing tracing(
@Value("${spring.zipkin.service.name:${spring.application.name:default}}") String serviceName,
Propagation.Factory factory, CurrentTraceContext currentTraceContext,
Sampler sampler, ErrorParser errorParser, SleuthProperties sleuthProperties) {
Sampler sampler, ErrorParser errorParser, SleuthProperties sleuthProperties,
@Nullable List<Reporter<zipkin2.Span>> spanReporters) {
Tracing.Builder builder = Tracing.newBuilder().sampler(sampler)
.errorParser(errorParser)
.localServiceName(StringUtils.isEmpty(serviceName) ? DEFAULT_SERVICE_NAME
: serviceName)
.propagationFactory(factory).currentTraceContext(currentTraceContext)
.spanReporter(
new CompositeReporter(this.spanAdjusters, this.spanReporters))
.spanReporter(new CompositeReporter(this.spanAdjusters,
spanReporters != null ? spanReporters : Collections.emptyList()))
.traceId128Bit(sleuthProperties.isTraceId128())
.supportsJoin(sleuthProperties.isSupportsJoin());
for (FinishedSpanHandler finishedSpanHandlerFactory : this.finishedSpanHandlers) {

View File

@@ -23,7 +23,6 @@ import brave.http.HttpClientParser;
import brave.http.HttpSampler;
import brave.http.HttpServerParser;
import brave.http.HttpTracing;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
@@ -32,6 +31,7 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.lang.Nullable;
/**
* {@link org.springframework.boot.autoconfigure.EnableAutoConfiguration
@@ -49,34 +49,22 @@ public class TraceHttpAutoConfiguration {
static final int TRACING_FILTER_ORDER = Ordered.HIGHEST_PRECEDENCE + 5;
@Autowired
HttpClientParser clientParser;
@Autowired
HttpServerParser serverParser;
@Autowired
@ClientSampler
HttpSampler clientSampler;
@Autowired(required = false)
@ServerSampler
HttpSampler serverSampler;
@Bean
@ConditionalOnMissingBean
// NOTE: stable bean name as might be used outside sleuth
HttpTracing httpTracing(Tracing tracing, SkipPatternProvider provider) {
HttpSampler serverSampler = combineUserProvidedSamplerWithSkipPatternSampler(
provider);
return HttpTracing.newBuilder(tracing).clientParser(this.clientParser)
.serverParser(this.serverParser).clientSampler(this.clientSampler)
.serverSampler(serverSampler).build();
HttpTracing httpTracing(Tracing tracing, SkipPatternProvider provider,
HttpClientParser clientParser, HttpServerParser serverParser,
@ClientSampler HttpSampler clientSampler,
@Nullable @ServerSampler HttpSampler serverSampler) {
HttpSampler combinedSampler = combineUserProvidedSamplerWithSkipPatternSampler(
serverSampler, provider);
return HttpTracing.newBuilder(tracing).clientParser(clientParser)
.serverParser(serverParser).clientSampler(clientSampler)
.serverSampler(combinedSampler).build();
}
private HttpSampler combineUserProvidedSamplerWithSkipPatternSampler(
SkipPatternProvider provider) {
HttpSampler serverSampler = this.serverSampler;
HttpSampler serverSampler, SkipPatternProvider provider) {
SleuthHttpSampler skipPatternSampler = new SleuthHttpSampler(provider);
if (serverSampler == null) {
return skipPatternSampler;

View File

@@ -8,8 +8,11 @@ import org.assertj.core.api.BDDAssertions;
import org.junit.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.cloud.sleuth.instrument.web.TraceHttpAutoConfiguration;
import org.springframework.cloud.sleuth.instrument.web.TraceWebAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.GenericApplicationContext;
public class TraceAutoConfigurationPropagationCustomizationTests {
@@ -43,6 +46,18 @@ public class TraceAutoConfigurationPropagationCustomizationTests {
});
}
@Test
public void hasNoCycles() {
this.contextRunner
.withConfiguration(AutoConfigurations.of(TraceWebAutoConfiguration.class,
TraceHttpAutoConfiguration.class))
.withInitializer(c -> ((GenericApplicationContext) c)
.setAllowCircularReferences(false))
.run((context) -> {
BDDAssertions.then(context.isRunning()).isEqualTo(true);
});
}
@Test
public void allowsCustomizationOfBuilder() {
this.contextRunner.withPropertyValues("spring.sleuth.baggage-keys=my-baggage")