ability to disable instrumentation in ThreadPoolTaskExecutor (#1035)

* ability to disable instrumentation in ThreadPoolTaskExecutor
* Fixing documentation and adding unit tests
This commit is contained in:
jalogar
2018-07-25 10:56:47 +02:00
committed by Marcin Grzejszczak
parent 3eef9e1a54
commit ed1f3afc8e
5 changed files with 138 additions and 4 deletions

View File

@@ -0,0 +1,34 @@
/*
* Copyright 2013-2018 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
*
* http://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 org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
/**
* {@link org.springframework.boot.autoconfigure.EnableAutoConfiguration Auto-configuration}
* that wraps an existing custom {@link AsyncConfigurer} in a {@link LazyTraceAsyncCustomizer}
*
* @author Jesus Alonso
* @since 2.1.0
*/
@Configuration
@EnableConfigurationProperties(AsyncProperties.class)
public class AsyncAutoConfiguration {
}

View File

@@ -0,0 +1,42 @@
/*
* Copyright 2013-2018 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
*
* http://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.Collections;
import java.util.List;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* Settings for disable instrumentation of ThreadPoolTaskExecutors
*
* @author Jesus Alonso
* @since 2.1.0
*/
@ConfigurationProperties(prefix = "spring.sleuth.async")
public class AsyncProperties {
private List<String> ignoredBeans = Collections.emptyList();
public List<String> getIgnoredBeans() {
return this.ignoredBeans;
}
public void setIgnoredBeans(List<String> ignoredBeans) {
this.ignoredBeans = ignoredBeans;
}
}

View File

@@ -38,6 +38,7 @@ import org.springframework.util.ReflectionUtils;
* method or is final.
*
* @author Marcin Grzejszczak
* @author Jesus Alonso
* @since 1.1.4
*/
class ExecutorBeanPostProcessor implements BeanPostProcessor {
@@ -46,6 +47,7 @@ class ExecutorBeanPostProcessor implements BeanPostProcessor {
ExecutorBeanPostProcessor.class);
private final BeanFactory beanFactory;
private AsyncProperties asyncProperties;
ExecutorBeanPostProcessor(BeanFactory beanFactory) {
this.beanFactory = beanFactory;
@@ -78,14 +80,23 @@ class ExecutorBeanPostProcessor implements BeanPostProcessor {
throw e;
}
} else if (bean instanceof ThreadPoolTaskExecutor) {
boolean classFinal = Modifier.isFinal(bean.getClass().getModifiers());
boolean cglibProxy = !classFinal;
ThreadPoolTaskExecutor executor = (ThreadPoolTaskExecutor) bean;
return createThreadPoolTaskExecutorProxy(bean, cglibProxy, executor);
if (isProxyNeeded(beanName)) {
boolean classFinal = Modifier.isFinal(bean.getClass().getModifiers());
boolean cglibProxy = !classFinal;
ThreadPoolTaskExecutor executor = (ThreadPoolTaskExecutor) bean;
return createThreadPoolTaskExecutorProxy(bean, cglibProxy, executor);
} else {
log.info("Not instrumenting bean " + beanName);
}
}
return bean;
}
boolean isProxyNeeded(String beanName) {
AsyncProperties asyncProperties = asyncConfigurationProperties();
return !asyncProperties.getIgnoredBeans().contains(beanName);
}
Object createThreadPoolTaskExecutorProxy(Object bean, boolean cglibProxy,
ThreadPoolTaskExecutor executor) {
ProxyFactoryBean factory = new ProxyFactoryBean();
@@ -107,6 +118,13 @@ class ExecutorBeanPostProcessor implements BeanPostProcessor {
factory.setTarget(bean);
return factory.getObject();
}
private AsyncProperties asyncConfigurationProperties() {
if (this.asyncProperties == null) {
this.asyncProperties = this.beanFactory.getBean(AsyncProperties.class);
}
return this.asyncProperties;
}
}
class ExecutorMethodInterceptor<T extends Executor> implements MethodInterceptor {

View File

@@ -8,6 +8,7 @@ org.springframework.cloud.sleuth.instrument.web.TraceWebAutoConfiguration,\
org.springframework.cloud.sleuth.instrument.web.TraceWebServletAutoConfiguration,\
org.springframework.cloud.sleuth.instrument.web.client.TraceWebClientAutoConfiguration,\
org.springframework.cloud.sleuth.instrument.web.client.TraceWebAsyncClientAutoConfiguration,\
org.springframework.cloud.sleuth.instrument.async.AsyncAutoConfiguration,\
org.springframework.cloud.sleuth.instrument.async.AsyncCustomAutoConfiguration,\
org.springframework.cloud.sleuth.instrument.async.AsyncDefaultAutoConfiguration,\
org.springframework.cloud.sleuth.instrument.scheduling.TraceSchedulingAutoConfiguration,\

View File

@@ -20,15 +20,20 @@ import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.aop.framework.AopConfigException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.util.ClassUtils;
import com.google.common.collect.ImmutableList;
import static org.junit.Assert.*;
import static org.assertj.core.api.BDDAssertions.then;
import static org.assertj.core.api.BDDAssertions.thenThrownBy;
@@ -39,6 +44,13 @@ import static org.assertj.core.api.BDDAssertions.thenThrownBy;
public class ExecutorBeanPostProcessorTests {
@Mock BeanFactory beanFactory;
private AsyncProperties asyncProperties;
@Before
public void setup() {
this.asyncProperties = new AsyncProperties();
Mockito.when(beanFactory.getBean(AsyncProperties.class)).thenReturn(this.asyncProperties);
}
@Test
public void should_create_a_cglib_proxy_by_default() throws Exception {
@@ -109,5 +121,32 @@ public class ExecutorBeanPostProcessorTests {
.isInstanceOf(AopConfigException.class)
.hasMessage("foo");
}
@Test
public void proxy_is_not_needed() throws Exception {
this.asyncProperties.setIgnoredBeans(ImmutableList.of("fooExecutor"));
boolean isProxyNeeded = new ExecutorBeanPostProcessor(this.beanFactory).isProxyNeeded("fooExecutor");
assertFalse(isProxyNeeded);
}
@Test
public void proxy_is_needed() throws Exception {
boolean isProxyNeeded = new ExecutorBeanPostProcessor(this.beanFactory).isProxyNeeded("fooExecutor");
assertTrue(isProxyNeeded);
}
@Test
public void should_not_create_proxy() throws Exception {
this.asyncProperties.setIgnoredBeans(ImmutableList.of("fooExecutor"));
Object o = new ExecutorBeanPostProcessor(this.beanFactory)
.postProcessAfterInitialization(new ThreadPoolTaskExecutor(), "fooExecutor");
then(o).isInstanceOf(ThreadPoolTaskExecutor.class);
then(ClassUtils.isCglibProxy(o)).isFalse();
}
}