Home > Back-end >  spring cloud sleuth - how to propagate trace id and span id to a listener
spring cloud sleuth - how to propagate trace id and span id to a listener

Time:12-15

I have a bean which is MessageHandler to handle an incoming message. The message handler is of type org.springframework.integration.aws.outbound.S3MessageHandler, which uploads the message to amazon s3. The issue is that, the operations of this message handler is performed in a different thread. How can I ensure that I can track the transaction id is propagated all the way to the thread performing this transaction?

DEBUG [app-name,,] 22540 --- [anager-worker-1] 

Also attached to this message handler is a progress listener of type com.amazonaws.services.s3.transfer.internal.S3ProgressListener. The callbacks to this listener are performed within a different thread altogether. I need the trace ids in this listener too.

INFO [app-name,,] 22540 --- [callback-thread]

CodePudding user response:

You may use MDC logging feature and log your trace id and span id.

The key problem that you have several threads for processing that leads to question how to correlate the logs between them.. so,

  1. Put traceid span unique message id into MDC of thread that processing request
  2. Put uniq message id into MDC of thread that MessageHelper uses
  3. Modify your appenders to print traceid, span, unique message id into logs

Such fields could be well indexed by ELK (or any similar tool, even grep). Then, by searching in logs by uniq message id you'll find all logs record threads names and other details like traceid and spanid

CodePudding user response:

Sleuth can do this for you, you need to check the docs how to use its API: https://docs.spring.io/spring-cloud-sleuth/docs/current/reference/html/using.html#using-creating-and-ending-spans

As you can see there, you can create a Span and you can also create a Scope (in SpanInScope ). The Scope is responsible for the context propagation, e.g.: handling the MDC for you so you don't need to do anything with the MDC.

What you need to do though is properly using this API so the context is propagated. This is usually done by instrumenting the ExuecutorService/CompletionService and/or Runnable/Callable (whichever you use). Here's how to implement such a thing:

  1. Get the current Span in the caller thread (tracer.currentSpan())
  2. Use this span and create a Scope (and a new Span if you need) in the "other" thread (inside of the thread-pool)

You don't need to do this on your own, Sleuth has a TraceRunnable (this is doing exactly what I described above) and a TraceableExecutorService.

  • Related