-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Guidance on logging with MDC #1985
Comments
Hi, I had usecase using MDC with WebClient and summarized how to do it in my blog post, MDC with WebClient in WebMVC. I put implementations with The implementation is I only considerd for WebMVC environment(I haven't thought about pure reactive env), but it is helpful to have such info as well since usage of |
What about the solution presented with @ttddyy could you give a working example with |
Here a proof that the mdc is not retained using the
If you execute this, the MDC is correctly filled during the |
Hi @membersound Thanks for checking the post. There are couple of things: public static ExchangeFilterFunction mdcFilter = (request, next) -> {
// here runs on main(request's) thread
Map<String, String> map = MDC.getCopyOfContextMap();
return next.exchange(request)
.doOnNext(value -> { // <======= HERE
// here runs on reactor's thread
if (map != null) {
MDC.setContextMap(map);
}
});
}; Another thing is the order of applying filters to WebClient webClient = WebClient.builder()
.filter(logResponse)
.filter(logRequest)
.filter(mdcFilter)
.build(); Please try with these two changes. |
@ttddyy thanks for the guidance, I could get it working when using your filter order. Plus I had to change the
Now the question is: is the @simonbasle Thanks for adding a faq to MDC logging directly on a |
Hello Everyone, In our webflux project we are using the publisher hooks as mentioned above to populate
With traceIds
If you see the response time increase exponentially with the load while without traceIDs it is much better. Any suggestion if there are better ways of using traceids without hampering the performance. Note: There is a slight change in our implementation, wee are also calling the |
I ran into the same problems. We're using OpenTelemetry and I was sticking the trace info into MDC and then using a Reactor operator to copy the MDC between threads. This seemed to work well but does not scale. Using features in log4j2 I've found a few mitigations that so far seem to work pretty well. They generally require leaning on storing the information in the OpenTelemetry context and using a Reactor operator to propagate that around instead. First I am using a custom Second I am using a custom implementation of Our not-very-scientific k6-based benchmarks are showing that with 1000 RPS before these changes we were seeing a P95 of 2.89s, and afterwards P95 is down to 11.43ms. YMMV. |
Hello fellow programmers, Sorry, but I don't understand how the guidance examples resolve the MDC logging problem. The main point IMHO is to be able to set context variables so they can be displayed in each logging entry occurring in the same context after that, it's useful because it enables the third party library log entries to show those variables. But in the guide examples, the MDC variables are set just before the log statement and unset just after it. I'm probably missing something, but still, if so, I guess I won't be the only one. Anyway, many thanks for your impressive work |
I'm interested in this as well. It would be interesting to have a way that the MDC can be populated for the entire life of a particular stream of events. Currently I have a way to set it for code that is in my control but as soon as I delegate to another library like mongodb driver the contextual information in the logs is lost. |
The Reactor documentation has Adding Context To a Reactive Sequence that covers the topic of
ThreadLocal
alternatives but there is no mention of how to log with MDC context.@simonbasle has a blog post but I think it must not be easy to find because various suggestions keep showing up that typically tend to not work in all cases or have significant trade-offs.
There should be some guidance that is easier to find. Possibly even a short section in the Reactor docs with a link to the blog post. Whatever it ends up being, we can further link to it from the Spring Framework docs.
A more ambitious option would be to provide logging framework integrations but that could end up as a separate issue. In the mean time some minimal documentation would really help.
The text was updated successfully, but these errors were encountered: