|
| 1 | +/* |
| 2 | + * Copyright 2012-2023 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.actuate.autoconfigure.observation.web.reactive; |
| 18 | + |
| 19 | +import io.micrometer.observation.ObservationPredicate; |
| 20 | +import io.micrometer.observation.ObservationRegistry; |
| 21 | +import io.micrometer.observation.tck.TestObservationRegistry; |
| 22 | +import io.micrometer.observation.tck.TestObservationRegistryAssert; |
| 23 | +import org.junit.jupiter.api.AfterEach; |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | + |
| 26 | +import org.springframework.beans.factory.annotation.Autowired; |
| 27 | +import org.springframework.boot.actuate.autoconfigure.endpoint.EndpointAutoConfiguration; |
| 28 | +import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointAutoConfiguration; |
| 29 | +import org.springframework.boot.actuate.autoconfigure.endpoint.web.reactive.WebFluxEndpointManagementContextConfiguration; |
| 30 | +import org.springframework.boot.actuate.autoconfigure.info.InfoEndpointAutoConfiguration; |
| 31 | +import org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration; |
| 32 | +import org.springframework.boot.actuate.autoconfigure.metrics.web.TestController; |
| 33 | +import org.springframework.boot.actuate.autoconfigure.observation.ObservationAutoConfiguration; |
| 34 | +import org.springframework.boot.actuate.autoconfigure.observation.web.reactive.WebFluxObservationAutoConfigurationIntegrationTests.TestApp; |
| 35 | +import org.springframework.boot.autoconfigure.ImportAutoConfiguration; |
| 36 | +import org.springframework.boot.autoconfigure.web.reactive.HttpHandlerAutoConfiguration; |
| 37 | +import org.springframework.boot.autoconfigure.web.reactive.ReactiveWebServerFactoryAutoConfiguration; |
| 38 | +import org.springframework.boot.autoconfigure.web.reactive.WebFluxAutoConfiguration; |
| 39 | +import org.springframework.boot.test.context.SpringBootTest; |
| 40 | +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; |
| 41 | +import org.springframework.boot.test.web.client.TestRestTemplate; |
| 42 | +import org.springframework.context.ApplicationContext; |
| 43 | +import org.springframework.context.annotation.Bean; |
| 44 | +import org.springframework.context.annotation.Configuration; |
| 45 | +import org.springframework.http.HttpStatus; |
| 46 | + |
| 47 | +import static org.assertj.core.api.Assertions.assertThat; |
| 48 | + |
| 49 | +/** |
| 50 | + * Integration tests for {@link WebFluxObservationAutoConfiguration}. Due to limitations |
| 51 | + * in {@code WebTestClient}, these tests need to start a real webserver and utilize a real |
| 52 | + * http client. |
| 53 | + * |
| 54 | + * @author Jonatan Ivanov |
| 55 | + */ |
| 56 | +@SpringBootTest( |
| 57 | + properties = { "spring.main.web-application-type=reactive", "management.endpoints.web.exposure.include=info" }, |
| 58 | + webEnvironment = WebEnvironment.RANDOM_PORT, classes = { TestApp.class, TestController.class }) |
| 59 | +class WebFluxObservationAutoConfigurationIntegrationTests { |
| 60 | + |
| 61 | + @Autowired |
| 62 | + private TestRestTemplate restTemplate; |
| 63 | + |
| 64 | + @Autowired |
| 65 | + private TestObservationRegistry observationRegistry; |
| 66 | + |
| 67 | + @Autowired |
| 68 | + private ApplicationContext context; |
| 69 | + |
| 70 | + @AfterEach |
| 71 | + void tearDown() { |
| 72 | + this.observationRegistry.clear(); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + void whenAnActuatorEndpointIsCalledObservationsShouldBeRecorded() { |
| 77 | + callUrlsToCreateObservations("/test0", "/actuator/info"); |
| 78 | + TestObservationRegistryAssert.assertThat(this.observationRegistry) |
| 79 | + .hasNumberOfObservationsWithNameEqualTo("http.server.requests", 2) |
| 80 | + .hasAnObservationWithAKeyValue("http.url", "/test0") |
| 81 | + .hasAnObservationWithAKeyValue("http.url", "/actuator/info"); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + void whenActuatorObservationsEnabledObservationsShouldBeRecorded() { |
| 86 | + // "management.observations.http.server.actuator.enabled=true" |
| 87 | + callUrlsToCreateObservations("/test0", "/actuator/info"); |
| 88 | + TestObservationRegistryAssert.assertThat(this.observationRegistry) |
| 89 | + .hasNumberOfObservationsWithNameEqualTo("http.server.requests", 2) |
| 90 | + .hasAnObservationWithAKeyValue("http.url", "/test0") |
| 91 | + .hasAnObservationWithAKeyValue("http.url", "/actuator/info"); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + void whenActuatorObservationsDisabledObservationsShouldNotBeRecorded() { |
| 96 | + // "management.observations.http.server.actuator.enabled=false" |
| 97 | + assertThat(this.context.getBean("actuatorWebEndpointObservationPredicate")) |
| 98 | + .isInstanceOf(ObservationPredicate.class); |
| 99 | + callUrlsToCreateObservations("/test0", "/actuator/info"); |
| 100 | + TestObservationRegistryAssert.assertThat(this.observationRegistry) |
| 101 | + .hasNumberOfObservationsWithNameEqualTo("http.server.requests", 1) |
| 102 | + .hasAnObservationWithAKeyValue("http.url", "/test0"); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + void whenActuatorObservationsDisabledObservationsShouldNotBeRecordedUsingCustomEndpointBasePath() { |
| 107 | + // "management.observations.http.server.actuator.enabled=false" |
| 108 | + // "management.endpoints.web.base-path=/management" |
| 109 | + assertThat(this.context.getBean("actuatorWebEndpointObservationPredicate")) |
| 110 | + .isInstanceOf(ObservationPredicate.class); |
| 111 | + callUrlsToCreateObservations("/test0", "/management/info"); |
| 112 | + TestObservationRegistryAssert.assertThat(this.observationRegistry) |
| 113 | + .hasNumberOfObservationsWithNameEqualTo("http.server.requests", 1) |
| 114 | + .hasAnObservationWithAKeyValue("http.url", "/test0"); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + void whenActuatorObservationsDisabledObservationsShouldNotBeRecordedUsingCustomWebfluxBasePath() { |
| 119 | + // "management.observations.http.server.actuator.enabled=false" |
| 120 | + // "spring.webflux.base-path=/test-path" |
| 121 | + assertThat(this.context.getBean("actuatorWebEndpointObservationPredicate")) |
| 122 | + .isInstanceOf(ObservationPredicate.class); |
| 123 | + callUrlsToCreateObservations("/test-path/test0", "/test-path/actuator/info"); |
| 124 | + TestObservationRegistryAssert.assertThat(this.observationRegistry) |
| 125 | + .hasNumberOfObservationsWithNameEqualTo("http.server.requests", 1) |
| 126 | + .hasAnObservationWithAKeyValue("http.url", "/test-path/test0"); |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + void whenActuatorObservationsDisabledObservationsShouldNotBeRecordedUsingCustomWebfluxBasePathAndCustomEndpointBasePath() { |
| 131 | + // "management.observations.http.server.actuator.enabled=false" |
| 132 | + // "spring.webflux.base-path=/test-path" |
| 133 | + // "management.endpoints.web.base-path=/management" |
| 134 | + assertThat(this.context.getBean("actuatorWebEndpointObservationPredicate")) |
| 135 | + .isInstanceOf(ObservationPredicate.class); |
| 136 | + callUrlsToCreateObservations("/test-path/test0", "/test-path/management/info"); |
| 137 | + TestObservationRegistryAssert.assertThat(this.observationRegistry) |
| 138 | + .hasNumberOfObservationsWithNameEqualTo("http.server.requests", 1) |
| 139 | + .hasAnObservationWithAKeyValue("http.url", "/test-path/test0"); |
| 140 | + } |
| 141 | + |
| 142 | + void callUrlsToCreateObservations(String... urls) { |
| 143 | + for (String url : urls) { |
| 144 | + assertThat(this.restTemplate.getForEntity(url, String.class).getStatusCode()).isEqualTo(HttpStatus.OK); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + @Configuration(proxyBeanMethods = false) |
| 149 | + @ImportAutoConfiguration({ ReactiveWebServerFactoryAutoConfiguration.class, WebFluxAutoConfiguration.class, |
| 150 | + InfoEndpointAutoConfiguration.class, HttpHandlerAutoConfiguration.class, EndpointAutoConfiguration.class, |
| 151 | + WebEndpointAutoConfiguration.class, WebFluxEndpointManagementContextConfiguration.class, |
| 152 | + MetricsAutoConfiguration.class, ObservationAutoConfiguration.class, |
| 153 | + WebFluxObservationAutoConfiguration.class }) |
| 154 | + static class TestApp { |
| 155 | + |
| 156 | + @Bean |
| 157 | + ObservationRegistry observationRegistry() { |
| 158 | + return TestObservationRegistry.create(); |
| 159 | + } |
| 160 | + |
| 161 | + } |
| 162 | + |
| 163 | +} |
0 commit comments