Skip to content

Commit d96ff16

Browse files
committed
Merge pull request #24147 from stokpop
* pr/24147: Polish "Avoid unnecessary pattern compilation in WebFluxTags" Avoid unnecessary pattern compilation in WebFluxTags Closes gh-24147
2 parents 4961599 + d344088 commit d96ff16

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;
@@ -134,7 +134,15 @@ public static Tag uri(ServerWebExchange exchange, boolean ignoreTrailingSlash) {
134134
private static String getPathInfo(ServerWebExchange exchange) {
135135
String path = exchange.getRequest().getPath().value();
136136
String uri = StringUtils.hasText(path) ? path : "/";
137-
return uri.replaceAll("//+", "/").replaceAll("/$", "");
137+
String singleSlashes = FORWARD_SLASHES_PATTERN.matcher(uri).replaceAll("/");
138+
return removeTrailingSlash(singleSlashes);
139+
}
140+
141+
private static String removeTrailingSlash(String text) {
142+
if (!StringUtils.hasLength(text)) {
143+
return text;
144+
}
145+
return text.endsWith("/") ? text.substring(0, text.length() - 1) : text;
138146
}
139147

140148
/**

0 commit comments

Comments
 (0)