If a thread switch occurs the MDC context is lost.

You can attach it to the exchange:

public static final AttachmentKey<Map<String, String>> MDC_CONTEXT_KEY = AttachmentKey.create(Map.class);
....
exchange.putAttachment(MDC_CONTEXT_KEY, MDC.getCopyOfContextMap());
...

and then restore it in your worker thread:

MDC.setContextMap(exchange.getAttachment(MDC_CONTEXT_KEY));

Hope this helps,

 Andrea Di Cesare
 Partner at SoftInstigate
 MongoDB Technology Partner 
 
 Via Copernico 38, 20125 Milano (Italy)
 Mobile: +39 329 737 6417
 Skype: ujibang

 The company behind RESTHeart, the NoCoding and NoSQL Platform for MongoDB 


Il giorno mar 2 giu 2020 alle ore 14:57 Мартынов Илья <imartynovsp@gmail.com> ha scritto:
Hello,

I've created a handler to fill MDC from incoming request header.
public class CorrelationHandler implements HttpHandler
{
    private HttpHandler next;
    public CorrelationHandler(HttpHandler next) {
        this.next = next;
    }
    @Override
    public void handleRequest(HttpServerExchange e) throws Exception {
        String corr = e.getRequestHeaders().getFirst("IDP-CORR-ID");
        MDC.put("corr", corr);
        next.handleRequest(e);
    }
}

But most of my processing is done in worker thread and mdc is not passed threre. How to pass it?

_______________________________________________
undertow-dev mailing list
undertow-dev@lists.jboss.org
https://lists.jboss.org/mailman/listinfo/undertow-dev