Browse Source

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
pull/32403/head
Brian Clozel 2 years ago
parent
commit
f6bc828569
  1. 2
      spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultWebClient.java
  2. 5
      spring-webflux/src/test/java/org/springframework/web/reactive/function/client/WebClientObservationTests.java

2
spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultWebClient.java

@ -467,7 +467,7 @@ final class DefaultWebClient implements WebClient { @@ -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);

5
spring-webflux/src/test/java/org/springframework/web/reactive/function/client/WebClientObservationTests.java

@ -110,7 +110,8 @@ class WebClientObservationTests { @@ -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 { @@ -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");
}

Loading…
Cancel
Save