-
Notifications
You must be signed in to change notification settings - Fork 38.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
spring-boot-starter-webflux doesn't put traceId in logger MDC #29466
Comments
Thanks for creating this issue. I'll try to summarize the current state of things here. MVC and WebFlux have a different instrumentation and that's intended. With Spring MVC, we're indeed opening an Micrometer documents the various instrumentation strategies for reactive libraries. If I'm not mistaken, using the I think you're conflating observation scopes and traces. We will not open a scope in the reactive Observation filter as it won't solve this problem. Your sample is using a simple, synchronous variant of a Controller handler, but using the same setup and logging from the pipeline (within a I'm closing this issue as a result, pinging @marcingrzejszczak so that he can correct me if I said something wrong! |
I can add one more thing that setting scope in reactive (so putting in thread local) and closing out on finally can lead to the situation where observation is put in thread local for thread 1 but it's cleared for thread 2 (cause on finally would be called in thread 2). So we would be polluting thread number 1. |
Well, this is probably true; I am just trying to learn this stuff now, so apologies if I have misunderstood. :) I think I follow you now, and see the issue. I have had another look at this. It seems that it can be made to work using a Reactor This seems to work well across multiple execution threads. Would something like the above be problematic, perhaps from a performance point of view? The thing with all this is, the initial problem statement remains: users will expect the It seems to me that expecting users to known about So getting as much of this to work "out of the box" seems like a reasonable thing to expect, at least in Spring Boot. |
The MDC topic is extensively documented in Reactor and your performance concerns have been experienced by others.
Taking a step back, this is not really about observability but the fact that MDC is based on
You can follow the reasoning and multiple solutions considered in reactor/reactor-core#3149. I understand the appeal of that feature, but I think that making the MDC available everywhere in reactive code is a non-goal in general. This is likely to make the runtime cost higher than just using a Servlet-based approach. Issues related to |
Thanks for the detailed reply. It is interesting that since reactor/reactor-core#3180 it is now the default behaviour to transparently restore the ThreadLocals for the One last question: is it worth considering, in the special case where the handler method returns a non-reactive type, to use the Something like:
This then delegates all of the special handling to Reactor. |
Sorry, the above suggestion is for |
I don't think we should apply this suggestion as this would only be applied for synchronous method signatures but not on reactive ones. This would confuse developers even more. We should document this properly. |
Please have a look at the outcome of the related issue: spring-projects/spring-boot#33372 (comment) |
Good morning! I'm running into the similar issue when switching from Spring Cloud Sleuth to Micrometer Tracing. With Sleuth everything just seems to work with Reactor based Spring project. When upgrading our service to Spring Boot 3.1.0 and adding the dependency,
the |
@joseph-freeman I'm afraid this is a bit vague. We do have working applications supporting this case. Please create a new issue with a minimal sample application showing the problem. |
I'm having exactly the same issue. This is the logging pattern for both versions:
|
Same thing @gianielsevier , this description is not enough for us to help you. Please create a new issue with a sample application showing the problem. |
Affects:
6.0.0-RC4
Expected Behaviour
When using
spring-boot-starter-webflux
I expect log message written in@RequestMapping
s methods to have thetraceId
andspanId
in the MDC.When using
spring-boot-starter-web
(non-reactive) this is indeed the observed behaviour, but it is not when usingspring-boot-starter-webflux
, as the logger MDC has notraceId
norspanId
properties.Having the
traceId
andspanId
in the MDC is helpful, as it allows correlation of spans to log messages. This is an established use case, see for example the "jump from trace to logs" feature in Grafana Tempo.Observed Behaviour
See this repo for example https://github.com/0x006EA1E5/spring-boot-3-reactive-trace-logging
I can see when using
spring-boot-starter-web
that this functions as expected, as the non-reactiveServerHttpObservationFilter
first create, then starts a newObservation
, then wraps the rest of the filter chain in a try-with-resources block that opens anObservation.Scope
.However, using
spring-boot-starter-webflux
, with the reactiveServerHttpObservationFilter
, noObservation.Scope
is opened, although anObservation
is created andstart()
edIt seems odd, that although an
Observation
is created and started, noObservation.Scope
is explicitly opened.I can see that the
Observation.Scope
isAutoCloseable
. Indeed, it appears that if theAutoCloseable
is not closed at the end of a request, then the next request maintains the sametraceId
, which seems obviously wrong.Therefore, it would seem that the
AutoCloseable
should be created as a step in theMono
, and closed in adoFinally()
handler, but I don't understand this well.See this thread for more discussion: https://gitter.im/spring-projects/spring-boot?at=636bb545abfb5860bb0aca48
Alternatively, perhaps when an
Observation
is started, a defaultObservation.Scope
should be started, which should be closed when theObservation
isstop()
ed?The text was updated successfully, but these errors were encountered: