I use Sping-Webflux
and want to automatically add jaeger
traceId into my @Slf4j
log.
I have an appender in my logback.xml
with %X{traceId}
pattern like this
<Pattern>
%d{yyyy-MM-dd HH:mm:ss} %X{traceId} -2048msg%n
</Pattern>
Following to this answer on a similiar topic I have created a custom web filter where I added traceId
into MDC
:
@Component
@RequiredArgsConstructor
public class CustomWebFilter implements WebFilter {
private final Tracer tracer;
@Override
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
SpanContext spanContext = tracer.activeSpan().context();
MDC.put("traceId", spanContext.toTraceId());
return chain
.filter(exchange);
}
}
I have tested it and it seems it works. But I have doubts that this solution is correct. I do not understand how a static MDC.put
can handle simultaneously multiple different log events if they happen at the same time? When I put traceId
into MDC
will it affect all logs or only the required log which is linked to the web request?