From f7307c9e077d3cdc2228c1ace456b0014ab83278 Mon Sep 17 00:00:00 2001 From: Brian Clozel Date: Wed, 19 Jun 2024 12:46:21 +0200 Subject: [PATCH] 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 --- .../org/springframework/web/client/DefaultRestClient.java | 7 ++----- .../web/client/RestClientObservationTests.java | 2 +- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/web/client/DefaultRestClient.java b/spring-web/src/main/java/org/springframework/web/client/DefaultRestClient.java index 16ea2c877c2..c9e4681a92f 100644 --- a/spring-web/src/main/java/org/springframework/web/client/DefaultRestClient.java +++ b/spring-web/src/main/java/org/springframework/web/client/DefaultRestClient.java @@ -240,19 +240,16 @@ 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); - } - throw restClientException; - } - finally { - if (observation != null) { observation.stop(); } + throw restClientException; } } diff --git a/spring-web/src/test/java/org/springframework/web/client/RestClientObservationTests.java b/spring-web/src/test/java/org/springframework/web/client/RestClientObservationTests.java index 62cbca8598b..98c4b136f9a 100644 --- a/spring-web/src/test/java/org/springframework/web/client/RestClientObservationTests.java +++ b/spring-web/src/test/java/org/springframework/web/client/RestClientObservationTests.java @@ -73,7 +73,6 @@ class RestClientObservationTests { @BeforeEach void setupEach() { - this.client = RestClient.builder() .messageConverters(converters -> converters.add(0, this.converter)) .requestFactory(this.requestFactory) @@ -267,6 +266,7 @@ class RestClientObservationTests { private TestObservationRegistryAssert.TestObservationRegistryAssertReturningObservationContextAssert assertThatHttpObservation() { + TestObservationRegistryAssert.assertThat(this.observationRegistry).hasNumberOfObservationsWithNameEqualTo("http.client.requests",1); return TestObservationRegistryAssert.assertThat(this.observationRegistry) .hasObservationWithNameEqualTo("http.client.requests").that(); }