Skip to content

Commit ee37dc1

Browse files
Jon Schneidersnicoll
Jon Schneider
authored andcommitted
Make sure exception tag values are not empty in web metrics
See gh-13187
1 parent 66156cc commit ee37dc1

File tree

4 files changed

+45
-4
lines changed

4 files changed

+45
-4
lines changed

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web/reactive/server/WebFluxTags.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ public static Tag uri(ServerWebExchange exchange) {
109109
*/
110110
public static Tag exception(Throwable exception) {
111111
if (exception != null) {
112-
return Tag.of("exception", exception.getClass().getSimpleName());
112+
String simpleName = exception.getClass().getSimpleName();
113+
return Tag.of("exception", simpleName.isEmpty() ? exception.getClass().getName() : simpleName);
113114
}
114115
return EXCEPTION_NONE;
115116
}

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web/servlet/WebMvcTags.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,11 @@ private static String getPathInfo(HttpServletRequest request) {
134134
* @return the exception tag derived from the exception
135135
*/
136136
public static Tag exception(Throwable exception) {
137-
return (exception != null
138-
? Tag.of("exception", exception.getClass().getSimpleName())
139-
: EXCEPTION_NONE);
137+
if (exception != null) {
138+
String simpleName = exception.getClass().getSimpleName();
139+
return Tag.of("exception", simpleName.isEmpty() ? exception.getClass().getName() : simpleName);
140+
}
141+
return EXCEPTION_NONE;
140142
}
141143

142144
}

spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/web/reactive/server/MetricsWebFilterTests.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,26 @@ public void filterAddsTagsToRegistryForExceptions() {
7777
}).block();
7878
assertMetricsContainsTag("uri", "/projects/{project}");
7979
assertMetricsContainsTag("status", "500");
80+
assertMetricsContainsTag("exception", "IllegalStateException");
81+
}
82+
83+
@Test
84+
public void filterAddsNonEmptyTagsToRegistryForAnonymousExceptions() {
85+
final Exception anonymous = new Exception("test error") {};
86+
87+
MockServerWebExchange exchange = createExchange("/projects/spring-boot",
88+
"/projects/{project}");
89+
this.webFilter
90+
.filter(exchange,
91+
(serverWebExchange) -> Mono
92+
.error(anonymous))
93+
.onErrorResume((t) -> {
94+
exchange.getResponse().setStatusCodeValue(500);
95+
return exchange.getResponse().setComplete();
96+
}).block();
97+
assertMetricsContainsTag("uri", "/projects/{project}");
98+
assertMetricsContainsTag("status", "500");
99+
assertMetricsContainsTag("exception", anonymous.getClass().getName());
80100
}
81101

82102
@Test

spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/web/servlet/WebMvcMetricsFilterTests.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,18 @@ public void unhandledError() {
188188
.tags("exception", "RuntimeException").timer().count()).isEqualTo(1L);
189189
}
190190

191+
@Test
192+
public void anonymousError() throws Exception {
193+
try {
194+
mvc.perform(get("/api/c1/anonymousError/10"));
195+
} catch(Throwable ignore) {
196+
}
197+
198+
assertThat(this.registry.get("http.server.requests").tag("uri", "/api/c1/anonymousError/{id}").timer().getId()
199+
.getTag("exception"))
200+
.endsWith("$1");
201+
}
202+
191203
@Test
192204
public void asyncCallableRequest() throws Exception {
193205
AtomicReference<MvcResult> result = new AtomicReference<>();
@@ -440,6 +452,12 @@ public String alwaysThrowsException(@PathVariable Long id) {
440452
throw new IllegalStateException("Boom on " + id + "!");
441453
}
442454

455+
@Timed
456+
@GetMapping("/anonymousError/{id}")
457+
public String alwaysThrowsAnonymousException(@PathVariable Long id) throws Exception {
458+
throw new Exception("this exception won't have a simple class name") {};
459+
}
460+
443461
@Timed
444462
@GetMapping("/unhandledError/{id}")
445463
public String alwaysThrowsUnhandledException(@PathVariable Long id) {

0 commit comments

Comments
 (0)