Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright 2012-present 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.test.metrics;

import java.util.List;

import org.jspecify.annotations.Nullable;

import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.test.context.ContextConfigurationAttributes;
import org.springframework.test.context.ContextCustomizer;
import org.springframework.test.context.ContextCustomizerFactory;
import org.springframework.test.context.MergedContextConfiguration;
import org.springframework.util.ClassUtils;

/**
* {@link ContextCustomizerFactory} to disable the use of Micrometer's
* {@link io.micrometer.core.instrument.Metrics#globalRegistry global registry} in tests,
* preventing {@link io.micrometer.core.instrument.MeterRegistry meter registries} from
* pinning application contexts when many test contexts are cached.
*/
class DisableGlobalMeterRegistryContextCustomizerFactory implements ContextCustomizerFactory {

private static final String METRICS_CLASS = "io.micrometer.core.instrument.Metrics";

private static final String USE_GLOBAL_REGISTRY_PROPERTY = "management.metrics.use-global-registry";

@Override
public @Nullable ContextCustomizer createContextCustomizer(Class<?> testClass,
List<ContextConfigurationAttributes> configAttributes) {
if (ClassUtils.isPresent(METRICS_CLASS, testClass.getClassLoader())) {
return new DisableGlobalMeterRegistryContextCustomizer();
}
return null;
}

static final class DisableGlobalMeterRegistryContextCustomizer implements ContextCustomizer {

@Override
public void customizeContext(ConfigurableApplicationContext context, MergedContextConfiguration mergedConfig) {
ConfigurableEnvironment environment = context.getEnvironment();
if (environment.getProperty(USE_GLOBAL_REGISTRY_PROPERTY) == null) {
TestPropertyValues.of(USE_GLOBAL_REGISTRY_PROPERTY + "=false").applyTo(environment);
}
}

@Override
public boolean equals(@Nullable Object obj) {
return obj instanceof DisableGlobalMeterRegistryContextCustomizer;
}

@Override
public int hashCode() {
return getClass().hashCode();
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2012-present 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.
*/

/**
* Spring Boot support for metrics testing.
*/
@NullMarked
package org.springframework.boot.test.metrics;

import org.jspecify.annotations.NullMarked;
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ org.springframework.boot.test.context.PropertyMappingContextCustomizerFactory,\
org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizerFactory,\
org.springframework.boot.test.context.filter.annotation.TypeExcludeFiltersContextCustomizerFactory,\
org.springframework.boot.test.http.client.DisableReactorResourceFactoryGlobalResourcesContextCustomizerFactory,\
org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory
org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory,\
org.springframework.boot.test.metrics.DisableGlobalMeterRegistryContextCustomizerFactory

# Application Context Initializers
org.springframework.context.ApplicationContextInitializer=\
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright 2012-present 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.test.metrics;

import org.junit.jupiter.api.Test;
import org.springframework.test.context.MergedContextConfiguration;

import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.env.StandardEnvironment;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;

/**
* Tests for {@link DisableGlobalMeterRegistryContextCustomizerFactory}.
*/
class DisableGlobalMeterRegistryContextCustomizerFactoryTests {

@Test
void disablesGlobalMeterRegistryByDefault() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.setEnvironment(new StandardEnvironment());
new DisableGlobalMeterRegistryContextCustomizerFactory.DisableGlobalMeterRegistryContextCustomizer()
.customizeContext(context, mock(MergedContextConfiguration.class));
assertThat(context.getEnvironment().getProperty("management.metrics.use-global-registry"))
.isEqualTo("false");
}

@Test
void doesNotOverrideExplicitProperty() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
StandardEnvironment environment = new StandardEnvironment();
TestPropertyValues.of("management.metrics.use-global-registry=true").applyTo(environment);
context.setEnvironment(environment);
new DisableGlobalMeterRegistryContextCustomizerFactory.DisableGlobalMeterRegistryContextCustomizer()
.customizeContext(context, mock(MergedContextConfiguration.class));
assertThat(context.getEnvironment().getProperty("management.metrics.use-global-registry"))
.isEqualTo("true");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -54,25 +54,31 @@ class MeterRegistryPostProcessor implements BeanPostProcessor, SmartInitializing

private final ObjectProvider<MeterBinder> binders;

private final ObjectProvider<MetricsAutoConfiguration.MeterRegistryCloser> meterRegistryCloser;

private volatile boolean deferBinding = true;

private final Set<MeterRegistry> deferredBindings = new LinkedHashSet<>();

MeterRegistryPostProcessor(ApplicationContext applicationContext,
ObjectProvider<MetricsProperties> metricsProperties, ObjectProvider<MeterRegistryCustomizer<?>> customizers,
ObjectProvider<MeterFilter> filters, ObjectProvider<MeterBinder> binders) {
this(CompositeMeterRegistries.of(applicationContext), metricsProperties, customizers, filters, binders);
ObjectProvider<MetricsProperties> metricsProperties,
ObjectProvider<MeterRegistryCustomizer<?>> customizers, ObjectProvider<MeterFilter> filters,
ObjectProvider<MeterBinder> binders,
ObjectProvider<MetricsAutoConfiguration.MeterRegistryCloser> meterRegistryCloser) {
this(CompositeMeterRegistries.of(applicationContext), metricsProperties, customizers, filters, binders,
meterRegistryCloser);
}

MeterRegistryPostProcessor(CompositeMeterRegistries compositeMeterRegistries,
ObjectProvider<MetricsProperties> properties, ObjectProvider<MeterRegistryCustomizer<?>> customizers,
ObjectProvider<MeterFilter> filters, ObjectProvider<MeterBinder> binders) {
ObjectProvider<MeterFilter> filters, ObjectProvider<MeterBinder> binders,
ObjectProvider<MetricsAutoConfiguration.MeterRegistryCloser> meterRegistryCloser) {
this.compositeMeterRegistries = compositeMeterRegistries;
this.properties = properties;
this.customizers = customizers;
this.filters = filters;
this.binders = binders;

this.meterRegistryCloser = meterRegistryCloser;
}

@Override
Expand Down Expand Up @@ -123,6 +129,7 @@ private void applyFilters(MeterRegistry meterRegistry) {
private void addToGlobalRegistryIfNecessary(MeterRegistry meterRegistry) {
if (this.properties.getObject().isUseGlobalRegistry() && !isGlobalRegistry(meterRegistry)) {
Metrics.addRegistry(meterRegistry);
this.meterRegistryCloser.getObject().track(meterRegistry);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

package org.springframework.boot.micrometer.metrics.autoconfigure;

import java.util.LinkedHashSet;
import java.util.Set;

import io.micrometer.core.annotation.Timed;
import io.micrometer.core.instrument.Clock;
import io.micrometer.core.instrument.MeterRegistry;
Expand Down Expand Up @@ -65,9 +68,10 @@ Clock micrometerClock() {
static MeterRegistryPostProcessor meterRegistryPostProcessor(ApplicationContext applicationContext,
ObjectProvider<MetricsProperties> metricsProperties,
ObjectProvider<MeterRegistryCustomizer<?>> meterRegistryCustomizers,
ObjectProvider<MeterFilter> meterFilters, ObjectProvider<MeterBinder> meterBinders) {
ObjectProvider<MeterFilter> meterFilters, ObjectProvider<MeterBinder> meterBinders,
ObjectProvider<MeterRegistryCloser> meterRegistryCloser) {
return new MeterRegistryPostProcessor(applicationContext, metricsProperties, meterRegistryCustomizers,
meterFilters, meterBinders);
meterFilters, meterBinders, meterRegistryCloser);
}

@Bean
Expand Down Expand Up @@ -102,20 +106,25 @@ static class MeterRegistryCloser implements ApplicationListener<ContextClosedEve

private final ApplicationContext context;

private final Iterable<MeterRegistry> meterRegistries;

private final boolean useGlobalRegistry;

private final Set<MeterRegistry> trackedRegistries = new LinkedHashSet<>();

MeterRegistryCloser(ApplicationContext context, boolean useGlobalRegistry) {
this.meterRegistries = context.getBeansOfType(MeterRegistry.class).values();
this.context = context;
this.useGlobalRegistry = useGlobalRegistry;
}

void track(MeterRegistry meterRegistry) {
this.trackedRegistries.add(meterRegistry);
}

@Override
public void onApplicationEvent(ContextClosedEvent event) {
if (this.context.equals(event.getApplicationContext())) {
for (MeterRegistry meterRegistry : this.meterRegistries) {
Set<MeterRegistry> meterRegistries = new LinkedHashSet<>(this.trackedRegistries);
meterRegistries.addAll(this.context.getBeansOfType(MeterRegistry.class).values());
for (MeterRegistry meterRegistry : meterRegistries) {
if (this.useGlobalRegistry) {
Metrics.globalRegistry.remove(meterRegistry);
}
Expand Down
Loading