Skip to content

Commit e65baba

Browse files
committed
Polish "Stop closing parent's MeterRegistry beans when child context closes"
See gh-48319
1 parent c89576d commit e65baba

File tree

2 files changed

+27
-17
lines changed

2 files changed

+27
-17
lines changed

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

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ public PropertiesMeterFilter propertiesMeterFilter(MetricsProperties properties)
7070
}
7171

7272
@Bean
73-
MeterRegistryCloser meterRegistryCloser() {
74-
return new MeterRegistryCloser();
73+
MeterRegistryCloser meterRegistryCloser(ApplicationContext context) {
74+
return new MeterRegistryCloser(context);
7575
}
7676

7777
/**
@@ -80,12 +80,19 @@ MeterRegistryCloser meterRegistryCloser() {
8080
*/
8181
static class MeterRegistryCloser implements ApplicationListener<ContextClosedEvent> {
8282

83+
private final ApplicationContext context;
84+
85+
private final Iterable<MeterRegistry> meterRegistries;
86+
87+
MeterRegistryCloser(ApplicationContext context) {
88+
this.meterRegistries = context.getBeansOfType(MeterRegistry.class).values();
89+
this.context = context;
90+
}
91+
8392
@Override
8493
public void onApplicationEvent(ContextClosedEvent event) {
85-
if (event.getApplicationContext().getParent() == null) {
86-
for (MeterRegistry meterRegistry : event.getApplicationContext()
87-
.getBeansOfType(MeterRegistry.class)
88-
.values()) {
94+
if (this.context.equals(event.getApplicationContext())) {
95+
for (MeterRegistry meterRegistry : this.meterRegistries) {
8996
if (!meterRegistry.isClosed()) {
9097
meterRegistry.close();
9198
}

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

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
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;
3433
import org.springframework.core.annotation.Order;
3534
import org.springframework.test.util.ReflectionTestUtils;
3635

@@ -92,17 +91,21 @@ void meterRegistryCloserShouldCloseRegistryOnShutdown() {
9291
}
9392

9493
@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();
94+
void meterRegistryCloserShouldOnlyCloseRegistriesBelongingToContextBeingClosed() {
95+
MeterRegistry parentMeterRegistry = new SimpleMeterRegistry();
96+
MeterRegistry childMeterRegistry = new SimpleMeterRegistry();
97+
this.contextRunner.withBean(MeterRegistry.class, () -> parentMeterRegistry).run((parent) -> {
98+
this.contextRunner.withBean(MeterRegistry.class, () -> childMeterRegistry)
99+
.withParent(parent)
100+
.run((child) -> {
101+
assertThat(childMeterRegistry.isClosed()).isFalse();
102+
assertThat(parentMeterRegistry.isClosed()).isFalse();
103+
});
104+
assertThat(childMeterRegistry.isClosed()).isTrue();
105+
assertThat(parentMeterRegistry.isClosed()).isFalse();
105106
});
107+
assertThat(childMeterRegistry.isClosed()).isTrue();
108+
assertThat(parentMeterRegistry.isClosed()).isTrue();
106109
}
107110

108111
@Configuration(proxyBeanMethods = false)

0 commit comments

Comments
 (0)