Skip to content

Commit 7836307

Browse files
wuyupengwoainigeoand
authored andcommitted
Fix Async instrumentation when using AsyncConfigurerSupport (#251)
* fix issue-183(@async instrumentation not working when using AsyncConfigurerSupport)
1 parent 48d5a1b commit 7836307

File tree

2 files changed

+72
-3
lines changed

2 files changed

+72
-3
lines changed

instrument-starters/opentracing-spring-cloud-core/src/main/java/io/opentracing/contrib/spring/cloud/async/CustomAsyncConfigurerAutoConfiguration.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2017-2018 The OpenTracing Authors
2+
* Copyright 2017-2019 The OpenTracing Authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
55
* in compliance with the License. You may obtain a copy of the License at
@@ -25,6 +25,7 @@
2525
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
2626
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
2727
import org.springframework.context.annotation.Configuration;
28+
import org.springframework.core.PriorityOrdered;
2829
import org.springframework.scheduling.annotation.AsyncConfigurer;
2930

3031
/**
@@ -39,7 +40,7 @@
3940
@AutoConfigureBefore(DefaultAsyncAutoConfiguration.class)
4041
@AutoConfigureAfter(TracerAutoConfiguration.class)
4142
@ConditionalOnProperty(name = "opentracing.spring.cloud.async.enabled", havingValue = "true", matchIfMissing = true)
42-
public class CustomAsyncConfigurerAutoConfiguration implements BeanPostProcessor {
43+
public class CustomAsyncConfigurerAutoConfiguration implements BeanPostProcessor,PriorityOrdered {
4344

4445
@Autowired
4546
private Tracer tracer;
@@ -52,10 +53,15 @@ public Object postProcessBeforeInitialization(Object bean, String beanName)
5253

5354
@Override
5455
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
55-
if (bean instanceof AsyncConfigurer) {
56+
if (bean instanceof AsyncConfigurer && !(bean instanceof TracedAsyncConfigurer)) {
5657
AsyncConfigurer configurer = (AsyncConfigurer) bean;
5758
return new TracedAsyncConfigurer(tracer, configurer);
5859
}
5960
return bean;
6061
}
62+
63+
@Override
64+
public int getOrder() {
65+
return PriorityOrdered.LOWEST_PRECEDENCE;
66+
}
6167
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
* Copyright 2017-2019 The OpenTracing Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
package io.opentracing.contrib.spring.cloud.async;
15+
16+
import io.opentracing.contrib.spring.cloud.MockTracingConfiguration;
17+
import io.opentracing.contrib.spring.cloud.async.instrument.TracedAsyncConfigurer;
18+
import java.util.concurrent.Executor;
19+
import org.assertj.core.api.BDDAssertions;
20+
import org.junit.Test;
21+
import org.junit.runner.RunWith;
22+
import org.springframework.beans.factory.annotation.Autowired;
23+
import org.springframework.boot.test.context.SpringBootTest;
24+
import org.springframework.context.annotation.Configuration;
25+
import org.springframework.scheduling.annotation.AsyncConfigurer;
26+
import org.springframework.scheduling.annotation.AsyncConfigurerSupport;
27+
import org.springframework.scheduling.annotation.EnableAsync;
28+
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
29+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
30+
31+
32+
33+
34+
/**
35+
* @Author wuyupeng
36+
**/
37+
@SpringBootTest(classes = {AsyncConfigurerTest.AsyncConfigurerConfig.class, MockTracingConfiguration.class, CustomAsyncConfigurerAutoConfiguration.class})
38+
@RunWith(SpringJUnit4ClassRunner.class)
39+
public class AsyncConfigurerTest {
40+
41+
@Autowired
42+
private AsyncConfigurer asyncConfigurer;
43+
44+
45+
@Configuration
46+
@EnableAsync
47+
static class AsyncConfigurerConfig extends AsyncConfigurerSupport {
48+
@Override
49+
public Executor getAsyncExecutor() {
50+
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
51+
executor.initialize();
52+
return executor;
53+
}
54+
}
55+
56+
/**
57+
* Test if the AsyncConfigurer configured by developer can be replaced with TracedAsyncConfigurer by CustomAsyncConfigurerAutoConfiguration
58+
*/
59+
@Test
60+
public void testIsTracedAsyncConfigurer() {
61+
BDDAssertions.then(asyncConfigurer).isInstanceOf(TracedAsyncConfigurer.class);
62+
}
63+
}

0 commit comments

Comments
 (0)