sofastack / sofa-tracer

SOFATracer is a component for the distributed system call trace. And through a unified traceId logging the logs of various network calls in the invoking link. These logs can be used for quick discovery of faults, service governance, etc.
Apache License 2.0
1.11k stars 375 forks source link

Spring Async Question #265

Closed zxt47 closed 5 years ago

zxt47 commented 5 years ago

My Question: Hello, guys, I want to know When My Project using Spring Async Annotation in order to start another asynchronous task, I will lose the traceID and spanID in the Main Thread,In my opinion, the context can't inherited? just like this blow: 2019-09-05 10:54:11,449 [TUMSMsgService-1] c.t.p.i.b.g.TumsServiceImplExample INFO [ac16b07b1567652051440100111292,0] >Start handle TUMS msg.MsgType:ARP,MsgContent:Ich liebe dich then async begin: 2019-09-05 10:54:07,312 [SimpleAsyncTaskExecutor-3] worklog INFO [,] >orisys is RCT_MU 2019-09-05 10:54:07,321 [SimpleAsyncTaskExecutor-3] worklog INFO [,] >MODE is CLIENT 2019-09-05 10:54:07,321 [SimpleAsyncTaskExecutor-3] worklog INFO [,] >servicename is RCT_MU 2019-09-05 10:54:07,321 [SimpleAsyncTaskExecutor-3] worklog INFO [,] >connectnum_put is 1 2019-09-05 10:54:07,321 [SimpleAsyncTaskExecutor-3] worklog INFO [,] >econnectnum_get is 1 2019-09-05 10:54:07,321 [SimpleAsyncTaskExecutor-3] worklog INFO [,] >qnameget=RCT_MU_INPUT So how can I make the Spring Async have the Parent Thread's Span?

**### Environment

glmapper commented 5 years ago

@zxt47 A simple implementation is to decorate the context of the current thread to the specified Runnable and finally reset the current thread context.

zxt47 commented 5 years ago

@glmapper

@zxt47 A simple implementation is to decorate the context of the current thread to the specified Runnable and finally reset the current thread context.

额,为什么这里不能使用InheritableThreadLocal或者阿里自研的transmittable-thread-local? 这里:

/**
 * SofaTracerThreadLocalTraceContext
 *
 * @author yangguanchao
 * @since 2018/05/01
 */
public class SofaTracerThreadLocalTraceContext implements SofaTraceContext {

    private final ThreadLocal<SofaTracerSpan> threadLocal = new InheritableThreadLocal<>();
vacuityv commented 2 years ago

我这边通过自定义async的线程池,然后自定义线程池的TaskDecorator来实现的