fix(webflux): register reactor hook in createWebFilter and add filter.#18844
fix(webflux): register reactor hook in createWebFilter and add filter.#18844amit306 wants to merge 1 commit into
Conversation
|
|
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
This PR makes Spring WebFlux 5.3 client/server telemetry automatically register Reactor’s context-propagation hook and adds tests to verify OTel context survives a thread switch.
Changes:
- Register
ContextPropagationOperatorinsideSpringWebfluxClientTelemetry.addFilter()andSpringWebfluxServerTelemetry.createWebFilter(). - Make the explicit “...AndRegisterReactorHook” APIs delegate to the now-hook-registering methods.
- Add unit tests asserting span context propagation across
Schedulers.boundedElastic()thread switching.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 6 comments.
| File | Description |
|---|---|
| instrumentation/spring/spring-webflux/spring-webflux-5.3/library/src/test/java/io/opentelemetry/instrumentation/spring/webflux/v5_3/ReactorHookRegistrationTest.java | Adds tests validating context propagation after hook auto-registration. |
| instrumentation/spring/spring-webflux/spring-webflux-5.3/library/src/main/java/io/opentelemetry/instrumentation/spring/webflux/v5_3/SpringWebfluxServerTelemetry.java | Auto-registers Reactor hook when creating the server WebFilter. |
| instrumentation/spring/spring-webflux/spring-webflux-5.3/library/src/main/java/io/opentelemetry/instrumentation/spring/webflux/v5_3/SpringWebfluxClientTelemetry.java | Auto-registers Reactor hook when adding the client ExchangeFilterFunction. |
Comments suppressed due to low confidence (1)
instrumentation/spring/spring-webflux/spring-webflux-5.3/library/src/main/java/io/opentelemetry/instrumentation/spring/webflux/v5_3/SpringWebfluxClientTelemetry.java:50
- This now registers a global Reactor hook even when
addFilter()returns early because the filter is already present. If the intent is to register the hook only when enabling instrumentation via this API, consider moving hook registration after the ‘already instrumented’ check, and/or guarding registration so it only happens once. This avoids repeated global work and reduces surprising side effects when callers invokeaddFilter()defensively.
public void addFilter(List<ExchangeFilterFunction> exchangeFilterFunctions) {
ContextPropagationOperator.builder().build().registerOnEachOperator();
for (ExchangeFilterFunction filterFunction : exchangeFilterFunctions) {
if (filterFunction instanceof WebClientTracingFilter) {
return;
}
| @AfterEach | ||
| void tearDown() { | ||
| ContextPropagationOperator.builder().build().resetOnEachOperator(); | ||
| } |
| @Test | ||
| void addFilterRegisterRectorHookSoContextPropogatesAcrossThreads() throws Exception { |
| @Test | ||
| void createWebFilterRegisterRectorHookSoContextPropogatesAcrossThreads() throws Exception { |
| /** Returns a {@link WebFilter} that instruments HTTP server requests. */ | ||
| public WebFilter createWebFilter() { | ||
| ContextPropagationOperator.builder().build().registerOnEachOperator(); | ||
| return new TelemetryProducingWebFilter(serverInstrumenter); | ||
| } |
| public WebFilter createWebFilterAndRegisterReactorHook() { | ||
| registerReactorHook(); | ||
| return this.createWebFilter(); | ||
| } |
|
|
||
| /** Returns a {@link WebFilter} that instruments HTTP server requests. */ | ||
| public WebFilter createWebFilter() { | ||
| ContextPropagationOperator.builder().build().registerOnEachOperator(); |
2f62a78 to
4c57afb
Compare
singhvibhanshu
left a comment
There was a problem hiding this comment.
LGTM, just a nit:
Suggested change: return webfluxServerTelemetry.createWebFilter();
4c57afb to
5d17641
Compare
@singhvibhanshu , Done, Thanks for catching this |
|
May I ask for a review from @open-telemetry/java-instrumentation-approvers when any of them get a chance? Thanks! |
|
Ready for review, addressed all feedback. Could someone from @open-telemetry/java-instrumentation-approvers please take a look? |
|
#17858 asks whether the reactor hook should be always registered. Did you attempt to fina an answer to that question? What are the benefits of registering the reactor hook? What would we loose if we did it the other way around and never registered it in these methods? |
@laurit When using Spring WebFlux library instrumentation, the reactor context propagation hook is generally required for OTel context to remain available across the reactive pipeline ( thread switches and scheduler). Without this, spans may be created without the parent context, which will create a broken parent-child relationships, which is hard to identify. Registering the hook directly in createWebFilter() and addFilter() means users get correct behavior by default. If these methods do not register the hook, the user must remember to call ContextPropagationOperator. Missing this call, it can lead to broken trace parent-child relationship and hard to debug. |
Fixes #17858
createWebFilter()andaddFilter()now always register the Reactorcontext propagation hook. The
AndRegisterReactorHookvariants aredeprecated and delegate to these methods.