Skip to content

Commit 97877c2

Browse files
committed
Merge pull request #12894 from jkschneider:webmvc-tags-perf
* pr/12894: Polish "Less object instantiation in WebMvcTags" Less object instantiation in WebMvcTags
2 parents 0bc7bef + 2b98b11 commit 97877c2

File tree

1 file changed

+15
-7
lines changed
  • spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web/servlet

1 file changed

+15
-7
lines changed

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web/servlet/WebMvcTags.java

+15-7
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ public final class WebMvcTags {
4040

4141
private static final Tag URI_REDIRECTION = Tag.of("uri", "REDIRECTION");
4242

43+
private static final Tag URI_UNKNOWN = Tag.of("uri", "UNKNOWN");
44+
45+
private static final Tag EXCEPTION_NONE = Tag.of("exception", "None");
46+
47+
private static final Tag STATUS_UNKNOWN = Tag.of("status", "UNKNOWN");
48+
49+
private static final Tag METHOD_UNKNOWN = Tag.of("method", "UNKNOWN");
50+
4351
private WebMvcTags() {
4452
}
4553

@@ -50,8 +58,7 @@ private WebMvcTags() {
5058
* @return the method tag whose value is a capitalized method (e.g. GET).
5159
*/
5260
public static Tag method(HttpServletRequest request) {
53-
return (request == null ? Tag.of("method", "UNKNOWN")
54-
: Tag.of("method", request.getMethod()));
61+
return (request == null ? METHOD_UNKNOWN : Tag.of("method", request.getMethod()));
5562
}
5663

5764
/**
@@ -60,8 +67,8 @@ public static Tag method(HttpServletRequest request) {
6067
* @return the status tag derived from the status of the response
6168
*/
6269
public static Tag status(HttpServletResponse response) {
63-
return (response == null ? Tag.of("status", "UNKNOWN")
64-
: Tag.of("status", ((Integer) response.getStatus()).toString()));
70+
return (response == null ? STATUS_UNKNOWN :
71+
Tag.of("status", Integer.toString(response.getStatus())));
6572
}
6673

6774
/**
@@ -91,7 +98,7 @@ else if (response != null) {
9198
String pathInfo = getPathInfo(request);
9299
return Tag.of("uri", pathInfo.isEmpty() ? "root" : pathInfo);
93100
}
94-
return Tag.of("uri", "UNKNOWN");
101+
return URI_UNKNOWN;
95102
}
96103

97104
private static HttpStatus extractStatus(HttpServletResponse response) {
@@ -121,8 +128,9 @@ private static String getPathInfo(HttpServletRequest request) {
121128
* @return the exception tag derived from the exception
122129
*/
123130
public static Tag exception(Throwable exception) {
124-
return Tag.of("exception",
125-
(exception == null ? "None" : exception.getClass().getSimpleName()));
131+
return (exception != null
132+
? Tag.of("exception", exception.getClass().getSimpleName())
133+
: EXCEPTION_NONE);
126134
}
127135

128136
}

0 commit comments

Comments
 (0)