Skip to content

Commit 7946f0e

Browse files
committed
Merge branch '3.5.x'
Closes spring-projectsgh-48325
2 parents c3e51d3 + 7f2ffca commit 7946f0e

File tree

2 files changed

+33
-10
lines changed

2 files changed

+33
-10
lines changed

module/spring-boot-micrometer-metrics/src/main/java/org/springframework/boot/micrometer/metrics/autoconfigure/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.micrometer.metrics.autoconfigure;
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;
@@ -47,6 +45,7 @@
4745
* @author Jon Schneider
4846
* @author Stephane Nicoll
4947
* @author Moritz Halbritter
48+
* @author Michael Berry
5049
* @author Phillip Webb
5150
* @since 4.0.0
5251
*/
@@ -77,8 +76,8 @@ PropertiesMeterFilter propertiesMeterFilter(MetricsProperties properties) {
7776
}
7877

7978
@Bean
80-
MeterRegistryCloser meterRegistryCloser(ObjectProvider<MeterRegistry> meterRegistries) {
81-
return new MeterRegistryCloser(meterRegistries.orderedStream().toList());
79+
MeterRegistryCloser meterRegistryCloser(ApplicationContext context) {
80+
return new MeterRegistryCloser(context);
8281
}
8382

8483
@Bean
@@ -100,17 +99,22 @@ DefaultMeterObservationHandler defaultMeterObservationHandler(ObjectProvider<Met
10099
*/
101100
static class MeterRegistryCloser implements ApplicationListener<ContextClosedEvent> {
102101

103-
private final List<MeterRegistry> meterRegistries;
102+
private final ApplicationContext context;
103+
104+
private final Iterable<MeterRegistry> meterRegistries;
104105

105-
MeterRegistryCloser(List<MeterRegistry> meterRegistries) {
106-
this.meterRegistries = meterRegistries;
106+
MeterRegistryCloser(ApplicationContext context) {
107+
this.meterRegistries = context.getBeansOfType(MeterRegistry.class).values();
108+
this.context = context;
107109
}
108110

109111
@Override
110112
public void onApplicationEvent(ContextClosedEvent event) {
111-
for (MeterRegistry meterRegistry : this.meterRegistries) {
112-
if (!meterRegistry.isClosed()) {
113-
meterRegistry.close();
113+
if (this.context.equals(event.getApplicationContext())) {
114+
for (MeterRegistry meterRegistry : this.meterRegistries) {
115+
if (!meterRegistry.isClosed()) {
116+
meterRegistry.close();
117+
}
114118
}
115119
}
116120
}

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
@@ -46,6 +46,7 @@
4646
*
4747
* @author Andy Wilkinson
4848
* @author Moritz Halbritter
49+
* @author Michael Berry
4950
* @author Phillip Webb
5051
*/
5152
class MetricsAutoConfigurationTests {
@@ -95,6 +96,24 @@ void meterRegistryCloserShouldCloseRegistryOnShutdown() {
9596
});
9697
}
9798

99+
@Test
100+
void meterRegistryCloserShouldOnlyCloseRegistriesBelongingToContextBeingClosed() {
101+
MeterRegistry parentMeterRegistry = new SimpleMeterRegistry();
102+
MeterRegistry childMeterRegistry = new SimpleMeterRegistry();
103+
this.contextRunner.withBean(MeterRegistry.class, () -> parentMeterRegistry).run((parent) -> {
104+
this.contextRunner.withBean(MeterRegistry.class, () -> childMeterRegistry)
105+
.withParent(parent)
106+
.run((child) -> {
107+
assertThat(childMeterRegistry.isClosed()).isFalse();
108+
assertThat(parentMeterRegistry.isClosed()).isFalse();
109+
});
110+
assertThat(childMeterRegistry.isClosed()).isTrue();
111+
assertThat(parentMeterRegistry.isClosed()).isFalse();
112+
});
113+
assertThat(childMeterRegistry.isClosed()).isTrue();
114+
assertThat(parentMeterRegistry.isClosed()).isTrue();
115+
}
116+
98117
@Test
99118
void supplyHandlerAndGroup() {
100119
this.contextRunner.run((context) -> {

0 commit comments

Comments
 (0)