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,14 +157,32 @@ private Observation createOrFetchObservation(HttpServletRequest request, HttpSer
160
157
return observation ;
161
158
}
162
159
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 ));
165
169
}
166
170
167
171
static @ Nullable Throwable fetchException (ServletRequest request ) {
168
172
return (Throwable ) request .getAttribute (RequestDispatcher .ERROR_EXCEPTION );
169
173
}
170
174
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
+
171
186
private static class ObservationAsyncListener implements AsyncListener {
172
187
173
188
private final Observation currentObservation ;
@@ -195,7 +210,6 @@ public void onComplete(AsyncEvent event) {
195
210
public void onError (AsyncEvent event ) {
196
211
this .currentObservation .error (unwrapServletException (event .getThrowable ()));
197
212
}
198
-
199
213
}
200
214
201
215
}
0 commit comments