Skip to content

Commit 69f5bf3

Browse files
committed
Exclude AutoConfiguredCompositeMeterRegistry from global registry
Signed-off-by: Yanming Zhou <[email protected]>
1 parent 711c53d commit 69f5bf3

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/MeterRegistryPostProcessor.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -39,6 +39,7 @@
3939
* @author Jon Schneider
4040
* @author Phillip Webb
4141
* @author Andy Wilkinson
42+
* @author Yanming Zhou
4243
*/
4344
class MeterRegistryPostProcessor implements BeanPostProcessor, SmartInitializingSingleton {
4445

@@ -116,7 +117,8 @@ private void applyFilters(MeterRegistry meterRegistry) {
116117
}
117118

118119
private void addToGlobalRegistryIfNecessary(MeterRegistry meterRegistry) {
119-
if (this.properties.getObject().isUseGlobalRegistry() && !isGlobalRegistry(meterRegistry)) {
120+
if (this.properties.getObject().isUseGlobalRegistry() && !isGlobalRegistry(meterRegistry)
121+
&& !isAutoConfiguredComposite(meterRegistry)) {
120122
Metrics.addRegistry(meterRegistry);
121123
}
122124
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/MetricsAutoConfigurationMeterRegistryPostProcessorIntegrationTests.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,6 +21,7 @@
2121
import ch.qos.logback.classic.Logger;
2222
import ch.qos.logback.classic.LoggerContext;
2323
import io.micrometer.core.instrument.MeterRegistry;
24+
import io.micrometer.core.instrument.Metrics;
2425
import io.micrometer.core.instrument.binder.MeterBinder;
2526
import io.micrometer.core.instrument.composite.CompositeMeterRegistry;
2627
import org.junit.jupiter.api.Test;
@@ -45,6 +46,7 @@
4546
* {@link MetricsAutoConfiguration}.
4647
*
4748
* @author Jon Schneider
49+
* @author Yanming Zhou
4850
*/
4951
class MetricsAutoConfigurationMeterRegistryPostProcessorIntegrationTests {
5052

@@ -104,6 +106,20 @@ void counterIsIncrementedOncePerEventWithCompositeMeterRegistry() {
104106
});
105107
}
106108

109+
@Test
110+
void excludeAutoConfiguredCompositeFromGlobalRegistry() {
111+
this.contextRunner.withConfiguration(AutoConfigurations.of(MetricsAutoConfiguration.class))
112+
.withPropertyValues("management.metrics.use-global-registry=true")
113+
.run((context) -> {
114+
assertThat(context).hasSingleBean(AutoConfiguredCompositeMeterRegistry.class);
115+
assertThat(Metrics.globalRegistry.getRegistries()).isNotEmpty();
116+
assertThat(Metrics.globalRegistry.getRegistries()
117+
.stream()
118+
.filter(AutoConfiguredCompositeMeterRegistry.class::isInstance)
119+
.findAny()).isEmpty();
120+
});
121+
}
122+
107123
@Configuration(proxyBeanMethods = false)
108124
static class TestConfiguration {
109125

0 commit comments

Comments
 (0)