27
27
import io .micrometer .observation .contextpropagation .ObservationThreadLocalAccessor ;
28
28
import io .micrometer .observation .tck .TestObservationRegistry ;
29
29
import io .micrometer .observation .tck .TestObservationRegistryAssert ;
30
+ import org .junit .jupiter .api .AfterEach ;
30
31
import org .junit .jupiter .api .BeforeEach ;
31
32
import org .junit .jupiter .api .Test ;
32
33
import org .mockito .ArgumentCaptor ;
33
34
import reactor .core .publisher .Flux ;
35
+ import reactor .core .publisher .Hooks ;
34
36
import reactor .core .publisher .Mono ;
35
37
import reactor .test .StepVerifier ;
36
38
@@ -63,6 +65,7 @@ class WebClientObservationTests {
63
65
64
66
@ BeforeEach
65
67
void setup () {
68
+ Hooks .enableAutomaticContextPropagation ();
66
69
ClientResponse mockResponse = mock ();
67
70
when (mockResponse .statusCode ()).thenReturn (HttpStatus .OK );
68
71
when (mockResponse .headers ()).thenReturn (new MockClientHeaders ());
@@ -74,6 +77,11 @@ void setup() {
74
77
this .observationRegistry .observationConfig ().observationHandler (new HeaderInjectingHandler ());
75
78
}
76
79
80
+ @ AfterEach
81
+ void cleanUp () {
82
+ Hooks .disableAutomaticContextPropagation ();
83
+ }
84
+
77
85
@ Test
78
86
void recordsObservationForSuccessfulExchange () {
79
87
this .builder .build ().get ().uri ("/resource/{id}" , 42 )
@@ -148,6 +156,19 @@ void setsCurrentObservationInReactorContext() {
148
156
verifyAndGetRequest ();
149
157
}
150
158
159
+ @ Test
160
+ void setsCurrentObservationInScope () {
161
+ ExchangeFilterFunction assertionFilter = (request , chain ) -> {
162
+ Observation currentObservation = observationRegistry .getCurrentObservation ();
163
+ assertThat (currentObservation ).isNotNull ();
164
+ assertThat (currentObservation .getContext ()).isInstanceOf (ClientRequestObservationContext .class );
165
+ return chain .exchange (request );
166
+ };
167
+ this .builder .filter (assertionFilter ).build ().get ().uri ("/resource/{id}" , 42 )
168
+ .retrieve ().bodyToMono (Void .class ).block (Duration .ofSeconds (5 ));
169
+ verifyAndGetRequest ();
170
+ }
171
+
151
172
@ Test
152
173
void recordsObservationWithResponseDetailsWhenFilterFunctionErrors () {
153
174
ExchangeFilterFunction errorFunction = (req , next ) -> next .exchange (req ).then (Mono .error (new IllegalStateException ()));
0 commit comments