Moves responsibility to import SamplerAutoConfiguration to core
Before, spring-cloud-sleuth-zipkin had to import `SamplerAutoConfiguration` directly to unwind a sampler ordering problem caused by `TraceAutoConfiguration` defining the default `Sampler` bean. This fixes that by moving the default `Sampler` to where it belongs (`SamplerAutoConfiguration`) and having `TraceAutoConfiguration` import the sampling configuration directly as opposed to relying on auto-configuration ordering. Finally it removes the mistake of setting `SamplerAutoConfiguration` as auto-configuration in the first place. The name `SamplerAutoConfiguration` was left alone because changing it would interfere with 3rd party code that formerly imported it to correct this issue in their non-zipkin setups. Fixes #1618
This commit is contained in:
@@ -54,8 +54,10 @@ import org.springframework.cloud.sleuth.DefaultSpanNamer;
|
||||
import org.springframework.cloud.sleuth.LocalServiceName;
|
||||
import org.springframework.cloud.sleuth.SpanAdjuster;
|
||||
import org.springframework.cloud.sleuth.SpanNamer;
|
||||
import org.springframework.cloud.sleuth.sampler.SamplerAutoConfiguration;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
@@ -71,6 +73,7 @@ import org.springframework.util.StringUtils;
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@ConditionalOnProperty(value = "spring.sleuth.enabled", matchIfMissing = true)
|
||||
@EnableConfigurationProperties(SleuthProperties.class)
|
||||
@Import(SamplerAutoConfiguration.class)
|
||||
public class TraceAutoConfiguration {
|
||||
|
||||
/**
|
||||
@@ -135,12 +138,6 @@ public class TraceAutoConfiguration {
|
||||
return tracing.tracer();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
Sampler sleuthTraceSampler() {
|
||||
return Sampler.NEVER_SAMPLE;
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
SpanNamer sleuthSpanNamer() {
|
||||
|
||||
@@ -20,24 +20,30 @@ import brave.sampler.Sampler;
|
||||
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.cloud.context.config.annotation.RefreshScope;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* {@link org.springframework.boot.autoconfigure.EnableAutoConfiguration
|
||||
* Auto-configuration} to setup sampling for Spring Cloud Sleuth.
|
||||
* {@linkplain Configuration configuration} for {@link Sampler}.
|
||||
*
|
||||
* @author Marcin Grzejszczak
|
||||
* @since 2.1.0
|
||||
*/
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@ConditionalOnProperty(value = "spring.sleuth.enabled", matchIfMissing = true)
|
||||
@EnableConfigurationProperties(SamplerProperties.class)
|
||||
// This is not auto-configuration, but it was in the past. Leaving the name as
|
||||
// SamplerAutoConfiguration because those not using Zipkin formerly had to
|
||||
// import this directly. A less precise name is better than rev-locking code.
|
||||
public class SamplerAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
Sampler sleuthTraceSampler() {
|
||||
return Sampler.NEVER_SAMPLE;
|
||||
}
|
||||
|
||||
static Sampler samplerFromProps(SamplerProperties config) {
|
||||
if (config.getProbability() != null) {
|
||||
return new ProbabilityBasedSampler(config);
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
# Auto Configuration
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
org.springframework.cloud.sleuth.annotation.SleuthAnnotationAutoConfiguration,\
|
||||
org.springframework.cloud.sleuth.sampler.SamplerAutoConfiguration,\
|
||||
org.springframework.cloud.sleuth.autoconfig.TraceAutoConfiguration,\
|
||||
org.springframework.cloud.sleuth.log.SleuthLogAutoConfiguration,\
|
||||
org.springframework.cloud.sleuth.propagation.SleuthTagPropagationAutoConfiguration,\
|
||||
|
||||
@@ -43,7 +43,6 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties
|
||||
import org.springframework.cloud.client.serviceregistry.Registration;
|
||||
import org.springframework.cloud.commons.util.InetUtils;
|
||||
import org.springframework.cloud.sleuth.autoconfig.TraceAutoConfiguration;
|
||||
import org.springframework.cloud.sleuth.sampler.SamplerAutoConfiguration;
|
||||
import org.springframework.cloud.sleuth.zipkin2.sender.ZipkinSenderConfigurationImportSelector;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
@@ -53,8 +52,7 @@ import org.springframework.web.client.RestTemplate;
|
||||
|
||||
/**
|
||||
* {@link org.springframework.boot.autoconfigure.EnableAutoConfiguration
|
||||
* Auto-configuration} enables reporting to Zipkin via HTTP. Has a default sampler set
|
||||
* from the {@link SamplerAutoConfiguration}
|
||||
* Auto-configuration} enables reporting to Zipkin via HTTP.
|
||||
*
|
||||
* The {@link ZipkinRestTemplateCustomizer} allows you to customize the
|
||||
* {@link RestTemplate} that is used to send Spans to Zipkin. Its default implementation -
|
||||
@@ -63,7 +61,6 @@ import org.springframework.web.client.RestTemplate;
|
||||
* @author Spencer Gibb
|
||||
* @author Tim Ysewyn
|
||||
* @since 1.0.0
|
||||
* @see SamplerAutoConfiguration
|
||||
* @see ZipkinRestTemplateCustomizer
|
||||
* @see DefaultZipkinRestTemplateCustomizer
|
||||
*/
|
||||
@@ -74,7 +71,7 @@ import org.springframework.web.client.RestTemplate;
|
||||
@AutoConfigureBefore(TraceAutoConfiguration.class)
|
||||
@AutoConfigureAfter(
|
||||
name = "org.springframework.cloud.autoconfigure.RefreshAutoConfiguration")
|
||||
@Import({ ZipkinSenderConfigurationImportSelector.class, SamplerAutoConfiguration.class })
|
||||
@Import(ZipkinSenderConfigurationImportSelector.class)
|
||||
public class ZipkinAutoConfiguration {
|
||||
|
||||
private static final Log log = LogFactory.getLog(ZipkinAutoConfiguration.class);
|
||||
|
||||
@@ -130,11 +130,11 @@ class Application {
|
||||
}
|
||||
|
||||
@Bean
|
||||
public AnnotatedFeignClient annotatedFeignClient(Client client, Decoder decoder, Encoder encoder,
|
||||
Contract contract) {
|
||||
public AnnotatedFeignClient annotatedFeignClient(Client client, Decoder decoder,
|
||||
Encoder encoder, Contract contract) {
|
||||
return Feign.builder().client(client).encoder(encoder).decoder(decoder)
|
||||
.contract(contract)
|
||||
.target(new HardCodedTarget<>(AnnotatedFeignClient.class, "foo", "http://foo"));
|
||||
.contract(contract).target(new HardCodedTarget<>(
|
||||
AnnotatedFeignClient.class, "foo", "http://foo"));
|
||||
}
|
||||
|
||||
@Bean
|
||||
@@ -151,7 +151,8 @@ class Application {
|
||||
|
||||
class MyLoadBalancerClient extends LoadBalancerFeignClient {
|
||||
|
||||
MyLoadBalancerClient(Client delegate, CachingSpringLoadBalancerFactory lbClientFactory,
|
||||
MyLoadBalancerClient(Client delegate,
|
||||
CachingSpringLoadBalancerFactory lbClientFactory,
|
||||
SpringClientFactory clientFactory) {
|
||||
super(delegate, lbClientFactory, clientFactory);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user