Skip to content

Commit daf33fc

Browse files
committed
Closing a child context no longer shuts down MeterRegistry instances\n\nSigned-off-by: Michael Berry <berry120@gmail.com>
1 parent c3e51d3 commit daf33fc

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

module/spring-boot-micrometer-metrics/src/main/java/org/springframework/boot/micrometer/metrics/autoconfigure/MetricsAutoConfiguration.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
* @author Stephane Nicoll
4949
* @author Moritz Halbritter
5050
* @author Phillip Webb
51+
* @author Michael Berry
5152
* @since 4.0.0
5253
*/
5354
@AutoConfiguration(before = CompositeMeterRegistryAutoConfiguration.class)
@@ -108,9 +109,11 @@ static class MeterRegistryCloser implements ApplicationListener<ContextClosedEve
108109

109110
@Override
110111
public void onApplicationEvent(ContextClosedEvent event) {
111-
for (MeterRegistry meterRegistry : this.meterRegistries) {
112-
if (!meterRegistry.isClosed()) {
113-
meterRegistry.close();
112+
if(event.getApplicationContext().getParent() == null) {
113+
for (MeterRegistry meterRegistry : this.meterRegistries) {
114+
if (!meterRegistry.isClosed()) {
115+
meterRegistry.close();
116+
}
114117
}
115118
}
116119
}

module/spring-boot-micrometer-metrics/src/test/java/org/springframework/boot/micrometer/metrics/autoconfigure/MetricsAutoConfigurationTests.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,12 @@
3131
import org.springframework.boot.autoconfigure.AutoConfigurations;
3232
import org.springframework.boot.micrometer.metrics.autoconfigure.MetricsAutoConfiguration.MeterRegistryCloser;
3333
import org.springframework.boot.micrometer.observation.autoconfigure.ObservationHandlerGroup;
34+
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
3435
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
36+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
3537
import org.springframework.context.annotation.Bean;
3638
import org.springframework.context.annotation.Configuration;
39+
import org.springframework.context.support.GenericApplicationContext;
3740
import org.springframework.core.annotation.Order;
3841
import org.springframework.test.util.ReflectionTestUtils;
3942

@@ -47,6 +50,7 @@
4750
* @author Andy Wilkinson
4851
* @author Moritz Halbritter
4952
* @author Phillip Webb
53+
* @author Michael Berry
5054
*/
5155
class MetricsAutoConfigurationTests {
5256

@@ -95,6 +99,21 @@ void meterRegistryCloserShouldCloseRegistryOnShutdown() {
9599
});
96100
}
97101

102+
@Test
103+
void meterRegistryCloserShouldNotCloseOnNonRootContextClosing() {
104+
this.contextRunner.withUserConfiguration(MeterRegistryConfiguration.class).run((context) -> {
105+
MeterRegistry meterRegistry = context.getBean(MeterRegistry.class);
106+
GenericApplicationContext childContext = new GenericApplicationContext();
107+
childContext.setParent(context);
108+
childContext.refresh();
109+
childContext.close();
110+
assertThat(meterRegistry.isClosed()).isFalse();
111+
context.close();
112+
assertThat(meterRegistry.isClosed()).isTrue();
113+
});
114+
}
115+
116+
98117
@Test
99118
void supplyHandlerAndGroup() {
100119
this.contextRunner.run((context) -> {

0 commit comments

Comments
 (0)