From 78a64d7f6118041f35120f028e416241ec398583 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Wed, 21 Sep 2022 11:14:00 +0100 Subject: [PATCH] Allow DefaultMeterObservationHandler to be replaced by user's bean Closes gh-32462 Co-authored-by: Jonatan Ivanov --- ...nHandlerObservationRegistryCustomizer.java | 43 ------------------ .../ObservationAutoConfiguration.java | 8 ++-- ...lerObservationRegistryCustomizerTests.java | 45 ------------------- .../ObservationAutoConfigurationTests.java | 12 ++--- 4 files changed, 9 insertions(+), 99 deletions(-) delete mode 100644 spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/observation/DefaultMeterObservationHandlerObservationRegistryCustomizer.java delete mode 100644 spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/observation/DefaultMeterObservationHandlerObservationRegistryCustomizerTests.java diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/observation/DefaultMeterObservationHandlerObservationRegistryCustomizer.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/observation/DefaultMeterObservationHandlerObservationRegistryCustomizer.java deleted file mode 100644 index 10cdf597c7..0000000000 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/observation/DefaultMeterObservationHandlerObservationRegistryCustomizer.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright 2012-2022 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.boot.actuate.autoconfigure.observation; - -import io.micrometer.core.instrument.MeterRegistry; -import io.micrometer.core.instrument.observation.DefaultMeterObservationHandler; -import io.micrometer.observation.ObservationRegistry; - -/** - * Registers the {@link DefaultMeterObservationHandler} with an - * {@link ObservationRegistry}. - * - * @author Moritz Halbritter - */ -class DefaultMeterObservationHandlerObservationRegistryCustomizer - implements ObservationRegistryCustomizer { - - private final MeterRegistry meterRegistry; - - DefaultMeterObservationHandlerObservationRegistryCustomizer(MeterRegistry meterRegistry) { - this.meterRegistry = meterRegistry; - } - - @Override - public void customize(ObservationRegistry registry) { - registry.observationConfig().observationHandler(new DefaultMeterObservationHandler(this.meterRegistry)); - } - -} diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/observation/ObservationAutoConfiguration.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/observation/ObservationAutoConfiguration.java index 91658fb348..d21e58e9f7 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/observation/ObservationAutoConfiguration.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/observation/ObservationAutoConfiguration.java @@ -17,6 +17,8 @@ package org.springframework.boot.actuate.autoconfigure.observation; import io.micrometer.core.instrument.MeterRegistry; +import io.micrometer.core.instrument.observation.DefaultMeterObservationHandler; +import io.micrometer.core.instrument.observation.MeterObservationHandler; import io.micrometer.observation.GlobalObservationConvention; import io.micrometer.observation.ObservationHandler; import io.micrometer.observation.ObservationPredicate; @@ -66,9 +68,9 @@ public class ObservationAutoConfiguration { static class MetricsConfiguration { @Bean - DefaultMeterObservationHandlerObservationRegistryCustomizer enableDefaultMeterObservationHandler( - MeterRegistry meterRegistry) { - return new DefaultMeterObservationHandlerObservationRegistryCustomizer(meterRegistry); + @ConditionalOnMissingBean(MeterObservationHandler.class) + DefaultMeterObservationHandler defaultMeterObservationHandler(MeterRegistry meterRegistry) { + return new DefaultMeterObservationHandler(meterRegistry); } } diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/observation/DefaultMeterObservationHandlerObservationRegistryCustomizerTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/observation/DefaultMeterObservationHandlerObservationRegistryCustomizerTests.java deleted file mode 100644 index 48fb1e141d..0000000000 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/observation/DefaultMeterObservationHandlerObservationRegistryCustomizerTests.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright 2012-2022 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.boot.actuate.autoconfigure.observation; - -import io.micrometer.core.instrument.MeterRegistry; -import io.micrometer.core.instrument.simple.SimpleMeterRegistry; -import io.micrometer.observation.Observation; -import io.micrometer.observation.ObservationRegistry; -import org.junit.jupiter.api.Test; - -import static org.assertj.core.api.Assertions.assertThat; - -/** - * Tests for {@link DefaultMeterObservationHandlerObservationRegistryCustomizer}. - * - * @author Moritz Halbritter - */ -class DefaultMeterObservationHandlerObservationRegistryCustomizerTests { - - @Test - void customizeInstallsDefaultMeterObservationHandler() { - MeterRegistry meterRegistry = new SimpleMeterRegistry(); - DefaultMeterObservationHandlerObservationRegistryCustomizer sut = new DefaultMeterObservationHandlerObservationRegistryCustomizer( - meterRegistry); - ObservationRegistry observationRegistry = ObservationRegistry.create(); - sut.customize(observationRegistry); - Observation.start("test-1", observationRegistry).stop(); - assertThat(meterRegistry.find("test-1").timer().count()).isEqualTo(1); - } - -} diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/observation/ObservationAutoConfigurationTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/observation/ObservationAutoConfigurationTests.java index 72250b0e02..b10d7d71d9 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/observation/ObservationAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/observation/ObservationAutoConfigurationTests.java @@ -22,6 +22,7 @@ import java.util.List; import io.micrometer.common.KeyValue; import io.micrometer.common.KeyValues; import io.micrometer.core.instrument.MeterRegistry; +import io.micrometer.core.instrument.observation.DefaultMeterObservationHandler; import io.micrometer.core.instrument.observation.MeterObservationHandler; import io.micrometer.core.instrument.search.MeterNotFoundException; import io.micrometer.observation.GlobalObservationConvention; @@ -67,6 +68,7 @@ class ObservationAutoConfigurationTests { @Test void autoConfiguresDefaultMeterObservationHandler() { this.contextRunner.run((context) -> { + assertThat(context).hasSingleBean(DefaultMeterObservationHandler.class); ObservationRegistry observationRegistry = context.getBean(ObservationRegistry.class); Observation.start("test-observation", observationRegistry).stop(); // When a DefaultMeterObservationHandler is registered, every stopped @@ -76,13 +78,6 @@ class ObservationAutoConfigurationTests { }); } - @Test - void allowsDefaultMeterObservationHandlerToBeDisabled() { - this.contextRunner.withClassLoader(new FilteredClassLoader(MeterRegistry.class)) - .run((context) -> assertThat(context) - .doesNotHaveBean(DefaultMeterObservationHandlerObservationRegistryCustomizer.class)); - } - @Test void autoConfiguresObservationPredicates() { this.contextRunner.withUserConfiguration(ObservationPredicates.class).run((context) -> { @@ -109,11 +104,12 @@ class ObservationAutoConfigurationTests { } @Test - void autoConfiguresObservationHandler() { + void autoConfiguresObservationHandlers() { this.contextRunner.withUserConfiguration(ObservationHandlers.class).run((context) -> { ObservationRegistry observationRegistry = context.getBean(ObservationRegistry.class); List> handlers = context.getBean(CalledHandlers.class).getCalledHandlers(); Observation.start("test-observation", observationRegistry); + assertThat(context).doesNotHaveBean(DefaultMeterObservationHandler.class); assertThat(handlers).hasSize(2); // Regular handlers are registered first assertThat(handlers.get(0)).isInstanceOf(CustomObservationHandler.class);