Skip to content

Commit b22bb7e

Browse files
stokpopsnicoll
authored andcommitted
Avoid unnecessary pattern compilation in WebFluxTags
See gh-24147
1 parent 4961599 commit b22bb7e

File tree

1 file changed

+11
-3
lines changed
  • spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web/reactive/server

1 file changed

+11
-3
lines changed

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web/reactive/server/WebFluxTags.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public final class WebFluxTags {
4949

5050
private static final Tag EXCEPTION_NONE = Tag.of("exception", "None");
5151

52-
private static final Pattern TRAILING_SLASH_PATTERN = Pattern.compile("/$");
52+
private static final Pattern FORWARD_SLASHES_PATTERN = Pattern.compile("//+");
5353

5454
private WebFluxTags() {
5555
}
@@ -108,7 +108,7 @@ public static Tag uri(ServerWebExchange exchange, boolean ignoreTrailingSlash) {
108108
if (pathPattern != null) {
109109
String patternString = pathPattern.getPatternString();
110110
if (ignoreTrailingSlash && patternString.length() > 1) {
111-
patternString = TRAILING_SLASH_PATTERN.matcher(patternString).replaceAll("");
111+
patternString = removeTrailingSlash(patternString);
112112
}
113113
if (patternString.isEmpty()) {
114114
return URI_ROOT;
@@ -131,10 +131,18 @@ public static Tag uri(ServerWebExchange exchange, boolean ignoreTrailingSlash) {
131131
return URI_UNKNOWN;
132132
}
133133

134+
private static String removeTrailingSlash(String text) {
135+
if (!StringUtils.hasLength(text)) {
136+
return text;
137+
}
138+
return text.endsWith("/") ? text.substring(0, text.length() - 1) : text;
139+
}
140+
134141
private static String getPathInfo(ServerWebExchange exchange) {
135142
String path = exchange.getRequest().getPath().value();
136143
String uri = StringUtils.hasText(path) ? path : "/";
137-
return uri.replaceAll("//+", "/").replaceAll("/$", "");
144+
String singleSlashes = FORWARD_SLASHES_PATTERN.matcher(uri).replaceAll("/");
145+
return removeTrailingSlash(singleSlashes);
138146
}
139147

140148
/**

0 commit comments

Comments
 (0)