From b22bb7ea045eebee30441850c0f5d45f248b43c0 Mon Sep 17 00:00:00 2001 From: Peter Paul Bakker Date: Fri, 13 Nov 2020 08:57:54 +0100 Subject: [PATCH 1/2] Avoid unnecessary pattern compilation in WebFluxTags See gh-24147 --- .../metrics/web/reactive/server/WebFluxTags.java | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 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 12fba3e48132..35c4a7935f7c 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 @@ -49,7 +49,7 @@ public final class WebFluxTags { private static final Tag EXCEPTION_NONE = Tag.of("exception", "None"); - private static final Pattern TRAILING_SLASH_PATTERN = Pattern.compile("/$"); + private static final Pattern FORWARD_SLASHES_PATTERN = Pattern.compile("//+"); private WebFluxTags() { } @@ -108,7 +108,7 @@ public static Tag uri(ServerWebExchange exchange, boolean ignoreTrailingSlash) { if (pathPattern != null) { String patternString = pathPattern.getPatternString(); if (ignoreTrailingSlash && patternString.length() > 1) { - patternString = TRAILING_SLASH_PATTERN.matcher(patternString).replaceAll(""); + patternString = removeTrailingSlash(patternString); } if (patternString.isEmpty()) { return URI_ROOT; @@ -131,10 +131,18 @@ public static Tag uri(ServerWebExchange exchange, boolean ignoreTrailingSlash) { return URI_UNKNOWN; } + private static String removeTrailingSlash(String text) { + if (!StringUtils.hasLength(text)) { + return text; + } + return text.endsWith("/") ? text.substring(0, text.length() - 1) : text; + } + private static String getPathInfo(ServerWebExchange exchange) { String path = exchange.getRequest().getPath().value(); String uri = StringUtils.hasText(path) ? path : "/"; - return uri.replaceAll("//+", "/").replaceAll("/$", ""); + String singleSlashes = FORWARD_SLASHES_PATTERN.matcher(uri).replaceAll("/"); + return removeTrailingSlash(singleSlashes); } /** From d3440880c4fc70a40b4e43391a44ce74f677db3d Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Mon, 16 Nov 2020 07:30:08 +0100 Subject: [PATCH 2/2] Polish "Avoid unnecessary pattern compilation in WebFluxTags" See gh-24147 --- .../metrics/web/reactive/server/WebFluxTags.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 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 35c4a7935f7c..f0d4daab27c6 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 @@ -131,13 +131,6 @@ public static Tag uri(ServerWebExchange exchange, boolean ignoreTrailingSlash) { return URI_UNKNOWN; } - private static String removeTrailingSlash(String text) { - if (!StringUtils.hasLength(text)) { - return text; - } - return text.endsWith("/") ? text.substring(0, text.length() - 1) : text; - } - private static String getPathInfo(ServerWebExchange exchange) { String path = exchange.getRequest().getPath().value(); String uri = StringUtils.hasText(path) ? path : "/"; @@ -145,6 +138,13 @@ private static String getPathInfo(ServerWebExchange exchange) { return removeTrailingSlash(singleSlashes); } + private static String removeTrailingSlash(String text) { + if (!StringUtils.hasLength(text)) { + return text; + } + return text.endsWith("/") ? text.substring(0, text.length() - 1) : text; + } + /** * Creates an {@code exception} tag based on the {@link Class#getSimpleName() simple * name} of the class of the given {@code exception}.