How to pass on a traceId from gRPC's context to another thread/threadPool?

故事扮演 提交于 2021-01-01 05:05:43


I am using grpc-java and have 3 services, A, B and C. I call service A and then service A calls B and C. I am using Hystrix in the calls to B and C. C in turn spawns another thread to call another service.

I have ClientInterceptors and ServerInterceptors which passes around the traceId. I can see the traceIds in the Context and logs as long as it is a gRPC worker thread but lose them when the call moves to another thread - RxIoScheduler thread or Hystrix thread. How do I pass the traceId around between requests on different threads and between different executor service and thread pools?


While it is possible to propagate in a fine-grained way (like executor.execute(Context.current().wrap(runnable))), you should try to integrate Context propagation into cross-thread work transfer. For many applications, that'd be as simple as wrapping the "main" executor as soon as it is created:

executor = Context.currentContextExecutor(executor);
// executor now auto-propagates

Do that once at the beginning of your application and then you mostly stop worrying about propagation.

But applications will vary. For example, applications that create Threads directly should probably make a ThreadFactory that propagates the calling thread's Context to the Thread:

class PropagatingThreadFactory implements ThreadFactory {
  private final ThreadFactory delegate;

  public PropagatingThreadFactory(ThreadFactory d) {delegate = d;}

  @Override public Thread newThread(Runnable r) {
    return delegate.newThread(Context.current().wrap(r));