|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2021 the original author or authors. |
| 2 | + * Copyright 2002-2023 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
22 | 22 | import java.util.function.BiFunction;
|
23 | 23 | import java.util.function.Function;
|
24 | 24 |
|
| 25 | +import io.micrometer.observation.tck.TestObservationRegistry; |
| 26 | +import io.micrometer.observation.tck.TestObservationRegistryAssert; |
25 | 27 | import org.junit.jupiter.api.Test;
|
26 | 28 | import reactor.core.publisher.Flux;
|
27 | 29 | import reactor.core.publisher.Mono;
|
|
36 | 38 | import org.springframework.http.server.reactive.HttpHandler;
|
37 | 39 | import org.springframework.http.server.reactive.HttpHandlerDecoratorFactory;
|
38 | 40 | import org.springframework.http.server.reactive.ServerHttpRequest;
|
| 41 | +import org.springframework.http.server.reactive.observation.DefaultServerRequestObservationConvention; |
| 42 | +import org.springframework.http.server.reactive.observation.ServerRequestObservationConvention; |
39 | 43 | import org.springframework.web.server.ServerWebExchange;
|
40 | 44 | import org.springframework.web.server.WebExceptionHandler;
|
41 | 45 | import org.springframework.web.server.WebFilter;
|
|
50 | 54 | * Unit tests for {@link WebHttpHandlerBuilder}.
|
51 | 55 | *
|
52 | 56 | * @author Rossen Stoyanchev
|
| 57 | + * @author Brian Clozel |
53 | 58 | */
|
54 | 59 | public class WebHttpHandlerBuilderTests {
|
55 | 60 |
|
@@ -155,6 +160,19 @@ void httpHandlerDecoratorFactoryBeans() {
|
155 | 160 | assertThat(headerValue.apply("decoratorC")).isLessThan(headerValue.apply("decoratorB"));
|
156 | 161 | }
|
157 | 162 |
|
| 163 | + @Test |
| 164 | + void observationRegistry() { |
| 165 | + AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(ObservationConfig.class); |
| 166 | + HttpHandler handler = WebHttpHandlerBuilder.applicationContext(applicationContext).build(); |
| 167 | + |
| 168 | + MockServerHttpResponse response = new MockServerHttpResponse(); |
| 169 | + handler.handle(MockServerHttpRequest.get("/").build(), response).block(); |
| 170 | + |
| 171 | + TestObservationRegistry observationRegistry = applicationContext.getBean(TestObservationRegistry.class); |
| 172 | + TestObservationRegistryAssert.assertThat(observationRegistry).hasObservationWithNameEqualTo("http.server.requests") |
| 173 | + .that().hasLowCardinalityKeyValue("uri", "UNKNOWN"); |
| 174 | + } |
| 175 | + |
158 | 176 | private static Mono<Void> writeToResponse(ServerWebExchange exchange, String value) {
|
159 | 177 | byte[] bytes = value.getBytes(StandardCharsets.UTF_8);
|
160 | 178 | DataBuffer buffer = DefaultDataBufferFactory.sharedInstance.wrap(bytes);
|
@@ -277,4 +295,24 @@ public WebHandler webHandler() {
|
277 | 295 | }
|
278 | 296 | }
|
279 | 297 |
|
| 298 | + @Configuration |
| 299 | + static class ObservationConfig { |
| 300 | + |
| 301 | + @Bean |
| 302 | + public TestObservationRegistry testObservationRegistry() { |
| 303 | + return TestObservationRegistry.create(); |
| 304 | + } |
| 305 | + |
| 306 | + @Bean |
| 307 | + public ServerRequestObservationConvention requestObservationConvention() { |
| 308 | + return new DefaultServerRequestObservationConvention(); |
| 309 | + } |
| 310 | + |
| 311 | + @Bean |
| 312 | + public WebHandler webHandler() { |
| 313 | + return exchange -> exchange.getResponse().setComplete(); |
| 314 | + } |
| 315 | + |
| 316 | + } |
| 317 | + |
280 | 318 | }
|
0 commit comments