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
This commit is contained in:
Jonatan Ivanov 2024-08-07 16:33:29 -07:00 committed by Stéphane Nicoll
parent 1e97b2137b
commit 36e84a5209
1 changed files with 4 additions and 3 deletions

View File

@ -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 {
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) {