sjanssen
sjanssen

Reputation: 1

Reactor Context lost after thread switch (in flatMap or WebClient exchange function)

So we're having issues with Project Reactor's automatic context propagation (Hooks.enableAutomaticContextPropagation()). As soon as the reactive stream switches execution thread we're losing the context we introduced at the beginning of the stream.

We've already had a good experience with a Spring Boot application integrated with Spring Cloud GCP trace (via com.google.cloud:spring-cloud-gcp-starter-trace) where the context got propagated correctly across the entire reactive stream and where the log statements got enhanced with the traceId and spanId of the currently active span.

The difference in what we are trying to achieve now however is that the reactive stream is no longer bound to a request/response. (Read: not a Spring Boot endpoint) Instead we are trying to process an infinite stream of messages where every message has an attribute attached to it when it was first created that contains the traceId that denotes the trace in which that message was made. We're then manually creating a child span for that incoming trace so that we can log every statement during processing of that message under the same traceId and spanId. The context however gets lost fairly early in our stream as soon as we do some processing in a flatMap or when doing a request using Spring WebClient.

I've prepared a very simple sample @ https://github.com/sjanssen1/tracing-propagation-sample It basically comes down to that we want the context added here https://github.com/sjanssen1/tracing-propagation-sample/blob/main/src/main/java/sample/Handler.java#L33 to be available downstream. We already have utility ready to let clients like WebClient propagate that information by using request headers and a logger that attaches the values of the context variable traceId and spanId to the logs but for as long as the reactor context is getting lost upon these "hops" we're not getting very far.

Is this a bug in Reactor's Hooks.enableAutomaticContextPropagation()? Are we doing something fundamentally wrong? Any help or suggestions to achieve what we want would be much appreciated.

Bonus: It would be even better if there was a possibility to let a span be "in scope" during every downstream operator that will be applied to one particular message. Opening and closing a span at the right time (upon success or error...) in the reactive stream for example. The difficulty however being that we don't want to introduce any tracing specifics in the implementation of the code that will process the message. It should be something that's handled in the background.

Upvotes: 0

Views: 625

Answers (0)

Related Questions