From ec2c9b5d0e210d62c7a8166a3af7dcd2adaa4115 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-32399 --- .../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 31deb88261da..8c6854143b29 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 @@ -465,7 +465,7 @@ public Mono exchange() { 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 4cd8900ce82a..c3a9460f3c55 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 @@ void recordsObservationForErrorExchange() { 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"); } @@ -180,7 +181,7 @@ void recordsObservationWithResponseDetailsWhenFilterFunctionErrors() { StepVerifier.create(responseMono) .expectError(IllegalStateException.class) .verify(Duration.ofSeconds(5)); - assertThatHttpObservation() + assertThatHttpObservation().hasError() .hasLowCardinalityKeyValue("exception", "IllegalStateException") .hasLowCardinalityKeyValue("status", "200"); }