Tracing not working for LogbookClientHandler
rajapanda15 opened this issue · 3 comments
Description
The LogbookClientHandler in Spring Boot 3 is not printing trace IDs when logging requests and responses using the LogbookClientHandler.
Expected Behavior
The logbook instance passed to LogbookClientHandler should log the trace and span ids.
Actual Behavior
The logs are printing empty/blank values.
The below project can be referred
https://github.com/rajapanda15/Springboot3-Logbook.git
I have the same issue.
I don't know if you are using Micrometer and Webflux, but you can check my issue on Reactor Netty.
I managed to make webClient calls working by overriding Logbook's logbookNettyClientCustomizers
and wrapping the LogbookClientHandler
with a custom Netty's ChannelDuplexHandler
as suggested by Violeta.
However, using the same mecanism for customizing the Netty HttpServer
does not work, even though the LogbookServerHandler
is called.
Checkout my sample here on the issues/reactor-netty branch.
You set log pattern to get the traceId
and spanId
from the MDC:
logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss.SSS} %5p [${spring.application.name:},%X{traceId:-},%X{spanId:-}] ...
And then expect them to be added to the log lines of Logbook (red boxes below, where they are empty):
However, notice that green boxes in the above figure: They show that Logbook is being executed from a separate thread called reactor-http-nio-2
, while the original thread was called http-nio-8080-exec-*
(not shown in the figure). It appears that WebClient
runs in a thread separate from the controller.
The important point to note, from Logback documentation (Logback is an implementation of SLF4J API, and is used behind the scenes by Logbook):
The MDC manages contextual information on a per thread basis.
So, here is what happens:
- Your controller receives a call, and the MDC is populated by
traceId
/spanId
- The controller calls WebClient without propagating the MDC. It creates a new thread with an empty MDC map.
- Any logs printed (regardless whether they are by Logbook or not) will not have
traceId
/spanId
.
This article might be helpful: https://ttddyy.github.io/mdc-with-webclient-in-webmvc/
Pay special attention to Spring Boot 3 section. Hope this helps!