Ensure that client responses are observed when filters fail
Prior to this commit, an error thrown by a `ExchangeFilterFunction` configured on a `WebClient` instance would be recorded as such by the client observation, but the response details would be missing from the observation. All filter functions and the exchange function (performing the HTTP call) would be merged into a single `ExchangeFunction`; this instance was instrumented and osberved. As a result, the instrumentation would only get the error signal returned by the filter function and would not see the HTTP response even if it was received. This means that the recorded observation would not have the relevant information for the HTTP status. This commit ensures that between the configured `ExchangeFilterFunction` and the `ExchangeFunction`, an instrumentation `ExchangeFilterFunction` is inserted. This allows to set the client response to the observation context, even if a later error signal is thrown by a filter function. Note that with this change, an error signal sent by a filter function will be still recorded in the observation. See gh-30059
This commit is contained in:
@@ -133,6 +133,19 @@ class WebClientObservationTests {
|
||||
verifyAndGetRequest();
|
||||
}
|
||||
|
||||
@Test
|
||||
void recordsObservationWithResponseDetailsWhenFilterFunctionErrors() {
|
||||
ExchangeFilterFunction errorFunction = (req, next) -> next.exchange(req).then(Mono.error(new IllegalStateException()));
|
||||
WebClient client = this.builder.filter(errorFunction).build();
|
||||
Mono<Void> responseMono = client.get().uri("/path").retrieve().bodyToMono(Void.class);
|
||||
StepVerifier.create(responseMono)
|
||||
.expectError(IllegalStateException.class)
|
||||
.verify(Duration.ofSeconds(5));
|
||||
assertThatHttpObservation()
|
||||
.hasLowCardinalityKeyValue("exception", "IllegalStateException")
|
||||
.hasLowCardinalityKeyValue("status", "200");
|
||||
}
|
||||
|
||||
private TestObservationRegistryAssert.TestObservationRegistryAssertReturningObservationContextAssert assertThatHttpObservation() {
|
||||
return TestObservationRegistryAssert.assertThat(this.observationRegistry)
|
||||
.hasObservationWithNameEqualTo("http.client.requests").that();
|
||||
|
||||
Reference in New Issue
Block a user