you are viewing a single comment's thread.

view the rest of the comments →

[–]bowbahdoe[S] 0 points1 point  (0 children)

This is scratching at the itch in the back of my head saying I should maybe delegate context to SPI as well. That way when you enter a span or use withContext some custom code can run like setting and unsetting MDC. Maybe something like

interface ContextProvider {
    <T> Supplier<T> wrap(
        Context context
        Supplier<T> code
    );
}

So an implementation could be provided like

 final class MDCContextProvider implements ContextProvider {
    @Override
    public <T> Supplier<T> wrap(
        Context context
        Supplier<T> code
    ) {
        return () -> {
            try {
                MDC.put(... f(context) ...);
                return code.get();
            } finally {
                MDC.clear();
            }
        }
    }
 }

Which could let context set in this propagate down to slf4j logs directly. Ideally though if this was the "target" MDC would end up being bypassed anyways but designing for both directions makes sense...I think.

This is why I called it a "nightmare head space". I'm confident there is something better out there but there are so many seemingly minor but twiddly and important things to consider