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-32399
pull/33048/head
Brian Clozel 2 years ago
parent
commit
ec2c9b5d0e
  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

@ -465,7 +465,7 @@ final class DefaultWebClient implements WebClient { @@ -465,7 +465,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");
}
@ -180,7 +181,7 @@ class WebClientObservationTests { @@ -180,7 +181,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