From b22f5e8db3edadbe9f84f175f6569e02d4597b40 Mon Sep 17 00:00:00 2001 From: Adrian Cole Date: Wed, 23 Oct 2019 18:48:37 +0800 Subject: [PATCH] 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 --- .../async/AsyncAutoConfiguration.java | 6 +- .../async/AsyncDefaultAutoConfiguration.java | 6 ++ .../LazyTraceThreadPoolTaskScheduler.java | 2 +- .../instrument/async/AsyncDisabledTests.java | 77 +++++++++++++++++++ ...LazyTraceThreadPoolTaskSchedulerTests.java | 3 +- 5 files changed, 91 insertions(+), 3 deletions(-) create mode 100644 spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/async/AsyncDisabledTests.java diff --git a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/async/AsyncAutoConfiguration.java b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/async/AsyncAutoConfiguration.java index 2d5560545..411900ff0 100644 --- a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/async/AsyncAutoConfiguration.java +++ b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/async/AsyncAutoConfiguration.java @@ -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 diff --git a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/async/AsyncDefaultAutoConfiguration.java b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/async/AsyncDefaultAutoConfiguration.java index 4e388633f..61d89f5a0 100644 --- a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/async/AsyncDefaultAutoConfiguration.java +++ b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/async/AsyncDefaultAutoConfiguration.java @@ -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); diff --git a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/async/LazyTraceThreadPoolTaskScheduler.java b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/async/LazyTraceThreadPoolTaskScheduler.java index f16031d30..bb69c6823 100644 --- a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/async/LazyTraceThreadPoolTaskScheduler.java +++ b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/async/LazyTraceThreadPoolTaskScheduler.java @@ -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 diff --git a/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/async/AsyncDisabledTests.java b/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/async/AsyncDisabledTests.java new file mode 100644 index 000000000..60e354228 --- /dev/null +++ b/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/async/AsyncDisabledTests.java @@ -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 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(); + } + + } + +} diff --git a/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/async/LazyTraceThreadPoolTaskSchedulerTests.java b/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/async/LazyTraceThreadPoolTaskSchedulerTests.java index 9c01b8ba5..c1c57960f 100644 --- a/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/async/LazyTraceThreadPoolTaskSchedulerTests.java +++ b/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/async/LazyTraceThreadPoolTaskSchedulerTests.java @@ -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