Adds the possibility to opt-out of executor decoration (#1464)

* Adds the possibility to opt-out of executor decoration

Recently, `AsyncAutoConfiguration` auto-instruments executors. This
causes spans named 'async' with no tag or other metadata to help
understand what is happening. It was somewhat difficult troubleshooting
this because the control is by bean name, but for example the "async"
span doesn't include the bean name.

See https://github.com/spring-cloud/spring-cloud-gcp/issues/1978
This commit is contained in:
Adrian Cole
2019-10-23 18:48:37 +08:00
committed by Marcin Grzejszczak
parent 5e6bdf9da2
commit b22f5e8db3
5 changed files with 91 additions and 3 deletions

View File

@@ -16,7 +16,9 @@
package org.springframework.cloud.sleuth.instrument.async;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.sleuth.instrument.scheduling.SleuthSchedulingProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@@ -28,7 +30,9 @@ import org.springframework.context.annotation.Configuration;
* @since 2.1.0
*/
@Configuration
@EnableConfigurationProperties(SleuthAsyncProperties.class)
@ConditionalOnProperty(value = "spring.sleuth.scheduled.enabled", matchIfMissing = true)
@EnableConfigurationProperties({ SleuthAsyncProperties.class,
SleuthSchedulingProperties.class })
public class AsyncAutoConfiguration {
@Bean

View File

@@ -32,7 +32,9 @@ import org.springframework.beans.factory.config.BeanDefinition;
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.sleuth.SpanNamer;
import org.springframework.cloud.sleuth.instrument.scheduling.SleuthSchedulingProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Role;
@@ -52,11 +54,15 @@ import org.springframework.scheduling.annotation.AsyncConfigurerSupport;
* @see TraceAsyncAspect
*/
@Configuration
@EnableConfigurationProperties({ SleuthAsyncProperties.class,
SleuthSchedulingProperties.class })
@ConditionalOnProperty(value = "spring.sleuth.async.enabled", matchIfMissing = true)
@ConditionalOnBean(Tracing.class)
public class AsyncDefaultAutoConfiguration {
@Bean
@ConditionalOnProperty(value = "spring.sleuth.scheduled.enabled",
matchIfMissing = true)
public static ExecutorBeanPostProcessor executorBeanPostProcessor(
BeanFactory beanFactory) {
return new ExecutorBeanPostProcessor(beanFactory);

View File

@@ -184,7 +184,7 @@ class LazyTraceThreadPoolTaskScheduler extends ThreadPoolTaskScheduler {
@Override
public void execute(Runnable task) {
this.delegate.execute(task);
this.delegate.execute(new TraceRunnable(tracing(), spanNamer(), task));
}
@Override

View File

@@ -0,0 +1,77 @@
/*
* Copyright 2013-2019 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.cloud.sleuth.instrument.async;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Executor;
import java.util.concurrent.LinkedBlockingQueue;
import brave.propagation.CurrentTraceContext;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.BDDAssertions.then;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = AsyncDisabledTests.ConfigureThreadPoolTaskScheduler.class,
webEnvironment = SpringBootTest.WebEnvironment.NONE,
properties = "spring.sleuth.scheduled.enabled=false")
public class AsyncDisabledTests {
@Autowired
CurrentTraceContext currentTraceContext;
@Autowired
@Qualifier("traceSenderThreadPool")
Executor executor;
/**
* We can't check the type of the executor because sleuth will proxy it. Instead, we
* check for behaviour.
*/
@Test
public void should_not_wrap_scheduler() throws InterruptedException {
BlockingQueue<Boolean> spans = new LinkedBlockingQueue<>();
this.executor.execute(() -> spans.add(this.currentTraceContext.get() != null));
then(spans.take()).isFalse();
}
@Configuration
@EnableAutoConfiguration
@EnableAsync
static class ConfigureThreadPoolTaskScheduler {
@Bean
@ConditionalOnMissingBean(name = "traceSenderThreadPool")
public ThreadPoolTaskScheduler traceSenderThreadPool() {
return new ThreadPoolTaskScheduler();
}
}
}

View File

@@ -128,7 +128,8 @@ public class LazyTraceThreadPoolTaskSchedulerTests {
};
this.executor.execute(r);
BDDMockito.then(this.delegate).should().execute(r);
BDDMockito.then(this.delegate).should()
.execute(BDDMockito.any(TraceRunnable.class));
}
@Test