Ignore trailing slash when recording Web metrics

Fixes gh-18207
This commit is contained in:
Madhura Bhave
2020-01-13 13:31:16 -08:00
parent 8edffc8ed7
commit e60194c7d5
11 changed files with 139 additions and 10 deletions

View File

@@ -37,6 +37,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* Tests for {@link MetricsWebFilter}
*
* @author Brian Clozel
* @author Madhura Bhave
*/
class MetricsWebFilterTests {
@@ -50,7 +51,7 @@ class MetricsWebFilterTests {
void setup() {
MockClock clock = new MockClock();
this.registry = new SimpleMeterRegistry(SimpleConfig.DEFAULT, clock);
this.webFilter = new MetricsWebFilter(this.registry, new DefaultWebFluxTagsProvider(), REQUEST_METRICS_NAME,
this.webFilter = new MetricsWebFilter(this.registry, new DefaultWebFluxTagsProvider(true), REQUEST_METRICS_NAME,
AutoTimer.ENABLED);
}
@@ -102,6 +103,19 @@ class MetricsWebFilterTests {
assertMetricsContainsTag("status", "500");
}
@Test
void trailingSlashShouldNotRecordDuplicateMetrics() {
MockServerWebExchange exchange1 = createExchange("/projects/spring-boot", "/projects/{project}");
MockServerWebExchange exchange2 = createExchange("/projects/spring-boot", "/projects/{project}/");
this.webFilter.filter(exchange1, (serverWebExchange) -> exchange1.getResponse().setComplete())
.block(Duration.ofSeconds(30));
this.webFilter.filter(exchange2, (serverWebExchange) -> exchange2.getResponse().setComplete())
.block(Duration.ofSeconds(30));
assertThat(this.registry.get(REQUEST_METRICS_NAME).tag("uri", "/projects/{project}").timer().count())
.isEqualTo(2);
assertThat(this.registry.get(REQUEST_METRICS_NAME).tag("status", "200").timer().count()).isEqualTo(2);
}
private MockServerWebExchange createExchange(String path, String pathPattern) {
PathPatternParser parser = new PathPatternParser();
MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get(path).build());

View File

@@ -289,6 +289,14 @@ class WebMvcMetricsFilterTests {
assertThat(this.prometheusRegistry.scrape()).contains("le=\"30.0\"");
}
@Test
void trailingSlashShouldNotRecordDuplicateMetrics() throws Exception {
this.mvc.perform(get("/api/c1/simple/10")).andExpect(status().isOk());
this.mvc.perform(get("/api/c1/simple/10/")).andExpect(status().isOk());
assertThat(this.registry.get("http.server.requests").tags("status", "200", "uri", "/api/c1/simple/{id}").timer()
.count()).isEqualTo(2);
}
@Target({ ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Timed(percentiles = 0.95)
@@ -356,7 +364,7 @@ class WebMvcMetricsFilterTests {
@Bean
WebMvcMetricsFilter webMetricsFilter(MeterRegistry registry, WebApplicationContext ctx) {
return new WebMvcMetricsFilter(registry, new DefaultWebMvcTagsProvider(), "http.server.requests",
return new WebMvcMetricsFilter(registry, new DefaultWebMvcTagsProvider(true), "http.server.requests",
AutoTimer.ENABLED);
}
@@ -380,6 +388,11 @@ class WebMvcMetricsFilterTests {
return id.toString();
}
@GetMapping("/simple/{id}")
String simpleMapping(@PathVariable Long id) {
return id.toString();
}
@Timed
@Timed(value = "my.long.request", extraTags = { "region", "test" }, longTask = true)
@GetMapping("/callable/{id}")