From 42b3e4c6c86a04ef17ea01ee4521cbd97ce6cb51 Mon Sep 17 00:00:00 2001 From: Tim Ysewyn Date: Thu, 25 Apr 2019 16:32:42 +0200 Subject: [PATCH] Updated pattern to ignore /actuator (#1342) Fixes #1338 --- .../web/TraceWebAutoConfiguration.java | 26 +++++++++++++++---- .../web/SkipPatternProviderConfigTest.java | 16 ++++++------ 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/web/TraceWebAutoConfiguration.java b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/web/TraceWebAutoConfiguration.java index ab5708f1d..248f428bb 100644 --- a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/web/TraceWebAutoConfiguration.java +++ b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/web/TraceWebAutoConfiguration.java @@ -51,6 +51,7 @@ import org.springframework.util.StringUtils; * based web application. * * @author Marcin Grzejszczak + * @author Tim Ysewyn * @since 1.0.0 */ @Configuration @@ -118,12 +119,12 @@ public class TraceWebAutoConfiguration { return Optional.empty(); } + String basePath = webEndpointProperties.getBasePath(); String pattern = endpoints.stream().map(PathMappedEndpoint::getRootPath) - .map(path -> path + "|" + path + "/.*").collect( - Collectors.joining("|", - getPathPrefix(contextPath, - webEndpointProperties.getBasePath()) + "/(", - ")")); + .map(path -> path + "|" + path + "/.*") + .collect(Collectors.joining("|", + getPathPrefix(contextPath, basePath), + getPathSuffix(contextPath, basePath))); if (StringUtils.hasText(pattern)) { return Optional.of(Pattern.compile(pattern)); } @@ -138,6 +139,21 @@ public class TraceWebAutoConfiguration { if (!actuatorBasePath.equals("/")) { result += actuatorBasePath; } + boolean ignoreBase = StringUtils.hasText(result) && !result.equals("/"); + String suffix = "/("; + if (ignoreBase) { + suffix = "(/|" + suffix; + } + return result + suffix; + } + + private static String getPathSuffix(String contextPath, String actuatorBasePath) { + String result = ")"; + if (StringUtils.hasText(contextPath) || + (StringUtils.hasText(actuatorBasePath) + && !"/".equals(actuatorBasePath))) { + result += ")?"; + } return result; } diff --git a/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/web/SkipPatternProviderConfigTest.java b/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/web/SkipPatternProviderConfigTest.java index f31b244c1..0ec2a0eb1 100644 --- a/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/web/SkipPatternProviderConfigTest.java +++ b/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/web/SkipPatternProviderConfigTest.java @@ -113,7 +113,7 @@ public class SkipPatternProviderConfigTest { then(pattern).isNotEmpty(); then(pattern.get().pattern()) - .isEqualTo("/actuator/(info|info/.*|health|health/.*)"); + .isEqualTo("/actuator(/|/(info|info/.*|health|health/.*))?"); } @Test @@ -136,7 +136,7 @@ public class SkipPatternProviderConfigTest { then(pattern).isNotEmpty(); then(pattern.get().pattern()) - .isEqualTo("foo/actuator/(info|info/.*|health|health/.*)"); + .isEqualTo("foo/actuator(/|/(info|info/.*|health|health/.*))?"); } @Test @@ -181,7 +181,7 @@ public class SkipPatternProviderConfigTest { .skipPattern(); then(pattern).isNotEmpty(); - then(pattern.get().pattern()).isEqualTo("foo/(info|info/.*|health|health/.*)"); + then(pattern.get().pattern()).isEqualTo("foo(/|/(info|info/.*|health|health/.*))?"); } @Test @@ -214,7 +214,7 @@ public class SkipPatternProviderConfigTest { then(patternSamePort).isNotEmpty(); then(patternSamePort.get().pattern()) - .isEqualTo("foo/(info|info/.*|health|health/.*)"); + .isEqualTo("foo(/|/(info|info/.*|health|health/.*))?"); } @Test @@ -238,7 +238,7 @@ public class SkipPatternProviderConfigTest { then(patternDifferentPort).isNotEmpty(); then(patternDifferentPort.get().pattern()) - .isEqualTo("/mgt/(info|info/.*|health|health/.*)"); + .isEqualTo("/mgt(/|/(info|info/.*|health|health/.*))?"); Optional patternSamePort = new TraceWebAutoConfiguration.ActuatorSkipPatternProviderConfig() .skipPatternForActuatorEndpointsSamePort(properties, @@ -247,7 +247,7 @@ public class SkipPatternProviderConfigTest { then(patternSamePort).isNotEmpty(); then(patternSamePort.get().pattern()) - .isEqualTo("foo/mgt/(info|info/.*|health|health/.*)"); + .isEqualTo("foo/mgt(/|/(info|info/.*|health|health/.*))?"); } @Test @@ -271,7 +271,7 @@ public class SkipPatternProviderConfigTest { then(patternDifferentPort).isNotEmpty(); then(patternDifferentPort.get().pattern()) - .isEqualTo("/actuator/(info|info/.*|health|health/.*)"); + .isEqualTo("/actuator(/|/(info|info/.*|health|health/.*))?"); Optional patternSamePort = new TraceWebAutoConfiguration.ActuatorSkipPatternProviderConfig() .skipPatternForActuatorEndpointsSamePort(properties, @@ -280,7 +280,7 @@ public class SkipPatternProviderConfigTest { then(patternSamePort).isNotEmpty(); then(patternSamePort.get().pattern()) - .isEqualTo("/foo/actuator/(info|info/.*|health|health/.*)"); + .isEqualTo("/foo/actuator(/|/(info|info/.*|health|health/.*))?"); } @Test