Spring-cloud-sleuth: Trace id problem when using Spring ThreadPoolTaskExecutor

Created on 16 Sep 2016  路  5Comments  路  Source: spring-cloud/spring-cloud-sleuth

Hi, I'm not sure if this is a problem with Spring Cloud Sleuth or I'm doing something wrong.

I found that when I use Spring ThreadPoolTaskExecutor the trace id of the parent process is not been used with the asynchronous task.

I have a small example with the problem here:
https://github.com/andcuevas/spring-cloud-sleuth-thread-pool-problem

Regards,
Andres

bug

All 5 comments

Since ThreadPoolTaskExecutor creates a separate thread it drops the MDC variables added by Sleuth and thus you see Trace Id and Span IDs being dropped.
Try to add a Decorator Pattern on top of ThreadPoolTaskExecutor class and override the execute method.

Also the Bean where you are configuring your TaskExecutor, initialize it to the new wrapper class.
Here is a sample implementation :

LogThreadWrapper.txt

I was hoping that Spring Cloud Sleuth and ThreadPoolTaskExecutor were compatible out the box since these are two tools from Spring.

It would be nice to know if a full compatibility will come in the future.

Well I hoped so too, may be that can be a question to @marcingrzejszczak However I dont see that as an issue in Sleuth but if ThreadPoolTaskExecutor can incorporate that in its library.

It should be working - currently I'm on holidays so will try to take a look at this once I'm back

Thanks. This is applicable for any multi-threaded method/class overridden. Best applicable for java 8 lambda functionality.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

scheerer picture scheerer  路  5Comments

gerasimovv88 picture gerasimovv88  路  7Comments

nickcodefresh picture nickcodefresh  路  7Comments

oburgosm picture oburgosm  路  4Comments

longting picture longting  路  7Comments