27
27
import io .micrometer .observation .Observation ;
28
28
import io .micrometer .observation .ObservationConvention ;
29
29
import io .micrometer .observation .ObservationRegistry ;
30
+ import io .micrometer .observation .contextpropagation .ObservationThreadLocalAccessor ;
30
31
import reactor .core .publisher .Mono ;
31
32
32
33
import org .springframework .lang .Nullable ;
@@ -73,20 +74,22 @@ private static AroundWebFilterObservation observation(ServerWebExchange exchange
73
74
}
74
75
75
76
private WebFilterChain wrapSecured (WebFilterChain original ) {
76
- return (exchange ) -> {
77
+ return (exchange ) -> Mono . deferContextual (( contextView ) -> {
77
78
AroundWebFilterObservation parent = observation (exchange );
79
+ Observation parentObservation = contextView .getOrDefault (ObservationThreadLocalAccessor .KEY , null );
78
80
Observation observation = Observation .createNotStarted (SECURED_OBSERVATION_NAME , this .registry )
79
- .contextualName ("secured request" );
81
+ .contextualName ("secured request" ). parentObservation ( parentObservation ) ;
80
82
return parent .wrap (WebFilterObservation .create (observation ).wrap (original )).filter (exchange );
81
- };
83
+ }) ;
82
84
}
83
85
84
86
private WebFilterChain wrapUnsecured (WebFilterChain original ) {
85
- return (exchange ) -> {
87
+ return (exchange ) -> Mono .deferContextual ((contextView ) -> {
88
+ Observation parentObservation = contextView .getOrDefault (ObservationThreadLocalAccessor .KEY , null );
86
89
Observation observation = Observation .createNotStarted (UNSECURED_OBSERVATION_NAME , this .registry )
87
- .contextualName ("unsecured request" );
90
+ .contextualName ("unsecured request" ). parentObservation ( parentObservation ) ;
88
91
return WebFilterObservation .create (observation ).wrap (original ).filter (exchange );
89
- };
92
+ }) ;
90
93
}
91
94
92
95
private List <ObservationWebFilter > wrap (List <WebFilter > filters ) {
@@ -186,8 +189,11 @@ String getName() {
186
189
@ Override
187
190
public Mono <Void > filter (ServerWebExchange exchange , WebFilterChain chain ) {
188
191
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
+ });
191
197
}
192
198
else {
193
199
return wrapFilter (exchange , chain );
@@ -211,11 +217,13 @@ private Mono<Void> wrapFilter(ServerWebExchange exchange, WebFilterChain chain)
211
217
});
212
218
}
213
219
214
- private AroundWebFilterObservation parent (ServerWebExchange exchange ) {
220
+ private AroundWebFilterObservation parent (ServerWebExchange exchange , Observation parentObservation ) {
215
221
WebFilterChainObservationContext beforeContext = WebFilterChainObservationContext .before ();
216
222
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 );
219
227
AroundWebFilterObservation parent = AroundWebFilterObservation .create (before , after );
220
228
exchange .getAttributes ().put (ATTRIBUTE , parent );
221
229
return parent ;
0 commit comments