Allow DefaultMeterObservationHandler to be replaced by user's bean

Closes gh-32462

Co-authored-by: Jonatan Ivanov <jonatan.ivanov@gmail.com>
pull/31255/head
Andy Wilkinson 2 years ago
parent 4a80b36160
commit 78a64d7f61

@ -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<ObservationRegistry> {
private final MeterRegistry meterRegistry;
DefaultMeterObservationHandlerObservationRegistryCustomizer(MeterRegistry meterRegistry) {
this.meterRegistry = meterRegistry;
}
@Override
public void customize(ObservationRegistry registry) {
registry.observationConfig().observationHandler(new DefaultMeterObservationHandler(this.meterRegistry));
}
}

@ -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);
}
}

@ -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);
}
}

@ -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<ObservationHandler<?>> 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);

Loading…
Cancel
Save