From d6761476803e519ecb74e2c7e1fd78ea2e165250 Mon Sep 17 00:00:00 2001 From: Jon Schneider Date: Tue, 17 Apr 2018 21:41:49 -0500 Subject: [PATCH 1/2] Less object instantiation in WebMvcTags See gh-12894 --- .../metrics/web/servlet/WebMvcTags.java | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web/servlet/WebMvcTags.java b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web/servlet/WebMvcTags.java index 25db2ba3f80..dad8370a637 100644 --- a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web/servlet/WebMvcTags.java +++ b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web/servlet/WebMvcTags.java @@ -40,6 +40,14 @@ public final class WebMvcTags { private static final Tag URI_REDIRECTION = Tag.of("uri", "REDIRECTION"); + private static final Tag URI_UNKNOWN = Tag.of("uri", "UNKNOWN"); + + private static final Tag EXCEPTION_NONE = Tag.of("exception", "None"); + + private static final Tag STATUS_UNKNOWN = Tag.of("status", "UNKNOWN"); + + private static final Tag METHOD_UNKNOWN = Tag.of("method", "UNKNOWN"); + private WebMvcTags() { } @@ -50,8 +58,7 @@ public final class WebMvcTags { * @return the method tag whose value is a capitalized method (e.g. GET). */ public static Tag method(HttpServletRequest request) { - return (request == null ? Tag.of("method", "UNKNOWN") - : Tag.of("method", request.getMethod())); + return request == null ? METHOD_UNKNOWN : Tag.of("method", request.getMethod()); } /** @@ -60,8 +67,8 @@ public final class WebMvcTags { * @return the status tag derived from the status of the response */ public static Tag status(HttpServletResponse response) { - return (response == null ? Tag.of("status", "UNKNOWN") - : Tag.of("status", ((Integer) response.getStatus()).toString())); + return response == null ? STATUS_UNKNOWN : + Tag.of("status", Integer.toString(response.getStatus())); } /** @@ -91,7 +98,7 @@ public final class WebMvcTags { String pathInfo = getPathInfo(request); return Tag.of("uri", pathInfo.isEmpty() ? "root" : pathInfo); } - return Tag.of("uri", "UNKNOWN"); + return URI_UNKNOWN; } private static HttpStatus extractStatus(HttpServletResponse response) { @@ -121,8 +128,8 @@ public final class WebMvcTags { * @return the exception tag derived from the exception */ public static Tag exception(Throwable exception) { - return Tag.of("exception", - (exception == null ? "None" : exception.getClass().getSimpleName())); + return exception == null ? EXCEPTION_NONE : + Tag.of("exception", exception.getClass().getSimpleName()); } } From 2b98b11c12122dafd1db2dccb7e5766409cfd256 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Wed, 18 Apr 2018 09:38:38 +0200 Subject: [PATCH 2/2] Polish "Less object instantiation in WebMvcTags" Closes gh-12894 --- .../boot/actuate/metrics/web/servlet/WebMvcTags.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web/servlet/WebMvcTags.java b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web/servlet/WebMvcTags.java index dad8370a637..3c79fe9d737 100644 --- a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web/servlet/WebMvcTags.java +++ b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web/servlet/WebMvcTags.java @@ -58,7 +58,7 @@ public final class WebMvcTags { * @return the method tag whose value is a capitalized method (e.g. GET). */ public static Tag method(HttpServletRequest request) { - return request == null ? METHOD_UNKNOWN : Tag.of("method", request.getMethod()); + return (request == null ? METHOD_UNKNOWN : Tag.of("method", request.getMethod())); } /** @@ -67,8 +67,8 @@ public final class WebMvcTags { * @return the status tag derived from the status of the response */ public static Tag status(HttpServletResponse response) { - return response == null ? STATUS_UNKNOWN : - Tag.of("status", Integer.toString(response.getStatus())); + return (response == null ? STATUS_UNKNOWN : + Tag.of("status", Integer.toString(response.getStatus()))); } /** @@ -128,8 +128,9 @@ public final class WebMvcTags { * @return the exception tag derived from the exception */ public static Tag exception(Throwable exception) { - return exception == null ? EXCEPTION_NONE : - Tag.of("exception", exception.getClass().getSimpleName()); + return (exception != null + ? Tag.of("exception", exception.getClass().getSimpleName()) + : EXCEPTION_NONE); } }