From b15e427a3e5d7232e21fcfac43c02763f73b68ad Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Wed, 18 Sep 2019 09:52:39 +0100 Subject: [PATCH] Improve handling of non-standard status codes in WebFluxTags Closes gh-18267 --- .../metrics/web/reactive/server/WebFluxTags.java | 15 +++++++++++++-- .../web/reactive/server/WebFluxTagsTests.java | 14 ++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web/reactive/server/WebFluxTags.java b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web/reactive/server/WebFluxTags.java index 1e26db8033..f01ac80675 100644 --- a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web/reactive/server/WebFluxTags.java +++ b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web/reactive/server/WebFluxTags.java @@ -20,6 +20,8 @@ import io.micrometer.core.instrument.Tag; import org.springframework.boot.actuate.metrics.http.Outcome; import org.springframework.http.HttpStatus; +import org.springframework.http.server.reactive.AbstractServerHttpResponse; +import org.springframework.http.server.reactive.ServerHttpResponse; import org.springframework.util.StringUtils; import org.springframework.web.reactive.HandlerMapping; import org.springframework.web.server.ServerWebExchange; @@ -133,9 +135,18 @@ public final class WebFluxTags { * @since 2.1.0 */ public static Tag outcome(ServerWebExchange exchange) { - HttpStatus status = exchange.getResponse().getStatusCode(); - Outcome outcome = (status != null) ? Outcome.forStatus(status.value()) : Outcome.UNKNOWN; + Integer statusCode = extractStatusCode(exchange); + Outcome outcome = (statusCode != null) ? Outcome.forStatus(statusCode) : Outcome.UNKNOWN; return outcome.asTag(); } + private static Integer extractStatusCode(ServerWebExchange exchange) { + ServerHttpResponse response = exchange.getResponse(); + if (response instanceof AbstractServerHttpResponse) { + return ((AbstractServerHttpResponse) response).getStatusCodeValue(); + } + HttpStatus status = response.getStatusCode(); + return (status != null) ? status.value() : null; + } + } diff --git a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/web/reactive/server/WebFluxTagsTests.java b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/web/reactive/server/WebFluxTagsTests.java index 53f8742076..b713485191 100644 --- a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/web/reactive/server/WebFluxTagsTests.java +++ b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/web/reactive/server/WebFluxTagsTests.java @@ -152,4 +152,18 @@ class WebFluxTagsTests { assertThat(tag.getValue()).isEqualTo("SERVER_ERROR"); } + @Test + void outcomeTagIsClientErrorWhenResponseIsNonStandardInClientSeries() { + this.exchange.getResponse().setStatusCodeValue(490); + Tag tag = WebFluxTags.outcome(this.exchange); + assertThat(tag.getValue()).isEqualTo("CLIENT_ERROR"); + } + + @Test + void outcomeTagIsUnknownWhenResponseStatusIsInUnknownSeries() { + this.exchange.getResponse().setStatusCodeValue(701); + Tag tag = WebFluxTags.outcome(this.exchange); + assertThat(tag.getValue()).isEqualTo("UNKNOWN"); + } + }