From 608228d617439f7cc80fba18d75a96cb4af5ebcb Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Fri, 23 Aug 2019 20:28:26 +0100 Subject: [PATCH] Improve handling of non-standard status codes in WebClient metrics Fixes gh-17695 --- .../web/reactive/client/WebClientExchangeTags.java | 2 +- .../client/DefaultWebClientExchangeTagsProviderTests.java | 2 +- .../client/MetricsWebClientFilterFunctionTests.java | 4 ++-- .../web/reactive/client/WebClientExchangeTagsTests.java | 8 +++++++- 4 files changed, 11 insertions(+), 5 deletions(-) diff --git a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web/reactive/client/WebClientExchangeTags.java b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web/reactive/client/WebClientExchangeTags.java index c5cded47a5..9e710e087c 100644 --- a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web/reactive/client/WebClientExchangeTags.java +++ b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web/reactive/client/WebClientExchangeTags.java @@ -80,7 +80,7 @@ public final class WebClientExchangeTags { * @return the status tag */ public static Tag status(ClientResponse response) { - return Tag.of("status", String.valueOf(response.statusCode().value())); + return Tag.of("status", String.valueOf(response.rawStatusCode())); } /** diff --git a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/web/reactive/client/DefaultWebClientExchangeTagsProviderTests.java b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/web/reactive/client/DefaultWebClientExchangeTagsProviderTests.java index bd3ca34639..7ca5b30f28 100644 --- a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/web/reactive/client/DefaultWebClientExchangeTagsProviderTests.java +++ b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/web/reactive/client/DefaultWebClientExchangeTagsProviderTests.java @@ -53,7 +53,7 @@ public class DefaultWebClientExchangeTagsProviderTests { this.request = ClientRequest.create(HttpMethod.GET, URI.create("https://example.org/projects/spring-boot")) .attribute(URI_TEMPLATE_ATTRIBUTE, "https://example.org/projects/{project}").build(); this.response = mock(ClientResponse.class); - given(this.response.statusCode()).willReturn(HttpStatus.OK); + given(this.response.rawStatusCode()).willReturn(HttpStatus.OK.value()); } @Test diff --git a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/web/reactive/client/MetricsWebClientFilterFunctionTests.java b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/web/reactive/client/MetricsWebClientFilterFunctionTests.java index 880999db38..69b15b8c73 100644 --- a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/web/reactive/client/MetricsWebClientFilterFunctionTests.java +++ b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/web/reactive/client/MetricsWebClientFilterFunctionTests.java @@ -71,7 +71,7 @@ public class MetricsWebClientFilterFunctionTests { public void filterShouldRecordTimer() { ClientRequest request = ClientRequest .create(HttpMethod.GET, URI.create("https://example.com/projects/spring-boot")).build(); - given(this.response.statusCode()).willReturn(HttpStatus.OK); + given(this.response.rawStatusCode()).willReturn(HttpStatus.OK.value()); this.filterFunction.filter(request, this.exchange).block(Duration.ofSeconds(30)); assertThat(this.registry.get("http.client.requests") .tags("method", "GET", "uri", "/projects/spring-boot", "status", "200").timer().count()).isEqualTo(1); @@ -82,7 +82,7 @@ public class MetricsWebClientFilterFunctionTests { ClientRequest request = ClientRequest .create(HttpMethod.GET, URI.create("https://example.com/projects/spring-boot")) .attribute(URI_TEMPLATE_ATTRIBUTE, "/projects/{project}").build(); - given(this.response.statusCode()).willReturn(HttpStatus.OK); + given(this.response.rawStatusCode()).willReturn(HttpStatus.OK.value()); this.filterFunction.filter(request, this.exchange).block(Duration.ofSeconds(30)); assertThat(this.registry.get("http.client.requests") .tags("method", "GET", "uri", "/projects/{project}", "status", "200").timer().count()).isEqualTo(1); diff --git a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/web/reactive/client/WebClientExchangeTagsTests.java b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/web/reactive/client/WebClientExchangeTagsTests.java index 49f865c0d5..15f14222b3 100644 --- a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/web/reactive/client/WebClientExchangeTagsTests.java +++ b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/web/reactive/client/WebClientExchangeTagsTests.java @@ -51,7 +51,6 @@ public class WebClientExchangeTagsTests { this.request = ClientRequest.create(HttpMethod.GET, URI.create("https://example.org/projects/spring-boot")) .attribute(URI_TEMPLATE_ATTRIBUTE, "https://example.org/projects/{project}").build(); this.response = mock(ClientResponse.class); - given(this.response.statusCode()).willReturn(HttpStatus.OK); } @Test @@ -85,6 +84,7 @@ public class WebClientExchangeTagsTests { @Test public void status() { + given(this.response.rawStatusCode()).willReturn(HttpStatus.OK.value()); assertThat(WebClientExchangeTags.status(this.response)).isEqualTo(Tag.of("status", "200")); } @@ -99,4 +99,10 @@ public class WebClientExchangeTagsTests { .isEqualTo(Tag.of("status", "CLIENT_ERROR")); } + @Test + public void statusWhenNonStandard() { + given(this.response.rawStatusCode()).willReturn(490); + assertThat(WebClientExchangeTags.status(this.response)).isEqualTo(Tag.of("status", "490")); + } + }