From 4dfd77040691e185efe06a86ea34df0504ab29e8 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Tue, 11 Oct 2022 20:30:40 +0100 Subject: [PATCH] Ensure that metrics and observation properties are enabled Closes gh-32676 --- ...tpClientObservationsAutoConfiguration.java | 2 ++ ...TemplateObservationConfigurationTests.java | 35 ++++++++++--------- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/web/client/HttpClientObservationsAutoConfiguration.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/web/client/HttpClientObservationsAutoConfiguration.java index ae3970478b..eb07f22382 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/web/client/HttpClientObservationsAutoConfiguration.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/web/client/HttpClientObservationsAutoConfiguration.java @@ -32,6 +32,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.web.client.RestTemplateAutoConfiguration; import org.springframework.boot.autoconfigure.web.reactive.function.client.WebClientAutoConfiguration; +import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; @@ -53,6 +54,7 @@ import org.springframework.core.annotation.Order; @ConditionalOnClass(Observation.class) @ConditionalOnBean(ObservationRegistry.class) @Import({ RestTemplateObservationConfiguration.class, WebClientObservationConfiguration.class }) +@EnableConfigurationProperties({ MetricsProperties.class, ObservationProperties.class }) public class HttpClientObservationsAutoConfiguration { @Configuration(proxyBeanMethods = false) diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/web/client/RestTemplateObservationConfigurationTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/web/client/RestTemplateObservationConfigurationTests.java index 64cba8f93d..7614b2341e 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/web/client/RestTemplateObservationConfigurationTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/web/client/RestTemplateObservationConfigurationTests.java @@ -57,7 +57,7 @@ import static org.springframework.test.web.client.response.MockRestResponseCreat @SuppressWarnings({ "deprecation", "removal" }) class RestTemplateObservationConfigurationTests { - private final ApplicationContextRunner contextRunner = new ApplicationContextRunner().with(MetricsRun.simple()) + private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() .withBean(ObservationRegistry.class, TestObservationRegistry::create) .withConfiguration(AutoConfigurations.of(ObservationAutoConfiguration.class, RestTemplateAutoConfiguration.class, HttpClientObservationsAutoConfiguration.class)); @@ -118,22 +118,23 @@ class RestTemplateObservationConfigurationTests { @Test void afterMaxUrisReachedFurtherUrisAreDenied(CapturedOutput output) { - this.contextRunner.withPropertyValues("management.metrics.web.client.max-uri-tags=2").run((context) -> { - - RestTemplate restTemplate = context.getBean(RestTemplateBuilder.class).build(); - MockRestServiceServer server = MockRestServiceServer.createServer(restTemplate); - for (int i = 0; i < 3; i++) { - server.expect(requestTo("/test/" + i)).andRespond(withStatus(HttpStatus.OK)); - } - for (int i = 0; i < 3; i++) { - restTemplate.getForObject("/test/" + i, String.class); - } - TestObservationRegistry registry = context.getBean(TestObservationRegistry.class); - TestObservationRegistryAssert.assertThat(registry); - // TODO: check observation count for name - assertThat(output).contains("Reached the maximum number of URI tags for 'http.client.requests'.") - .contains("Are you using 'uriVariables'?"); - }); + this.contextRunner.with(MetricsRun.simple()).withPropertyValues("management.metrics.web.client.max-uri-tags=2") + .run((context) -> { + + RestTemplate restTemplate = context.getBean(RestTemplateBuilder.class).build(); + MockRestServiceServer server = MockRestServiceServer.createServer(restTemplate); + for (int i = 0; i < 3; i++) { + server.expect(requestTo("/test/" + i)).andRespond(withStatus(HttpStatus.OK)); + } + for (int i = 0; i < 3; i++) { + restTemplate.getForObject("/test/" + i, String.class); + } + TestObservationRegistry registry = context.getBean(TestObservationRegistry.class); + TestObservationRegistryAssert.assertThat(registry); + // TODO: check observation count for name + assertThat(output).contains("Reached the maximum number of URI tags for 'http.client.requests'.") + .contains("Are you using 'uriVariables'?"); + }); } @Test