Commit ebffe6e4 authored by Stephane Nicoll's avatar Stephane Nicoll

Merge pull request #13817 from jkschneider:metrics-anon-exception

* pr/13817:
  Polish "Make sure exception tag values are not empty in web metrics"
  Make sure exception tag values are not empty in web metrics
parents 66156cc2 592754d8
...@@ -19,6 +19,7 @@ package org.springframework.boot.actuate.metrics.web.reactive.server; ...@@ -19,6 +19,7 @@ package org.springframework.boot.actuate.metrics.web.reactive.server;
import io.micrometer.core.instrument.Tag; import io.micrometer.core.instrument.Tag;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.util.StringUtils;
import org.springframework.web.reactive.HandlerMapping; import org.springframework.web.reactive.HandlerMapping;
import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.util.pattern.PathPattern; import org.springframework.web.util.pattern.PathPattern;
...@@ -109,7 +110,9 @@ public final class WebFluxTags { ...@@ -109,7 +110,9 @@ public final class WebFluxTags {
*/ */
public static Tag exception(Throwable exception) { public static Tag exception(Throwable exception) {
if (exception != null) { if (exception != null) {
return Tag.of("exception", exception.getClass().getSimpleName()); String simpleName = exception.getClass().getSimpleName();
return Tag.of("exception", StringUtils.hasText(simpleName) ? simpleName
: exception.getClass().getName());
} }
return EXCEPTION_NONE; return EXCEPTION_NONE;
} }
......
...@@ -134,9 +134,12 @@ public final class WebMvcTags { ...@@ -134,9 +134,12 @@ public final class WebMvcTags {
* @return the exception tag derived from the exception * @return the exception tag derived from the exception
*/ */
public static Tag exception(Throwable exception) { public static Tag exception(Throwable exception) {
return (exception != null if (exception != null) {
? Tag.of("exception", exception.getClass().getSimpleName()) String simpleName = exception.getClass().getSimpleName();
: EXCEPTION_NONE); return Tag.of("exception", StringUtils.hasText(simpleName) ? simpleName
: exception.getClass().getName());
}
return EXCEPTION_NONE;
} }
} }
...@@ -77,6 +77,24 @@ public class MetricsWebFilterTests { ...@@ -77,6 +77,24 @@ public class MetricsWebFilterTests {
}).block(); }).block();
assertMetricsContainsTag("uri", "/projects/{project}"); assertMetricsContainsTag("uri", "/projects/{project}");
assertMetricsContainsTag("status", "500"); assertMetricsContainsTag("status", "500");
assertMetricsContainsTag("exception", "IllegalStateException");
}
@Test
public void filterAddsNonEmptyTagsToRegistryForAnonymousExceptions() {
final Exception anonymous = new Exception("test error") {
};
MockServerWebExchange exchange = createExchange("/projects/spring-boot",
"/projects/{project}");
this.webFilter.filter(exchange, (serverWebExchange) -> Mono.error(anonymous))
.onErrorResume((t) -> {
exchange.getResponse().setStatusCodeValue(500);
return exchange.getResponse().setComplete();
}).block();
assertMetricsContainsTag("uri", "/projects/{project}");
assertMetricsContainsTag("status", "500");
assertMetricsContainsTag("exception", anonymous.getClass().getName());
} }
@Test @Test
......
...@@ -188,6 +188,18 @@ public class WebMvcMetricsFilterTests { ...@@ -188,6 +188,18 @@ public class WebMvcMetricsFilterTests {
.tags("exception", "RuntimeException").timer().count()).isEqualTo(1L); .tags("exception", "RuntimeException").timer().count()).isEqualTo(1L);
} }
@Test
public void anonymousError() {
try {
this.mvc.perform(get("/api/c1/anonymousError/10"));
}
catch (Throwable ignore) {
}
assertThat(this.registry.get("http.server.requests")
.tag("uri", "/api/c1/anonymousError/{id}").timer().getId()
.getTag("exception")).endsWith("$1");
}
@Test @Test
public void asyncCallableRequest() throws Exception { public void asyncCallableRequest() throws Exception {
AtomicReference<MvcResult> result = new AtomicReference<>(); AtomicReference<MvcResult> result = new AtomicReference<>();
...@@ -440,6 +452,14 @@ public class WebMvcMetricsFilterTests { ...@@ -440,6 +452,14 @@ public class WebMvcMetricsFilterTests {
throw new IllegalStateException("Boom on " + id + "!"); throw new IllegalStateException("Boom on " + id + "!");
} }
@Timed
@GetMapping("/anonymousError/{id}")
public String alwaysThrowsAnonymousException(@PathVariable Long id)
throws Exception {
throw new Exception("this exception won't have a simple class name") {
};
}
@Timed @Timed
@GetMapping("/unhandledError/{id}") @GetMapping("/unhandledError/{id}")
public String alwaysThrowsUnhandledException(@PathVariable Long id) { public String alwaysThrowsUnhandledException(@PathVariable Long id) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment