|
27 | 27 | import io.micrometer.observation.ObservationHandler;
|
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 |
|
33 | 34 | import org.springframework.http.HttpHeaders;
|
34 | 35 | import org.springframework.http.HttpMethod;
|
| 36 | +import org.springframework.http.HttpRequest; |
35 | 37 | import org.springframework.http.HttpStatus;
|
36 | 38 | import org.springframework.http.MediaType;
|
37 | 39 | import org.springframework.http.client.ClientHttpRequest;
|
| 40 | +import org.springframework.http.client.ClientHttpRequestExecution; |
38 | 41 | import org.springframework.http.client.ClientHttpRequestFactory;
|
| 42 | +import org.springframework.http.client.ClientHttpRequestInterceptor; |
39 | 43 | import org.springframework.http.client.ClientHttpResponse;
|
40 | 44 | import org.springframework.http.client.observation.ClientRequestObservationContext;
|
41 | 45 | import org.springframework.http.client.observation.ClientRequestObservationConvention;
|
@@ -73,12 +77,15 @@ class RestClientObservationTests {
|
73 | 77 |
|
74 | 78 | @BeforeEach
|
75 | 79 | void setupEach() {
|
76 |
| - this.client = RestClient.builder() |
| 80 | + this.client = createBuilder().build(); |
| 81 | + this.observationRegistry.observationConfig().observationHandler(new ContextAssertionObservationHandler()); |
| 82 | + } |
| 83 | + |
| 84 | + RestClient.Builder createBuilder() { |
| 85 | + return RestClient.builder() |
77 | 86 | .messageConverters(converters -> converters.add(0, this.converter))
|
78 | 87 | .requestFactory(this.requestFactory)
|
79 |
| - .observationRegistry(this.observationRegistry) |
80 |
| - .build(); |
81 |
| - this.observationRegistry.observationConfig().observationHandler(new ContextAssertionObservationHandler()); |
| 88 | + .observationRegistry(this.observationRegistry); |
82 | 89 | }
|
83 | 90 |
|
84 | 91 | @Test
|
@@ -238,6 +245,22 @@ void registerObservationWhenReadingStream() throws Exception {
|
238 | 245 | assertThatHttpObservation().hasLowCardinalityKeyValue("outcome", "SUCCESS");
|
239 | 246 | }
|
240 | 247 |
|
| 248 | + @Test |
| 249 | + void openScopeWithObservation() throws Exception { |
| 250 | + this.client = createBuilder().requestInterceptor(new ObservationContextInterceptor(this.observationRegistry)) |
| 251 | + .defaultStatusHandler(new ObservationErrorHandler(this.observationRegistry)).build(); |
| 252 | + mockSentRequest(GET, "https://example.org"); |
| 253 | + mockResponseStatus(HttpStatus.OK); |
| 254 | + mockResponseBody("Hello World", MediaType.TEXT_PLAIN); |
| 255 | + |
| 256 | + client.get().uri("https://example.org").retrieve().toBodilessEntity(); |
| 257 | + } |
| 258 | + |
| 259 | + @AfterEach |
| 260 | + void checkAfter() { |
| 261 | + assertThat(this.observationRegistry.getCurrentObservationScope()).isNull(); |
| 262 | + } |
| 263 | + |
241 | 264 |
|
242 | 265 | private void mockSentRequest(HttpMethod method, String uri) throws Exception {
|
243 | 266 | mockSentRequest(method, uri, new HttpHeaders());
|
@@ -288,4 +311,38 @@ record User(String name) {
|
288 | 311 |
|
289 | 312 | }
|
290 | 313 |
|
| 314 | + static class ObservationContextInterceptor implements ClientHttpRequestInterceptor { |
| 315 | + |
| 316 | + private final TestObservationRegistry observationRegistry; |
| 317 | + |
| 318 | + public ObservationContextInterceptor(TestObservationRegistry observationRegistry) { |
| 319 | + this.observationRegistry = observationRegistry; |
| 320 | + } |
| 321 | + |
| 322 | + @Override |
| 323 | + public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException { |
| 324 | + assertThat(this.observationRegistry.getCurrentObservationScope()).isNotNull(); |
| 325 | + return execution.execute(request, body); |
| 326 | + } |
| 327 | + } |
| 328 | + |
| 329 | + static class ObservationErrorHandler implements ResponseErrorHandler { |
| 330 | + |
| 331 | + final TestObservationRegistry observationRegistry; |
| 332 | + |
| 333 | + ObservationErrorHandler(TestObservationRegistry observationRegistry) { |
| 334 | + this.observationRegistry = observationRegistry; |
| 335 | + } |
| 336 | + |
| 337 | + @Override |
| 338 | + public boolean hasError(ClientHttpResponse response) throws IOException { |
| 339 | + return true; |
| 340 | + } |
| 341 | + |
| 342 | + @Override |
| 343 | + public void handleError(ClientHttpResponse response) throws IOException { |
| 344 | + assertThat(this.observationRegistry.getCurrentObservationScope()).isNotNull(); |
| 345 | + } |
| 346 | + } |
| 347 | + |
291 | 348 | }
|
0 commit comments