From 95e26ffcb9f6abd991655e1ae90013f4d4b1f330 Mon Sep 17 00:00:00 2001 From: dreis2211 Date: Tue, 8 Jan 2019 21:30:09 +0100 Subject: [PATCH] Avoid uri tag explosion when use of path variable is undetected This commit aligns the Spring WebFlux instrumentation on Spring MVC since gh-12447. From now on, if the best matching path pattern is not found, the recorded uri tag will be "UNKNOWN". Note that for WebFlux.fn, the pattern information is properly recorded as of SPR-17395. Closes gh-15609 --- .../web/reactive/server/WebFluxTags.java | 19 +++++++++++---- .../web/reactive/server/WebFluxTagsTests.java | 24 ++++++++++++++++++- 2 files changed, 38 insertions(+), 5 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 6393ff1488..8c92d74133 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 @@ -1,5 +1,5 @@ /* - * Copyright 2012-2018 the original author or authors. + * Copyright 2012-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -40,6 +40,8 @@ public final class WebFluxTags { private static final Tag URI_ROOT = Tag.of("uri", "root"); + private static final Tag URI_UNKNOWN = Tag.of("uri", "UNKNOWN"); + private static final Tag EXCEPTION_NONE = Tag.of("exception", "None"); private WebFluxTags() { @@ -73,7 +75,10 @@ public final class WebFluxTags { /** * Creates a {@code uri} tag based on the URI of the given {@code exchange}. Uses the - * {@link HandlerMapping#BEST_MATCHING_PATTERN_ATTRIBUTE} best matching pattern. + * {@link HandlerMapping#BEST_MATCHING_PATTERN_ATTRIBUTE} best matching pattern if + * available. Falling back to {@code REDIRECTION} for 3xx responses, {@code NOT_FOUND} + * for 404 responses, {@code root} for requests with no path info, and {@code UNKNOWN} + * for all other requests. * @param exchange the exchange * @return the uri tag derived from the exchange */ @@ -92,11 +97,17 @@ public final class WebFluxTags { return URI_NOT_FOUND; } } - String path = exchange.getRequest().getPath().value(); + String path = getPathInfo(exchange); if (path.isEmpty()) { return URI_ROOT; } - return Tag.of("uri", path); + return URI_UNKNOWN; + } + + private static String getPathInfo(ServerWebExchange exchange) { + String path = exchange.getRequest().getPath().value(); + String uri = StringUtils.hasText(path) ? path : "/"; + return uri.replaceAll("//+", "/").replaceAll("/$", ""); } /** diff --git a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/web/reactive/server/WebFluxTagsTests.java b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/web/reactive/server/WebFluxTagsTests.java index a25a30da1d..88e7aa21e1 100644 --- a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/web/reactive/server/WebFluxTagsTests.java +++ b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/web/reactive/server/WebFluxTagsTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2018 the original author or authors. + * Copyright 2012-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -78,6 +78,28 @@ public class WebFluxTagsTests { assertThat(tag.getValue()).isEqualTo("root"); } + @Test + public void uriTagValueIsRootWhenRequestHasNoPatternOrPathInfo() { + Tag tag = WebFluxTags.uri(this.exchange); + assertThat(tag.getValue()).isEqualTo("root"); + } + + @Test + public void uriTagValueIsRootWhenRequestHasNoPatternAndSlashPathInfo() { + MockServerHttpRequest request = MockServerHttpRequest.get("/").build(); + ServerWebExchange exchange = MockServerWebExchange.from(request); + Tag tag = WebFluxTags.uri(exchange); + assertThat(tag.getValue()).isEqualTo("root"); + } + + @Test + public void uriTagValueIsUnknownWhenRequestHasNoPatternAndNonRootPathInfo() { + MockServerHttpRequest request = MockServerHttpRequest.get("/example").build(); + ServerWebExchange exchange = MockServerWebExchange.from(request); + Tag tag = WebFluxTags.uri(exchange); + assertThat(tag.getValue()).isEqualTo("UNKNOWN"); + } + @Test public void methodTagToleratesNonStandardHttpMethods() { ServerWebExchange exchange = mock(ServerWebExchange.class);