2929import io .micrometer .observation .Observation ;
3030import io .micrometer .observation .ObservationConvention ;
3131import io .micrometer .observation .ObservationRegistry ;
32+ import io .micrometer .observation .ObservationView ;
3233import jakarta .servlet .Filter ;
3334import jakarta .servlet .FilterChain ;
3435import jakarta .servlet .ServletException ;
@@ -53,6 +54,8 @@ public final class ObservationFilterChainDecorator implements FilterChainProxy.F
5354 private static final Log logger = LogFactory .getLog (FilterChainProxy .class );
5455
5556 private static final String ATTRIBUTE = ObservationFilterChainDecorator .class + ".observation" ;
57+ private static final String ATTRIBUTE_BEFORE_OBSERVATION_VIEW = ObservationFilterChainDecorator .class + ".observation.view.before" ;
58+ private static final String ATTRIBUTE_AFTER_OBSERVATION_VIEW = ObservationFilterChainDecorator .class + ".observation.view.after" ;
5659
5760 static final String UNSECURED_OBSERVATION_NAME = "spring.security.http.unsecured.requests" ;
5861
@@ -250,9 +253,30 @@ private void wrapFilter(ServletRequest request, ServletResponse response, Filter
250253 private AroundFilterObservation parent (HttpServletRequest request ) {
251254 FilterChainObservationContext beforeContext = FilterChainObservationContext .before ();
252255 FilterChainObservationContext afterContext = FilterChainObservationContext .after ();
256+
257+ Object maybeBeforeObservationView = request .getAttribute (ATTRIBUTE_BEFORE_OBSERVATION_VIEW );
258+ if (maybeBeforeObservationView != null ) {
259+ ObservationView observationView = (ObservationView ) maybeBeforeObservationView ;
260+ beforeContext .setParentObservation (observationView );
261+ }
262+
263+ Object maybeAfterObservationView = request .getAttribute (ATTRIBUTE_AFTER_OBSERVATION_VIEW );
264+ if (maybeAfterObservationView != null ) {
265+ ObservationView observationView = (ObservationView ) maybeAfterObservationView ;
266+ afterContext .setParentObservation (observationView );
267+ }
268+
253269 Observation before = Observation .createNotStarted (this .convention , () -> beforeContext , this .registry );
254270 Observation after = Observation .createNotStarted (this .convention , () -> afterContext , this .registry );
255271 AroundFilterObservation parent = AroundFilterObservation .create (before , after );
272+
273+ if (request .getAttribute (ATTRIBUTE_BEFORE_OBSERVATION_VIEW ) == null ) {
274+ request .setAttribute (ATTRIBUTE_BEFORE_OBSERVATION_VIEW , before .getContext ().getParentObservation ());
275+ }
276+ if (request .getAttribute (ATTRIBUTE_AFTER_OBSERVATION_VIEW ) == null ) {
277+ request .setAttribute (ATTRIBUTE_AFTER_OBSERVATION_VIEW , after .getContext ().getParentObservation ());
278+ }
279+
256280 request .setAttribute (ATTRIBUTE , parent );
257281 return parent ;
258282 }
0 commit comments