Search code examples
spring-bootnettylogbackmdcdistributed-tracing

Logback MDC on Netty or any other non-blocking IO server


Logback MDC (Mapped Diagnostic Context) is leveraging threadLocal (As far as I know) so that it will be accessible on all the log statements executed by the same thread.

My question is, will logback MDC work in the non blocking IO server-side runtime like Netty or Undertow as it used to work in for example, tomcat? If yes, how is it working as Netty/Undertow is not following one-thread-per-request unlike tomcat.

I am trying to put a traceID in MDC, so that I can trace all my log from a one transaction trail across multiple microservices/pipeline-listeners in a centralized logging system like Splunk/ELK


Solution

  • I'm not terribly familiar with using netty directly, but I do know that it is possible to use logback MDC with asynchronous code. It isn't easy though.

    Basically you need to somehow tie the traceId to the request/connection, and each time you start processing for that connection, set the traceId in the MDC for the current thread.

    If you are using a thread pool, you can do this with a custom Executor that gets the current state (ex. traceId) from the current thread local storage, and creates a wrapped Runnable or Callable that sets thread local storage to the previously retrieved value before running. That wrapped runnable is then forwarded to a delegate Executor.

    You can see how this is done here: https://github.com/lucidsoftware/java-thread-context/blob/master/thread-context/src/main/java/com/github/threadcontext/PropagatingExecutorService.java

    In fact, depending on your needs, you may be able to use that java-thread-context project.