Skip to content

Exclude AutoConfiguredCompositeMeterRegistry from global registry #45536

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2024 the original author or authors.
* Copyright 2012-2025 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.
Expand Down Expand Up @@ -39,6 +39,7 @@
* @author Jon Schneider
* @author Phillip Webb
* @author Andy Wilkinson
* @author Yanming Zhou
*/
class MeterRegistryPostProcessor implements BeanPostProcessor, SmartInitializingSingleton {

Expand Down Expand Up @@ -116,7 +117,8 @@ private void applyFilters(MeterRegistry meterRegistry) {
}

private void addToGlobalRegistryIfNecessary(MeterRegistry meterRegistry) {
if (this.properties.getObject().isUseGlobalRegistry() && !isGlobalRegistry(meterRegistry)) {
if (this.properties.getObject().isUseGlobalRegistry() && !isGlobalRegistry(meterRegistry)
&& !isAutoConfiguredComposite(meterRegistry)) {
Metrics.addRegistry(meterRegistry);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2025 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.
Expand All @@ -21,6 +21,7 @@
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Metrics;
import io.micrometer.core.instrument.binder.MeterBinder;
import io.micrometer.core.instrument.composite.CompositeMeterRegistry;
import org.junit.jupiter.api.Test;
Expand All @@ -45,6 +46,7 @@
* {@link MetricsAutoConfiguration}.
*
* @author Jon Schneider
* @author Yanming Zhou
*/
class MetricsAutoConfigurationMeterRegistryPostProcessorIntegrationTests {

Expand Down Expand Up @@ -104,6 +106,20 @@ void counterIsIncrementedOncePerEventWithCompositeMeterRegistry() {
});
}

@Test
void excludeAutoConfiguredCompositeFromGlobalRegistry() {
this.contextRunner.withConfiguration(AutoConfigurations.of(MetricsAutoConfiguration.class))
.withPropertyValues("management.metrics.use-global-registry=true")
.run((context) -> {
assertThat(context).hasSingleBean(AutoConfiguredCompositeMeterRegistry.class);
assertThat(Metrics.globalRegistry.getRegistries()).isNotEmpty();
assertThat(Metrics.globalRegistry.getRegistries()
.stream()
.filter(AutoConfiguredCompositeMeterRegistry.class::isInstance)
.findAny()).isEmpty();
});
}

@Configuration(proxyBeanMethods = false)
static class TestConfiguration {

Expand Down