You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Allow to manually tag request metrics with exceptions
Prior to this commit, some exceptions handled at the controller or
handler function level would:
* not bubble up to the Spring Boot error handling support
* not be tagged as part of the request metrics
This situation is inconsistent because in general, exceptions handled at
the controller level can be considered as expected behavior.
Also, depending on how the exception is handled, the request metrics
might not be tagged with the exception.
This will be reconsidered in gh-23795.
This commit prepares a transition to the new situation. Developers can
now opt-in and set the handled exception as a request attribute. This
well-known attribute will be later read by the metrics support and used
for tagging the request metrics with the exception provided.
This mechanism is automatically used by the error handling support in
Spring Boot.
Closesgh-24028
Copy file name to clipboardExpand all lines: spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web/reactive/server/MetricsWebFilter.java
Copy file name to clipboardExpand all lines: spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web/servlet/WebMvcMetricsFilter.java
+11-2
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,5 @@
1
1
/*
2
-
* Copyright 2012-2020 the original author or authors.
2
+
* Copyright 2012-2021 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
Copy file name to clipboardExpand all lines: spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/web/reactive/server/MetricsWebFilterTests.java
Copy file name to clipboardExpand all lines: spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/web/servlet/WebMvcMetricsFilterTests.java
+6-2
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,5 @@
1
1
/*
2
-
* Copyright 2012-2020 the original author or authors.
2
+
* Copyright 2012-2021 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
Copy file name to clipboardExpand all lines: spring-boot-project/spring-boot-docs/src/docs/asciidoc/production-ready-features.adoc
+4-1
Original file line number
Diff line number
Diff line change
@@ -2188,6 +2188,8 @@ By default, Spring MVC-related metrics are tagged with the following information
2188
2188
To add to the default tags, provide one or more ``@Bean``s that implement `WebMvcTagsContributor`.
2189
2189
To replace the default tags, provide a `@Bean` that implements `WebMvcTagsProvider`.
2190
2190
2191
+
TIP: In some cases, exceptions handled in Web controllers are not recorded as request metrics tags.
2192
+
Applications can opt-in and record exceptions by <<spring-boot-features.adoc#boot-features-error-handling, setting handled exceptions as request parameters>>.
2191
2193
2192
2194
2193
2195
[[production-ready-metrics-web-flux]]
@@ -2222,7 +2224,8 @@ By default, WebFlux-related metrics are tagged with the following information:
2222
2224
To add to the default tags, provide one or more ``@Bean``s that implement `WebFluxTagsContributor`.
2223
2225
To replace the default tags, provide a `@Bean` that implements `WebFluxTagsProvider`.
2224
2226
2225
-
2227
+
TIP: In some cases, exceptions handled in controllers and handler functions are not recorded as request metrics tags.
2228
+
Applications can opt-in and record exceptions by <<spring-boot-features.adoc#boot-features-webflux-error-handling, setting handled exceptions as request parameters>>.
In the preceding example, if `YourException` is thrown by a controller defined in the same package as `AcmeController`, a JSON representation of the `CustomErrorType` POJO is used instead of the `ErrorAttributes` representation.
2568
2568
2569
+
In some cases, errors handled at the controller level are not recorded by the <<production-ready-features.adoc#production-ready-metrics-spring-mvc, metrics infrastructure>>.
2570
+
Applications can ensure that such exceptions are recorded with the request metrics by setting the handled exception as a request attribute:
For a more complete picture, you can also subclass `DefaultErrorWebExceptionHandler` directly and override specific methods.
2825
2831
2832
+
In some cases, errors handled at the controller or handler function level are not recorded by the <<production-ready-features.adoc#production-ready-metrics-web-flux, metrics infrastructure>>.
2833
+
Applications can ensure that such exceptions are recorded with the request metrics by setting the handled exception as a request attribute:
Copy file name to clipboardExpand all lines: spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/reactive/error/DefaultErrorAttributes.java
Copy file name to clipboardExpand all lines: spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/reactive/error/ErrorAttributes.java
+7
Original file line number
Diff line number
Diff line change
@@ -34,6 +34,13 @@
34
34
*/
35
35
publicinterfaceErrorAttributes {
36
36
37
+
/**
38
+
* Name of the {@link ServerRequest#attribute(String)} Request attribute} holding the
39
+
* error resolved by the {@code ErrorAttributes} implementation.
Copy file name to clipboardExpand all lines: spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/error/DefaultErrorAttributes.java
Copy file name to clipboardExpand all lines: spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/error/ErrorAttributes.java
+8
Original file line number
Diff line number
Diff line change
@@ -34,6 +34,14 @@
34
34
*/
35
35
publicinterfaceErrorAttributes {
36
36
37
+
/**
38
+
* Name of the {@link javax.servlet.http.HttpServletRequest#getAttribute(String)
39
+
* Request attribute} holding the error resolved by the {@code ErrorAttributes}
Copy file name to clipboardExpand all lines: spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/reactive/error/DefaultErrorAttributesTests.java
0 commit comments