Skip to content

Commit f7307c9

Browse files
committed
Avoid recording RestClient observations twice
Prior to this commit, the fix for gh-32575 introduced cases where the client observation would be stopped twice. This commit ensures that `RestClient` observations are stopped only once when the response is closed, or before throwing an unhanlded exception. Fixes gh-33068
1 parent 098c4b1 commit f7307c9

File tree

2 files changed

+3
-6
lines changed

2 files changed

+3
-6
lines changed

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

+2-5
Original file line numberDiff line numberDiff line change
@@ -240,19 +240,16 @@ 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();
243244
}
244245
throw restClientException;
245246
}
246247
catch (RestClientException restClientException) {
247248
if (observation != null) {
248249
observation.error(restClientException);
249-
}
250-
throw restClientException;
251-
}
252-
finally {
253-
if (observation != null) {
254250
observation.stop();
255251
}
252+
throw restClientException;
256253
}
257254
}
258255

spring-web/src/test/java/org/springframework/web/client/RestClientObservationTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ class RestClientObservationTests {
7373

7474
@BeforeEach
7575
void setupEach() {
76-
7776
this.client = RestClient.builder()
7877
.messageConverters(converters -> converters.add(0, this.converter))
7978
.requestFactory(this.requestFactory)
@@ -267,6 +266,7 @@ private void mockResponseBody(String expectedBody, MediaType mediaType) throws E
267266

268267

269268
private TestObservationRegistryAssert.TestObservationRegistryAssertReturningObservationContextAssert assertThatHttpObservation() {
269+
TestObservationRegistryAssert.assertThat(this.observationRegistry).hasNumberOfObservationsWithNameEqualTo("http.client.requests",1);
270270
return TestObservationRegistryAssert.assertThat(this.observationRegistry)
271271
.hasObservationWithNameEqualTo("http.client.requests").that();
272272
}

0 commit comments

Comments
 (0)