Skip to content

Commit 8becf59

Browse files
committed
Merge pull request #48319 from berry120
* gh-48319: Polish "Stop closing parent's MeterRegistry beans when child context closes" Stop closing parent's MeterRegistry beans when child context closes Closes gh-48319
2 parents 06055f4 + e65baba commit 8becf59

File tree

2 files changed

+33
-10
lines changed

2 files changed

+33
-10
lines changed

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

Lines changed: 14 additions & 10 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(ApplicationContext context) {
74+
return new MeterRegistryCloser(context);
7675
}
7776

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

84-
private final List<MeterRegistry> meterRegistries;
83+
private final ApplicationContext context;
84+
85+
private final Iterable<MeterRegistry> meterRegistries;
8586

86-
MeterRegistryCloser(List<MeterRegistry> meterRegistries) {
87-
this.meterRegistries = meterRegistries;
87+
MeterRegistryCloser(ApplicationContext context) {
88+
this.meterRegistries = context.getBeansOfType(MeterRegistry.class).values();
89+
this.context = context;
8890
}
8991

9092
@Override
9193
public void onApplicationEvent(ContextClosedEvent event) {
92-
for (MeterRegistry meterRegistry : this.meterRegistries) {
93-
if (!meterRegistry.isClosed()) {
94-
meterRegistry.close();
94+
if (this.context.equals(event.getApplicationContext())) {
95+
for (MeterRegistry meterRegistry : this.meterRegistries) {
96+
if (!meterRegistry.isClosed()) {
97+
meterRegistry.close();
98+
}
9599
}
96100
}
97101
}

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
*
4343
* @author Andy Wilkinson
4444
* @author Moritz Halbritter
45+
* @author Michael Berry
4546
*/
4647
class MetricsAutoConfigurationTests {
4748

@@ -89,6 +90,24 @@ void meterRegistryCloserShouldCloseRegistryOnShutdown() {
8990
});
9091
}
9192

93+
@Test
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();
106+
});
107+
assertThat(childMeterRegistry.isClosed()).isTrue();
108+
assertThat(parentMeterRegistry.isClosed()).isTrue();
109+
}
110+
92111
@Configuration(proxyBeanMethods = false)
93112
static class CustomClockConfiguration {
94113

0 commit comments

Comments
 (0)