5555public class ServerHttpObservationFilter extends OncePerRequestFilter {
5656
5757 /**
58- * Name of the request attribute holding the {@link ServerRequestObservationContext context } for the current observation.
58+ * Name of the request attribute holding the {@link ServerRequestObservationContext} for the current observation.
5959 */
60- public static final String CURRENT_OBSERVATION_CONTEXT_ATTRIBUTE = ServerHttpObservationFilter .class .getName () + ".context" ;
60+ public static final String CURRENT_OBSERVATION_CONTEXT_ATTRIBUTE =
61+ ServerHttpObservationFilter .class .getName () + ".context" ;
6162
62- private static final ServerRequestObservationConvention DEFAULT_OBSERVATION_CONVENTION = new DefaultServerRequestObservationConvention ();
63+ private static final String CURRENT_OBSERVATION_ATTRIBUTE =
64+ ServerHttpObservationFilter .class .getName () + ".observation" ;
6365
64- private static final String CURRENT_OBSERVATION_ATTRIBUTE = ServerHttpObservationFilter .class .getName () + ".observation" ;
66+ private static final ServerRequestObservationConvention DEFAULT_OBSERVATION_CONVENTION =
67+ new DefaultServerRequestObservationConvention ();
6568
6669
6770 private final ObservationRegistry observationRegistry ;
6871
6972 private final ServerRequestObservationConvention observationConvention ;
7073
74+
7175 /**
7276 * Create an {@code HttpRequestsObservationFilter} that records observations
7377 * against the given {@link ObservationRegistry}. The default
@@ -89,14 +93,6 @@ public ServerHttpObservationFilter(ObservationRegistry observationRegistry, Serv
8993 this .observationConvention = observationConvention ;
9094 }
9195
92- /**
93- * Get the current {@link ServerRequestObservationContext observation context} from the given request, if available.
94- * @param request the current request
95- * @return the current observation context
96- */
97- public static Optional <ServerRequestObservationContext > findObservationContext (HttpServletRequest request ) {
98- return Optional .ofNullable ((ServerRequestObservationContext ) request .getAttribute (CURRENT_OBSERVATION_CONTEXT_ATTRIBUTE ));
99- }
10096
10197 @ Override
10298 protected boolean shouldNotFilterAsyncDispatch () {
@@ -150,8 +146,9 @@ private Observation createOrFetchObservation(HttpServletRequest request, HttpSer
150146 Observation observation = (Observation ) request .getAttribute (CURRENT_OBSERVATION_ATTRIBUTE );
151147 if (observation == null ) {
152148 ServerRequestObservationContext context = new ServerRequestObservationContext (request , response );
153- observation = ServerHttpObservationDocumentation .HTTP_SERVLET_SERVER_REQUESTS .observation (this .observationConvention ,
154- DEFAULT_OBSERVATION_CONVENTION , () -> context , this .observationRegistry ).start ();
149+ observation = ServerHttpObservationDocumentation .HTTP_SERVLET_SERVER_REQUESTS .observation (
150+ this .observationConvention , DEFAULT_OBSERVATION_CONVENTION , () -> context , this .observationRegistry )
151+ .start ();
155152 request .setAttribute (CURRENT_OBSERVATION_ATTRIBUTE , observation );
156153 if (!observation .isNoop ()) {
157154 request .setAttribute (CURRENT_OBSERVATION_CONTEXT_ATTRIBUTE , observation .getContext ());
@@ -160,16 +157,33 @@ private Observation createOrFetchObservation(HttpServletRequest request, HttpSer
160157 return observation ;
161158 }
162159
163- @ Nullable
164- static Throwable unwrapServletException (Throwable ex ) {
165- return (ex instanceof ServletException ) ? ex .getCause () : ex ;
160+
161+ /**
162+ * Get the current {@link ServerRequestObservationContext observation context} from the given request, if available.
163+ * @param request the current request
164+ * @return the current observation context
165+ */
166+ public static Optional <ServerRequestObservationContext > findObservationContext (HttpServletRequest request ) {
167+ return Optional .ofNullable (
168+ (ServerRequestObservationContext ) request .getAttribute (CURRENT_OBSERVATION_CONTEXT_ATTRIBUTE ));
166169 }
167170
168171 @ Nullable
169172 static Throwable fetchException (ServletRequest request ) {
170173 return (Throwable ) request .getAttribute (RequestDispatcher .ERROR_EXCEPTION );
171174 }
172175
176+ static Throwable unwrapServletException (Throwable ex ) {
177+ if (ex instanceof ServletException ) {
178+ Throwable cause = ex .getCause ();
179+ if (cause != null ) {
180+ return cause ;
181+ }
182+ }
183+ return ex ;
184+ }
185+
186+
173187 private static class ObservationAsyncListener implements AsyncListener {
174188
175189 private final Observation currentObservation ;
@@ -197,7 +211,6 @@ public void onComplete(AsyncEvent event) {
197211 public void onError (AsyncEvent event ) {
198212 this .currentObservation .error (unwrapServletException (event .getThrowable ()));
199213 }
200-
201214 }
202215
203216}
0 commit comments