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 ;
@@ -53,6 +54,8 @@ public final class ObservationFilterChainDecorator implements FilterChainProxy.F
53
54
private static final Log logger = LogFactory .getLog (FilterChainProxy .class );
54
55
55
56
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" ;
56
59
57
60
static final String UNSECURED_OBSERVATION_NAME = "spring.security.http.unsecured.requests" ;
58
61
@@ -250,9 +253,30 @@ private void wrapFilter(ServletRequest request, ServletResponse response, Filter
250
253
private AroundFilterObservation parent (HttpServletRequest request ) {
251
254
FilterChainObservationContext beforeContext = FilterChainObservationContext .before ();
252
255
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
+
253
269
Observation before = Observation .createNotStarted (this .convention , () -> beforeContext , this .registry );
254
270
Observation after = Observation .createNotStarted (this .convention , () -> afterContext , this .registry );
255
271
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
+
256
280
request .setAttribute (ATTRIBUTE , parent );
257
281
return parent ;
258
282
}
0 commit comments