From f6bc828569dd0c88c3cf1ca0d13bd5a07bbdf5ff Mon Sep 17 00:00:00 2001 From: Brian Clozel Date: Fri, 8 Mar 2024 11:33:45 +0100 Subject: [PATCH] Set error on observation in WebClient instrumentation Prior to this commit, error signals flowing from the client response publisher in `WebClient` would be set on the `Observation.Context`. This is enough for the observation convention to collect data about the error but observation handlers are not notified of this error. This commit sets the error instead on the observation directly to fix this issue. Fixes gh-32389 --- .../web/reactive/function/client/DefaultWebClient.java | 2 +- .../reactive/function/client/WebClientObservationTests.java | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultWebClient.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultWebClient.java index 9518978298e..425e08336d9 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultWebClient.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultWebClient.java @@ -467,7 +467,7 @@ final class DefaultWebClient implements WebClient { final AtomicBoolean responseReceived = new AtomicBoolean(); return responseMono .doOnNext(response -> responseReceived.set(true)) - .doOnError(observationContext::setError) + .doOnError(observation::error) .doFinally(signalType -> { if (signalType == SignalType.CANCEL && !responseReceived.get()) { observationContext.setAborted(true); diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/WebClientObservationTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/WebClientObservationTests.java index f18f0772910..3ef2cdda489 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/WebClientObservationTests.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/WebClientObservationTests.java @@ -110,7 +110,8 @@ class WebClientObservationTests { StepVerifier.create(client.get().uri("/path").retrieve().bodyToMono(Void.class)) .expectError(IllegalStateException.class) .verify(Duration.ofSeconds(5)); - assertThatHttpObservation().hasLowCardinalityKeyValue("exception", "IllegalStateException") + assertThatHttpObservation().hasError() + .hasLowCardinalityKeyValue("exception", "IllegalStateException") .hasLowCardinalityKeyValue("status", "CLIENT_ERROR"); } @@ -172,7 +173,7 @@ class WebClientObservationTests { StepVerifier.create(responseMono) .expectError(IllegalStateException.class) .verify(Duration.ofSeconds(5)); - assertThatHttpObservation() + assertThatHttpObservation().hasError() .hasLowCardinalityKeyValue("exception", "IllegalStateException") .hasLowCardinalityKeyValue("status", "200"); }