Skip to content

Anonymous exception leads to empty exception tags in WebMvc/WebFlux metrics #13817

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ public static Tag uri(ServerWebExchange exchange) {
*/
public static Tag exception(Throwable exception) {
if (exception != null) {
return Tag.of("exception", exception.getClass().getSimpleName());
String simpleName = exception.getClass().getSimpleName();
return Tag.of("exception", simpleName.isEmpty() ? exception.getClass().getName() : simpleName);
}
return EXCEPTION_NONE;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,11 @@ private static String getPathInfo(HttpServletRequest request) {
* @return the exception tag derived from the exception
*/
public static Tag exception(Throwable exception) {
return (exception != null
? Tag.of("exception", exception.getClass().getSimpleName())
: EXCEPTION_NONE);
if (exception != null) {
String simpleName = exception.getClass().getSimpleName();
return Tag.of("exception", simpleName.isEmpty() ? exception.getClass().getName() : simpleName);
}
return EXCEPTION_NONE;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,26 @@ public void filterAddsTagsToRegistryForExceptions() {
}).block();
assertMetricsContainsTag("uri", "/projects/{project}");
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,18 @@ public void unhandledError() {
.tags("exception", "RuntimeException").timer().count()).isEqualTo(1L);
}

@Test
public void anonymousError() throws Exception {
try {
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
public void asyncCallableRequest() throws Exception {
AtomicReference<MvcResult> result = new AtomicReference<>();
Expand Down Expand Up @@ -440,6 +452,12 @@ public String alwaysThrowsException(@PathVariable Long 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
@GetMapping("/unhandledError/{id}")
public String alwaysThrowsUnhandledException(@PathVariable Long id) {
Expand Down