[undertow-dev] pass MDC to worker thread

James Perkins jperkins at redhat.com
Thu Jun 4 15:07:50 EDT 2020


You'd have to log from the handler as MDC is associated with a ThreadLocal.

On Wed, Jun 3, 2020 at 1:35 AM Мартынов Илья <imartynovsp at gmail.com> wrote:

> Thank you for your answers. I understand I need to explicitly set MDC in
> worker thread. Problem is I cannot modify the codebase of application that
> runs on Wildfly. Actually it is a war deployed into Wildfly, and I cannot
> re-build it. I want to customize only undertow/wildfly.
> The custom handler I add this way runs in IO thread:
>
> /subsystem=undertow/configuration=filter/custom-filter=correlation:add(module="com.ingrammicro.idp.undertow-correlation",class-name=com.ingrammicro.idp.CorrelationHandler)
>
> /subsystem=undertow/server=default-server/host=default-host/filter-ref=correlation:add()
>
> But from stacktrace I see multiple handlers run in worker thread:
> handleRequest:43, PredicateHandler (io.undertow.server.handlers)
> handleRequest:68, RedirectDirHandler (io.undertow.servlet.handlers)
> handleRequest:132, SSLInformationAssociationHandler
> (io.undertow.servlet.handlers.security)
> handleRequest:57, ServletAuthenticationCallHandler
> (io.undertow.servlet.handlers.security)
>
> Also, I can add an expression handler that runs in worker thread:
> /subsystem=undertow/configuration=filter/expression-filter=IM_private:add(,expression="path-template('/auth/realms/{realm}/restart-session')
> -> ip-access-control(acl={'127.0.0.1 allow', '0:0:0:0:0:0:0:1 allow'})")
>
> /subsystem=undertow/server=default-server/host=default-host/filter-ref=IM_private:add()
>
> Is it possible to add a custom handler to run in a worker thread?
>
>
>
> ср, 3 июн. 2020 г. в 10:23, Andrea Di Cesare <andrea at softinstigate.com>:
>
>> 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 Soft*Instigate* <https://softinstigate.com/>
>>  MongoDB Technology Partner
>>   <https://sched.co/b2Hz>
>>  Via Copernico 38, 20125 Milano (Italy)
>>  Mobile: +39 329 737 6417
>>  Skype: ujibang
>>
>>  The company behind *REST*Heart <https://restheart.org>, the NoCoding
>> and NoSQL Platform for MongoDB
>>
>>
>> Il giorno mar 2 giu 2020 alle ore 14:57 Мартынов Илья <
>> imartynovsp at 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 at lists.jboss.org
>>> https://lists.jboss.org/mailman/listinfo/undertow-dev
>>
>> _______________________________________________
> undertow-dev mailing list
> undertow-dev at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/undertow-dev



-- 
James R. Perkins
JBoss by Red Hat
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/undertow-dev/attachments/20200604/ae48cf79/attachment.html 


More information about the undertow-dev mailing list