Skip to content

Commit 36e84a5

Browse files
jonatan-ivanovsnicoll
authored andcommitted
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
1 parent 1e97b21 commit 36e84a5

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ private <T> T readWithMessageConverters(ClientHttpResponse clientResponse, Runna
196196

197197
MediaType contentType = getContentType(clientResponse);
198198

199-
try (clientResponse) {
199+
try {
200200
callback.run();
201201

202202
IntrospectingClientHttpResponse responseWrapper = new IntrospectingClientHttpResponse(clientResponse);
@@ -240,17 +240,18 @@ private <T> T readWithMessageConverters(ClientHttpResponse clientResponse, Runna
240240
ResolvableType.forType(bodyType) + "] and content type [" + contentType + "]", cause);
241241
if (observation != null) {
242242
observation.error(restClientException);
243-
observation.stop();
244243
}
245244
throw restClientException;
246245
}
247246
catch (RestClientException restClientException) {
248247
if (observation != null) {
249248
observation.error(restClientException);
250-
observation.stop();
251249
}
252250
throw restClientException;
253251
}
252+
finally {
253+
clientResponse.close();
254+
}
254255
}
255256

256257
private static MediaType getContentType(ClientHttpResponse clientResponse) {

0 commit comments

Comments
 (0)