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,14 +157,32 @@ private Observation createOrFetchObservation(HttpServletRequest request, HttpSer
160157 return observation ;
161158 }
162159
163- static @ Nullable Throwable unwrapServletException (Throwable ex ) {
164- 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 ));
165169 }
166170
167171 static @ Nullable Throwable fetchException (ServletRequest request ) {
168172 return (Throwable ) request .getAttribute (RequestDispatcher .ERROR_EXCEPTION );
169173 }
170174
175+ static Throwable unwrapServletException (Throwable ex ) {
176+ if (ex instanceof ServletException ) {
177+ Throwable cause = ex .getCause ();
178+ if (cause != null ) {
179+ return cause ;
180+ }
181+ }
182+ return ex ;
183+ }
184+
185+
171186 private static class ObservationAsyncListener implements AsyncListener {
172187
173188 private final Observation currentObservation ;
@@ -195,7 +210,6 @@ public void onComplete(AsyncEvent event) {
195210 public void onError (AsyncEvent event ) {
196211 this .currentObservation .error (unwrapServletException (event .getThrowable ()));
197212 }
198-
199213 }
200214
201215}
0 commit comments