Polish "Make sure exception tag values are not empty in web metrics"

Closes gh-13187
pull/14003/head
Stephane Nicoll 6 years ago
parent ee37dc1c31
commit 592754d806

@ -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;
@ -110,7 +111,8 @@ public final class WebFluxTags {
public static Tag exception(Throwable exception) { public static Tag exception(Throwable exception) {
if (exception != null) { if (exception != null) {
String simpleName = exception.getClass().getSimpleName(); String simpleName = exception.getClass().getSimpleName();
return Tag.of("exception", simpleName.isEmpty() ? exception.getClass().getName() : simpleName); return Tag.of("exception", StringUtils.hasText(simpleName) ? simpleName
: exception.getClass().getName());
} }
return EXCEPTION_NONE; return EXCEPTION_NONE;
} }

@ -136,7 +136,8 @@ public final class WebMvcTags {
public static Tag exception(Throwable exception) { public static Tag exception(Throwable exception) {
if (exception != null) { if (exception != null) {
String simpleName = exception.getClass().getSimpleName(); String simpleName = exception.getClass().getSimpleName();
return Tag.of("exception", simpleName.isEmpty() ? exception.getClass().getName() : simpleName); return Tag.of("exception", StringUtils.hasText(simpleName) ? simpleName
: exception.getClass().getName());
} }
return EXCEPTION_NONE; return EXCEPTION_NONE;
} }

@ -82,14 +82,12 @@ public class MetricsWebFilterTests {
@Test @Test
public void filterAddsNonEmptyTagsToRegistryForAnonymousExceptions() { public void filterAddsNonEmptyTagsToRegistryForAnonymousExceptions() {
final Exception anonymous = new Exception("test error") {}; final Exception anonymous = new Exception("test error") {
};
MockServerWebExchange exchange = createExchange("/projects/spring-boot", MockServerWebExchange exchange = createExchange("/projects/spring-boot",
"/projects/{project}"); "/projects/{project}");
this.webFilter this.webFilter.filter(exchange, (serverWebExchange) -> Mono.error(anonymous))
.filter(exchange,
(serverWebExchange) -> Mono
.error(anonymous))
.onErrorResume((t) -> { .onErrorResume((t) -> {
exchange.getResponse().setStatusCodeValue(500); exchange.getResponse().setStatusCodeValue(500);
return exchange.getResponse().setComplete(); return exchange.getResponse().setComplete();

@ -189,15 +189,15 @@ public class WebMvcMetricsFilterTests {
} }
@Test @Test
public void anonymousError() throws Exception { public void anonymousError() {
try { try {
mvc.perform(get("/api/c1/anonymousError/10")); this.mvc.perform(get("/api/c1/anonymousError/10"));
} catch(Throwable ignore) {
} }
catch (Throwable ignore) {
assertThat(this.registry.get("http.server.requests").tag("uri", "/api/c1/anonymousError/{id}").timer().getId() }
.getTag("exception")) assertThat(this.registry.get("http.server.requests")
.endsWith("$1"); .tag("uri", "/api/c1/anonymousError/{id}").timer().getId()
.getTag("exception")).endsWith("$1");
} }
@Test @Test
@ -454,8 +454,10 @@ public class WebMvcMetricsFilterTests {
@Timed @Timed
@GetMapping("/anonymousError/{id}") @GetMapping("/anonymousError/{id}")
public String alwaysThrowsAnonymousException(@PathVariable Long id) throws Exception { public String alwaysThrowsAnonymousException(@PathVariable Long id)
throw new Exception("this exception won't have a simple class name") {}; throws Exception {
throw new Exception("this exception won't have a simple class name") {
};
} }
@Timed @Timed

Loading…
Cancel
Save