2727import io .micrometer .observation .Observation ;
2828import io .micrometer .observation .ObservationConvention ;
2929import io .micrometer .observation .ObservationRegistry ;
30+ import io .micrometer .observation .contextpropagation .ObservationThreadLocalAccessor ;
3031import reactor .core .publisher .Mono ;
3132
3233import org .springframework .lang .Nullable ;
@@ -73,20 +74,22 @@ private static AroundWebFilterObservation observation(ServerWebExchange exchange
7374 }
7475
7576 private WebFilterChain wrapSecured (WebFilterChain original ) {
76- return (exchange ) -> {
77+ return (exchange ) -> Mono . deferContextual (( contextView ) -> {
7778 AroundWebFilterObservation parent = observation (exchange );
79+ Observation parentObservation = contextView .getOrDefault (ObservationThreadLocalAccessor .KEY , null );
7880 Observation observation = Observation .createNotStarted (SECURED_OBSERVATION_NAME , this .registry )
79- .contextualName ("secured request" );
81+ .contextualName ("secured request" ). parentObservation ( parentObservation ) ;
8082 return parent .wrap (WebFilterObservation .create (observation ).wrap (original )).filter (exchange );
81- };
83+ }) ;
8284 }
8385
8486 private WebFilterChain wrapUnsecured (WebFilterChain original ) {
85- return (exchange ) -> {
87+ return (exchange ) -> Mono .deferContextual ((contextView ) -> {
88+ Observation parentObservation = contextView .getOrDefault (ObservationThreadLocalAccessor .KEY , null );
8689 Observation observation = Observation .createNotStarted (UNSECURED_OBSERVATION_NAME , this .registry )
87- .contextualName ("unsecured request" );
90+ .contextualName ("unsecured request" ). parentObservation ( parentObservation ) ;
8891 return WebFilterObservation .create (observation ).wrap (original ).filter (exchange );
89- };
92+ }) ;
9093 }
9194
9295 private List <ObservationWebFilter > wrap (List <WebFilter > filters ) {
@@ -186,8 +189,11 @@ String getName() {
186189 @ Override
187190 public Mono <Void > filter (ServerWebExchange exchange , WebFilterChain chain ) {
188191 if (this .position == 1 ) {
189- AroundWebFilterObservation parent = parent (exchange );
190- return parent .wrap (this ::wrapFilter ).filter (exchange , chain );
192+ return Mono .deferContextual ((contextView ) -> {
193+ Observation parentObservation = contextView .getOrDefault (ObservationThreadLocalAccessor .KEY , null );
194+ AroundWebFilterObservation parent = parent (exchange , parentObservation );
195+ return parent .wrap (this ::wrapFilter ).filter (exchange , chain );
196+ });
191197 }
192198 else {
193199 return wrapFilter (exchange , chain );
@@ -211,11 +217,13 @@ private Mono<Void> wrapFilter(ServerWebExchange exchange, WebFilterChain chain)
211217 });
212218 }
213219
214- private AroundWebFilterObservation parent (ServerWebExchange exchange ) {
220+ private AroundWebFilterObservation parent (ServerWebExchange exchange , Observation parentObservation ) {
215221 WebFilterChainObservationContext beforeContext = WebFilterChainObservationContext .before ();
216222 WebFilterChainObservationContext afterContext = WebFilterChainObservationContext .after ();
217- Observation before = Observation .createNotStarted (this .convention , () -> beforeContext , this .registry );
218- Observation after = Observation .createNotStarted (this .convention , () -> afterContext , this .registry );
223+ Observation before = Observation .createNotStarted (this .convention , () -> beforeContext , this .registry )
224+ .parentObservation (parentObservation );
225+ Observation after = Observation .createNotStarted (this .convention , () -> afterContext , this .registry )
226+ .parentObservation (parentObservation );
219227 AroundWebFilterObservation parent = AroundWebFilterObservation .create (before , after );
220228 exchange .getAttributes ().put (ATTRIBUTE , parent );
221229 return parent ;
0 commit comments