29
29
import io .micrometer .observation .Observation ;
30
30
import io .micrometer .observation .ObservationConvention ;
31
31
import io .micrometer .observation .ObservationRegistry ;
32
+ import io .micrometer .observation .ObservationView ;
32
33
import jakarta .servlet .Filter ;
33
34
import jakarta .servlet .FilterChain ;
34
35
import jakarta .servlet .ServletException ;
46
47
* wraps the chain in before and after observations
47
48
*
48
49
* @author Josh Cummings
50
+ * @author Nikita Konev
49
51
* @since 6.0
50
52
*/
51
53
public final class ObservationFilterChainDecorator implements FilterChainProxy .FilterChainDecorator {
@@ -54,6 +56,12 @@ public final class ObservationFilterChainDecorator implements FilterChainProxy.F
54
56
55
57
private static final String ATTRIBUTE = ObservationFilterChainDecorator .class + ".observation" ;
56
58
59
+ private static final String ATTRIBUTE_BEFORE_OBSERVATION_VIEW = ObservationFilterChainDecorator .class
60
+ + ".observation.view.before" ;
61
+
62
+ private static final String ATTRIBUTE_AFTER_OBSERVATION_VIEW = ObservationFilterChainDecorator .class
63
+ + ".observation.view.after" ;
64
+
57
65
static final String UNSECURED_OBSERVATION_NAME = "spring.security.http.unsecured.requests" ;
58
66
59
67
static final String SECURED_OBSERVATION_NAME = "spring.security.http.secured.requests" ;
@@ -250,9 +258,30 @@ private void wrapFilter(ServletRequest request, ServletResponse response, Filter
250
258
private AroundFilterObservation parent (HttpServletRequest request ) {
251
259
FilterChainObservationContext beforeContext = FilterChainObservationContext .before ();
252
260
FilterChainObservationContext afterContext = FilterChainObservationContext .after ();
261
+
262
+ Object maybeBeforeObservationView = request .getAttribute (ATTRIBUTE_BEFORE_OBSERVATION_VIEW );
263
+ if (maybeBeforeObservationView != null ) {
264
+ ObservationView observationView = (ObservationView ) maybeBeforeObservationView ;
265
+ beforeContext .setParentObservation (observationView );
266
+ }
267
+
268
+ Object maybeAfterObservationView = request .getAttribute (ATTRIBUTE_AFTER_OBSERVATION_VIEW );
269
+ if (maybeAfterObservationView != null ) {
270
+ ObservationView observationView = (ObservationView ) maybeAfterObservationView ;
271
+ afterContext .setParentObservation (observationView );
272
+ }
273
+
253
274
Observation before = Observation .createNotStarted (this .convention , () -> beforeContext , this .registry );
254
275
Observation after = Observation .createNotStarted (this .convention , () -> afterContext , this .registry );
255
276
AroundFilterObservation parent = AroundFilterObservation .create (before , after );
277
+
278
+ if (maybeBeforeObservationView == null ) {
279
+ request .setAttribute (ATTRIBUTE_BEFORE_OBSERVATION_VIEW , before .getContext ().getParentObservation ());
280
+ }
281
+ if (maybeAfterObservationView == null ) {
282
+ request .setAttribute (ATTRIBUTE_AFTER_OBSERVATION_VIEW , after .getContext ().getParentObservation ());
283
+ }
284
+
256
285
request .setAttribute (ATTRIBUTE , parent );
257
286
return parent ;
258
287
}
0 commit comments