55
55
public class ServerHttpObservationFilter extends OncePerRequestFilter {
56
56
57
57
/**
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.
59
59
*/
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" ;
61
62
62
- private static final ServerRequestObservationConvention DEFAULT_OBSERVATION_CONVENTION = new DefaultServerRequestObservationConvention ();
63
+ private static final String CURRENT_OBSERVATION_ATTRIBUTE =
64
+ ServerHttpObservationFilter .class .getName () + ".observation" ;
63
65
64
- private static final String CURRENT_OBSERVATION_ATTRIBUTE = ServerHttpObservationFilter .class .getName () + ".observation" ;
66
+ private static final ServerRequestObservationConvention DEFAULT_OBSERVATION_CONVENTION =
67
+ new DefaultServerRequestObservationConvention ();
65
68
66
69
67
70
private final ObservationRegistry observationRegistry ;
68
71
69
72
private final ServerRequestObservationConvention observationConvention ;
70
73
74
+
71
75
/**
72
76
* Create an {@code HttpRequestsObservationFilter} that records observations
73
77
* against the given {@link ObservationRegistry}. The default
@@ -89,14 +93,6 @@ public ServerHttpObservationFilter(ObservationRegistry observationRegistry, Serv
89
93
this .observationConvention = observationConvention ;
90
94
}
91
95
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
- }
100
96
101
97
@ Override
102
98
protected boolean shouldNotFilterAsyncDispatch () {
@@ -150,8 +146,9 @@ private Observation createOrFetchObservation(HttpServletRequest request, HttpSer
150
146
Observation observation = (Observation ) request .getAttribute (CURRENT_OBSERVATION_ATTRIBUTE );
151
147
if (observation == null ) {
152
148
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 ();
155
152
request .setAttribute (CURRENT_OBSERVATION_ATTRIBUTE , observation );
156
153
if (!observation .isNoop ()) {
157
154
request .setAttribute (CURRENT_OBSERVATION_CONTEXT_ATTRIBUTE , observation .getContext ());
@@ -160,16 +157,33 @@ private Observation createOrFetchObservation(HttpServletRequest request, HttpSer
160
157
return observation ;
161
158
}
162
159
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 ));
166
169
}
167
170
168
171
@ Nullable
169
172
static Throwable fetchException (ServletRequest request ) {
170
173
return (Throwable ) request .getAttribute (RequestDispatcher .ERROR_EXCEPTION );
171
174
}
172
175
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
+
173
187
private static class ObservationAsyncListener implements AsyncListener {
174
188
175
189
private final Observation currentObservation ;
@@ -197,7 +211,6 @@ public void onComplete(AsyncEvent event) {
197
211
public void onError (AsyncEvent event ) {
198
212
this .currentObservation .error (unwrapServletException (event .getThrowable ()));
199
213
}
200
-
201
214
}
202
215
203
216
}
0 commit comments