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 ;
@@ -54,6 +55,12 @@ public final class ObservationFilterChainDecorator implements FilterChainProxy.F
54
55
55
56
private static final String ATTRIBUTE = ObservationFilterChainDecorator .class + ".observation" ;
56
57
58
+ private static final String ATTRIBUTE_BEFORE_OBSERVATION_VIEW = ObservationFilterChainDecorator .class
59
+ + ".observation.view.before" ;
60
+
61
+ private static final String ATTRIBUTE_AFTER_OBSERVATION_VIEW = ObservationFilterChainDecorator .class
62
+ + ".observation.view.after" ;
63
+
57
64
static final String UNSECURED_OBSERVATION_NAME = "spring.security.http.unsecured.requests" ;
58
65
59
66
static final String SECURED_OBSERVATION_NAME = "spring.security.http.secured.requests" ;
@@ -250,9 +257,30 @@ private void wrapFilter(ServletRequest request, ServletResponse response, Filter
250
257
private AroundFilterObservation parent (HttpServletRequest request ) {
251
258
FilterChainObservationContext beforeContext = FilterChainObservationContext .before ();
252
259
FilterChainObservationContext afterContext = FilterChainObservationContext .after ();
260
+
261
+ Object maybeBeforeObservationView = request .getAttribute (ATTRIBUTE_BEFORE_OBSERVATION_VIEW );
262
+ if (maybeBeforeObservationView != null ) {
263
+ ObservationView observationView = (ObservationView ) maybeBeforeObservationView ;
264
+ beforeContext .setParentObservation (observationView );
265
+ }
266
+
267
+ Object maybeAfterObservationView = request .getAttribute (ATTRIBUTE_AFTER_OBSERVATION_VIEW );
268
+ if (maybeAfterObservationView != null ) {
269
+ ObservationView observationView = (ObservationView ) maybeAfterObservationView ;
270
+ afterContext .setParentObservation (observationView );
271
+ }
272
+
253
273
Observation before = Observation .createNotStarted (this .convention , () -> beforeContext , this .registry );
254
274
Observation after = Observation .createNotStarted (this .convention , () -> afterContext , this .registry );
255
275
AroundFilterObservation parent = AroundFilterObservation .create (before , after );
276
+
277
+ if (request .getAttribute (ATTRIBUTE_BEFORE_OBSERVATION_VIEW ) == null ) {
278
+ request .setAttribute (ATTRIBUTE_BEFORE_OBSERVATION_VIEW , before .getContext ().getParentObservation ());
279
+ }
280
+ if (request .getAttribute (ATTRIBUTE_AFTER_OBSERVATION_VIEW ) == null ) {
281
+ request .setAttribute (ATTRIBUTE_AFTER_OBSERVATION_VIEW , after .getContext ().getParentObservation ());
282
+ }
283
+
256
284
request .setAttribute (ATTRIBUTE , parent );
257
285
return parent ;
258
286
}
0 commit comments