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 org.springframework.http.HttpStatus;
import org.springframework.util.StringUtils;
import org.springframework.web.reactive.HandlerMapping;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.util.pattern.PathPattern;
@ -110,7 +111,8 @@ public final class WebFluxTags {
public static Tag exception(Throwable exception) {
if (exception != null) {
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;
}

@ -136,7 +136,8 @@ public final class WebMvcTags {
public static Tag exception(Throwable exception) {
if (exception != null) {
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;
}

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

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

Loading…
Cancel
Save