spring-projects / spring-integration

Spring Integration provides an extension of the Spring programming model to support the well-known Enterprise Integration Patterns (EIP)
http://projects.spring.io/spring-integration/
Apache License 2.0
1.51k stars 1.08k forks source link

Restore ReactorContext into Imperative Processing #9259

Closed syedyusufh closed 3 days ago

syedyusufh commented 1 week ago

ReactorContext is NOT carried onto the following Imperative processing and this results in a new ObservationContext (change in traceId)

SO reference - https://stackoverflow.com/questions/78653090/spring-integration-restore-reactorcontext-into-imperative-processing

Issue happening with Spring Boot v3.3.0

artembilan commented 1 week ago

Was going to suggest another workaround:

.channel("notObserved")

and

@EnableIntegrationManagement(observationPatterns = "!notObserved")

or

@EnableIntegrationManagement(observationPatterns ={  "!notObserved", "*" })

But that does not work: the first one just ignores everything else. The second one ends up with a sort in HashSet, where * got lower hash, therefore it comes first in a matching loop 😄 .

Will raise separate issue and fix. Nothing related for this yet.

artembilan commented 1 week ago

Here is that issue: https://github.com/spring-projects/spring-integration/issues/9260.

BTW, feel free to contribute fixes: https://github.com/spring-projects/spring-integration/blob/main/CONTRIBUTING.adoc

artembilan commented 4 days ago

So, our logic for the reactive reply from this kind of components is like Mono.toFuture(). And looks like there is no Reactor context propagation in there down to the CompletableFuture consumer.

Thinking...

artembilan commented 3 days ago

I have made the fix and now your sample application is OK:

2024-06-28T11:49:18.915-04:00  INFO 31944 --- [   scheduling-1] [667edb7e9be7aa6059b96f78bc67c011-ae124118f96ecbd9] o.s.integration.handler.LoggingHandler   : ----------------------START-------------------------
2024-06-28T11:49:18.919-04:00  INFO 31944 --- [   scheduling-1] [667edb7e9be7aa6059b96f78bc67c011-50473877464ebff9] o.s.integration.handler.LoggingHandler   : WebClient GET Request (start of reactorContext)
2024-06-28T11:49:18.978-04:00  INFO 31944 --- [   scheduling-1] [667edb7e9be7aa6059b96f78bc67c011-8ae480e6d77503b6] c.i.sample.config.TracingConfig          : Request Headers: [Content-Type:"application/json", traceparent:"00-667edb7e9be7aa6059b96f78bc67c011-8ae480e6d77503b6-01"]
2024-06-28T11:49:19.640-04:00  INFO 31944 --- [ctor-http-nio-3] [667edb7e9be7aa6059b96f78bc67c011-8ae480e6d77503b6] c.i.sample.config.TracingConfig          : Response Headers: [Access-Control-Allow-Origin:"*", Alt-Svc:"h3=":443"; ma=2592000", Content-Type:"application/json", Date:"Fri, 28 Jun 2024 15:49:19 GMT", Server:"Caddy", Vary:"Accept-Encoding", Transfer-Encoding:"chunked"]
2024-06-28T11:49:19.667-04:00 ERROR 31944 --- [oundedElastic-1] [667edb7e9be7aa6059b96f78bc67c011-f0b19daa83cd9fd5] .o.WebFluxRequestExecutingMessageHandler : Failed to send async reply: org.springframework.integration.support.MessageBuilder@21b720b4

Will issue Pull Request shortly.

artembilan commented 3 days ago

Forgot to mention. Now you don't need ec -> ec.customizeMonoReply((message, mono) -> mono.contextCapture()) since it is done in the framework by itself.

syedyusufh commented 2 days ago

Thanks for the quick fix.