Skip to content

Commit 95e26ff

Browse files
dreis2211bclozel
authored andcommitted
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
1 parent 0e13fd0 commit 95e26ff

File tree

2 files changed

+38
-5
lines changed
  • spring-boot-project/spring-boot-actuator/src

2 files changed

+38
-5
lines changed

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -40,6 +40,8 @@ public final class WebFluxTags {
4040

4141
private static final Tag URI_ROOT = Tag.of("uri", "root");
4242

43+
private static final Tag URI_UNKNOWN = Tag.of("uri", "UNKNOWN");
44+
4345
private static final Tag EXCEPTION_NONE = Tag.of("exception", "None");
4446

4547
private WebFluxTags() {
@@ -73,7 +75,10 @@ public static Tag status(ServerWebExchange exchange) {
7375

7476
/**
7577
* Creates a {@code uri} tag based on the URI of the given {@code exchange}. Uses the
76-
* {@link HandlerMapping#BEST_MATCHING_PATTERN_ATTRIBUTE} best matching pattern.
78+
* {@link HandlerMapping#BEST_MATCHING_PATTERN_ATTRIBUTE} best matching pattern if
79+
* available. Falling back to {@code REDIRECTION} for 3xx responses, {@code NOT_FOUND}
80+
* for 404 responses, {@code root} for requests with no path info, and {@code UNKNOWN}
81+
* for all other requests.
7782
* @param exchange the exchange
7883
* @return the uri tag derived from the exchange
7984
*/
@@ -92,11 +97,17 @@ public static Tag uri(ServerWebExchange exchange) {
9297
return URI_NOT_FOUND;
9398
}
9499
}
95-
String path = exchange.getRequest().getPath().value();
100+
String path = getPathInfo(exchange);
96101
if (path.isEmpty()) {
97102
return URI_ROOT;
98103
}
99-
return Tag.of("uri", path);
104+
return URI_UNKNOWN;
105+
}
106+
107+
private static String getPathInfo(ServerWebExchange exchange) {
108+
String path = exchange.getRequest().getPath().value();
109+
String uri = StringUtils.hasText(path) ? path : "/";
110+
return uri.replaceAll("//+", "/").replaceAll("/$", "");
100111
}
101112

102113
/**

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -78,6 +78,28 @@ public void uriTagToleratesCustomResponseStatus() {
7878
assertThat(tag.getValue()).isEqualTo("root");
7979
}
8080

81+
@Test
82+
public void uriTagValueIsRootWhenRequestHasNoPatternOrPathInfo() {
83+
Tag tag = WebFluxTags.uri(this.exchange);
84+
assertThat(tag.getValue()).isEqualTo("root");
85+
}
86+
87+
@Test
88+
public void uriTagValueIsRootWhenRequestHasNoPatternAndSlashPathInfo() {
89+
MockServerHttpRequest request = MockServerHttpRequest.get("/").build();
90+
ServerWebExchange exchange = MockServerWebExchange.from(request);
91+
Tag tag = WebFluxTags.uri(exchange);
92+
assertThat(tag.getValue()).isEqualTo("root");
93+
}
94+
95+
@Test
96+
public void uriTagValueIsUnknownWhenRequestHasNoPatternAndNonRootPathInfo() {
97+
MockServerHttpRequest request = MockServerHttpRequest.get("/example").build();
98+
ServerWebExchange exchange = MockServerWebExchange.from(request);
99+
Tag tag = WebFluxTags.uri(exchange);
100+
assertThat(tag.getValue()).isEqualTo("UNKNOWN");
101+
}
102+
81103
@Test
82104
public void methodTagToleratesNonStandardHttpMethods() {
83105
ServerWebExchange exchange = mock(ServerWebExchange.class);

0 commit comments

Comments
 (0)