Skip to content

Commit c89576d

Browse files
berry120wilkinsona
authored andcommitted
Stop closing parent's MeterRegistry beans when child context closes
See gh-48319 Signed-off-by: Michael Berry <berry120@gmail.com>
1 parent 06055f4 commit c89576d

File tree

2 files changed

+26
-13
lines changed

2 files changed

+26
-13
lines changed

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

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
package org.springframework.boot.actuate.autoconfigure.metrics;
1818

19-
import java.util.List;
20-
2119
import io.micrometer.core.annotation.Timed;
2220
import io.micrometer.core.instrument.Clock;
2321
import io.micrometer.core.instrument.MeterRegistry;
@@ -42,6 +40,7 @@
4240
* @author Jon Schneider
4341
* @author Stephane Nicoll
4442
* @author Moritz Halbritter
43+
* @author Michael Berry
4544
* @since 2.0.0
4645
*/
4746
@AutoConfiguration(before = CompositeMeterRegistryAutoConfiguration.class)
@@ -71,8 +70,8 @@ public PropertiesMeterFilter propertiesMeterFilter(MetricsProperties properties)
7170
}
7271

7372
@Bean
74-
MeterRegistryCloser meterRegistryCloser(ObjectProvider<MeterRegistry> meterRegistries) {
75-
return new MeterRegistryCloser(meterRegistries.orderedStream().toList());
73+
MeterRegistryCloser meterRegistryCloser() {
74+
return new MeterRegistryCloser();
7675
}
7776

7877
/**
@@ -81,17 +80,15 @@ MeterRegistryCloser meterRegistryCloser(ObjectProvider<MeterRegistry> meterRegis
8180
*/
8281
static class MeterRegistryCloser implements ApplicationListener<ContextClosedEvent> {
8382

84-
private final List<MeterRegistry> meterRegistries;
85-
86-
MeterRegistryCloser(List<MeterRegistry> meterRegistries) {
87-
this.meterRegistries = meterRegistries;
88-
}
89-
9083
@Override
9184
public void onApplicationEvent(ContextClosedEvent event) {
92-
for (MeterRegistry meterRegistry : this.meterRegistries) {
93-
if (!meterRegistry.isClosed()) {
94-
meterRegistry.close();
85+
if (event.getApplicationContext().getParent() == null) {
86+
for (MeterRegistry meterRegistry : event.getApplicationContext()
87+
.getBeansOfType(MeterRegistry.class)
88+
.values()) {
89+
if (!meterRegistry.isClosed()) {
90+
meterRegistry.close();
91+
}
9592
}
9693
}
9794
}

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
3131
import org.springframework.context.annotation.Bean;
3232
import org.springframework.context.annotation.Configuration;
33+
import org.springframework.context.support.GenericApplicationContext;
3334
import org.springframework.core.annotation.Order;
3435
import org.springframework.test.util.ReflectionTestUtils;
3536

@@ -42,6 +43,7 @@
4243
*
4344
* @author Andy Wilkinson
4445
* @author Moritz Halbritter
46+
* @author Michael Berry
4547
*/
4648
class MetricsAutoConfigurationTests {
4749

@@ -89,6 +91,20 @@ void meterRegistryCloserShouldCloseRegistryOnShutdown() {
8991
});
9092
}
9193

94+
@Test
95+
void meterRegistryCloserShouldNotCloseOnNonRootContextClosing() {
96+
this.contextRunner.withUserConfiguration(MeterRegistryConfiguration.class).run((context) -> {
97+
MeterRegistry meterRegistry = context.getBean(MeterRegistry.class);
98+
GenericApplicationContext childContext = new GenericApplicationContext();
99+
childContext.setParent(context);
100+
childContext.refresh();
101+
childContext.close();
102+
assertThat(meterRegistry.isClosed()).isFalse();
103+
context.close();
104+
assertThat(meterRegistry.isClosed()).isTrue();
105+
});
106+
}
107+
92108
@Configuration(proxyBeanMethods = false)
93109
static class CustomClockConfiguration {
94110

0 commit comments

Comments
 (0)