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.LocalServiceName;
|
||||||
import org.springframework.cloud.sleuth.SpanAdjuster;
|
import org.springframework.cloud.sleuth.SpanAdjuster;
|
||||||
import org.springframework.cloud.sleuth.SpanNamer;
|
import org.springframework.cloud.sleuth.SpanNamer;
|
||||||
|
import org.springframework.cloud.sleuth.sampler.SamplerAutoConfiguration;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.Import;
|
||||||
import org.springframework.lang.Nullable;
|
import org.springframework.lang.Nullable;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
@@ -71,6 +73,7 @@ import org.springframework.util.StringUtils;
|
|||||||
@Configuration(proxyBeanMethods = false)
|
@Configuration(proxyBeanMethods = false)
|
||||||
@ConditionalOnProperty(value = "spring.sleuth.enabled", matchIfMissing = true)
|
@ConditionalOnProperty(value = "spring.sleuth.enabled", matchIfMissing = true)
|
||||||
@EnableConfigurationProperties(SleuthProperties.class)
|
@EnableConfigurationProperties(SleuthProperties.class)
|
||||||
|
@Import(SamplerAutoConfiguration.class)
|
||||||
public class TraceAutoConfiguration {
|
public class TraceAutoConfiguration {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -135,12 +138,6 @@ public class TraceAutoConfiguration {
|
|||||||
return tracing.tracer();
|
return tracing.tracer();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
|
||||||
@ConditionalOnMissingBean
|
|
||||||
Sampler sleuthTraceSampler() {
|
|
||||||
return Sampler.NEVER_SAMPLE;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@ConditionalOnMissingBean
|
@ConditionalOnMissingBean
|
||||||
SpanNamer sleuthSpanNamer() {
|
SpanNamer sleuthSpanNamer() {
|
||||||
|
|||||||
@@ -20,24 +20,30 @@ import brave.sampler.Sampler;
|
|||||||
|
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
|
||||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||||
import org.springframework.cloud.context.config.annotation.RefreshScope;
|
import org.springframework.cloud.context.config.annotation.RefreshScope;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@link org.springframework.boot.autoconfigure.EnableAutoConfiguration
|
* {@linkplain Configuration configuration} for {@link Sampler}.
|
||||||
* Auto-configuration} to setup sampling for Spring Cloud Sleuth.
|
|
||||||
*
|
*
|
||||||
* @author Marcin Grzejszczak
|
* @author Marcin Grzejszczak
|
||||||
* @since 2.1.0
|
* @since 2.1.0
|
||||||
*/
|
*/
|
||||||
@Configuration(proxyBeanMethods = false)
|
@Configuration(proxyBeanMethods = false)
|
||||||
@ConditionalOnProperty(value = "spring.sleuth.enabled", matchIfMissing = true)
|
|
||||||
@EnableConfigurationProperties(SamplerProperties.class)
|
@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 {
|
public class SamplerAutoConfiguration {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
@ConditionalOnMissingBean
|
||||||
|
Sampler sleuthTraceSampler() {
|
||||||
|
return Sampler.NEVER_SAMPLE;
|
||||||
|
}
|
||||||
|
|
||||||
static Sampler samplerFromProps(SamplerProperties config) {
|
static Sampler samplerFromProps(SamplerProperties config) {
|
||||||
if (config.getProbability() != null) {
|
if (config.getProbability() != null) {
|
||||||
return new ProbabilityBasedSampler(config);
|
return new ProbabilityBasedSampler(config);
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
# Auto Configuration
|
# Auto Configuration
|
||||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||||
org.springframework.cloud.sleuth.annotation.SleuthAnnotationAutoConfiguration,\
|
org.springframework.cloud.sleuth.annotation.SleuthAnnotationAutoConfiguration,\
|
||||||
org.springframework.cloud.sleuth.sampler.SamplerAutoConfiguration,\
|
|
||||||
org.springframework.cloud.sleuth.autoconfig.TraceAutoConfiguration,\
|
org.springframework.cloud.sleuth.autoconfig.TraceAutoConfiguration,\
|
||||||
org.springframework.cloud.sleuth.log.SleuthLogAutoConfiguration,\
|
org.springframework.cloud.sleuth.log.SleuthLogAutoConfiguration,\
|
||||||
org.springframework.cloud.sleuth.propagation.SleuthTagPropagationAutoConfiguration,\
|
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.client.serviceregistry.Registration;
|
||||||
import org.springframework.cloud.commons.util.InetUtils;
|
import org.springframework.cloud.commons.util.InetUtils;
|
||||||
import org.springframework.cloud.sleuth.autoconfig.TraceAutoConfiguration;
|
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.cloud.sleuth.zipkin2.sender.ZipkinSenderConfigurationImportSelector;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
@@ -53,8 +52,7 @@ import org.springframework.web.client.RestTemplate;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* {@link org.springframework.boot.autoconfigure.EnableAutoConfiguration
|
* {@link org.springframework.boot.autoconfigure.EnableAutoConfiguration
|
||||||
* Auto-configuration} enables reporting to Zipkin via HTTP. Has a default sampler set
|
* Auto-configuration} enables reporting to Zipkin via HTTP.
|
||||||
* from the {@link SamplerAutoConfiguration}
|
|
||||||
*
|
*
|
||||||
* The {@link ZipkinRestTemplateCustomizer} allows you to customize the
|
* The {@link ZipkinRestTemplateCustomizer} allows you to customize the
|
||||||
* {@link RestTemplate} that is used to send Spans to Zipkin. Its default implementation -
|
* {@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 Spencer Gibb
|
||||||
* @author Tim Ysewyn
|
* @author Tim Ysewyn
|
||||||
* @since 1.0.0
|
* @since 1.0.0
|
||||||
* @see SamplerAutoConfiguration
|
|
||||||
* @see ZipkinRestTemplateCustomizer
|
* @see ZipkinRestTemplateCustomizer
|
||||||
* @see DefaultZipkinRestTemplateCustomizer
|
* @see DefaultZipkinRestTemplateCustomizer
|
||||||
*/
|
*/
|
||||||
@@ -74,7 +71,7 @@ import org.springframework.web.client.RestTemplate;
|
|||||||
@AutoConfigureBefore(TraceAutoConfiguration.class)
|
@AutoConfigureBefore(TraceAutoConfiguration.class)
|
||||||
@AutoConfigureAfter(
|
@AutoConfigureAfter(
|
||||||
name = "org.springframework.cloud.autoconfigure.RefreshAutoConfiguration")
|
name = "org.springframework.cloud.autoconfigure.RefreshAutoConfiguration")
|
||||||
@Import({ ZipkinSenderConfigurationImportSelector.class, SamplerAutoConfiguration.class })
|
@Import(ZipkinSenderConfigurationImportSelector.class)
|
||||||
public class ZipkinAutoConfiguration {
|
public class ZipkinAutoConfiguration {
|
||||||
|
|
||||||
private static final Log log = LogFactory.getLog(ZipkinAutoConfiguration.class);
|
private static final Log log = LogFactory.getLog(ZipkinAutoConfiguration.class);
|
||||||
|
|||||||
@@ -130,11 +130,11 @@ class Application {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public AnnotatedFeignClient annotatedFeignClient(Client client, Decoder decoder, Encoder encoder,
|
public AnnotatedFeignClient annotatedFeignClient(Client client, Decoder decoder,
|
||||||
Contract contract) {
|
Encoder encoder, Contract contract) {
|
||||||
return Feign.builder().client(client).encoder(encoder).decoder(decoder)
|
return Feign.builder().client(client).encoder(encoder).decoder(decoder)
|
||||||
.contract(contract)
|
.contract(contract).target(new HardCodedTarget<>(
|
||||||
.target(new HardCodedTarget<>(AnnotatedFeignClient.class, "foo", "http://foo"));
|
AnnotatedFeignClient.class, "foo", "http://foo"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@@ -151,7 +151,8 @@ class Application {
|
|||||||
|
|
||||||
class MyLoadBalancerClient extends LoadBalancerFeignClient {
|
class MyLoadBalancerClient extends LoadBalancerFeignClient {
|
||||||
|
|
||||||
MyLoadBalancerClient(Client delegate, CachingSpringLoadBalancerFactory lbClientFactory,
|
MyLoadBalancerClient(Client delegate,
|
||||||
|
CachingSpringLoadBalancerFactory lbClientFactory,
|
||||||
SpringClientFactory clientFactory) {
|
SpringClientFactory clientFactory) {
|
||||||
super(delegate, lbClientFactory, clientFactory);
|
super(delegate, lbClientFactory, clientFactory);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user