Browse Source

Fix RestClient instrumentation

ClientHttpResponse implements Closeable and the close method of
DefaultConvertibleClientHttpResponse also stops the current Observation.

Before this change exiting the try-with-resource block stopped the
Observation since it called close on ClientHttpResponse.
After this, there were multiple error and stop calls on the Observation
in the catch blocks after the Observation was already stopped
which is invalid.

This change reorders the flow by stopping the Observation in the
finally block (closing ClientHttpResponse) and not stopping
the Observation in any of the catch blocks.

Closes gh-33346
pull/33365/head
Jonatan Ivanov 2 years ago committed by Stéphane Nicoll
parent
commit
36e84a5209
  1. 7
      spring-web/src/main/java/org/springframework/web/client/DefaultRestClient.java

7
spring-web/src/main/java/org/springframework/web/client/DefaultRestClient.java

@ -196,7 +196,7 @@ final class DefaultRestClient implements RestClient { @@ -196,7 +196,7 @@ final class DefaultRestClient implements RestClient {
MediaType contentType = getContentType(clientResponse);
try (clientResponse) {
try {
callback.run();
IntrospectingClientHttpResponse responseWrapper = new IntrospectingClientHttpResponse(clientResponse);
@ -240,17 +240,18 @@ final class DefaultRestClient implements RestClient { @@ -240,17 +240,18 @@ final class DefaultRestClient implements RestClient {
ResolvableType.forType(bodyType) + "] and content type [" + contentType + "]", cause);
if (observation != null) {
observation.error(restClientException);
observation.stop();
}
throw restClientException;
}
catch (RestClientException restClientException) {
if (observation != null) {
observation.error(restClientException);
observation.stop();
}
throw restClientException;
}
finally {
clientResponse.close();
}
}
private static MediaType getContentType(ClientHttpResponse clientResponse) {

Loading…
Cancel
Save