mirror of
https://github.com/spring-projects/spring-boot.git
synced 2026-09-17 12:09:16 +00:00
Merge branch '3.5.x'
Closes gh-48325
This commit is contained in:
+14
-10
@@ -16,8 +16,6 @@
|
||||
|
||||
package org.springframework.boot.micrometer.metrics.autoconfigure;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import io.micrometer.core.annotation.Timed;
|
||||
import io.micrometer.core.instrument.Clock;
|
||||
import io.micrometer.core.instrument.MeterRegistry;
|
||||
@@ -47,6 +45,7 @@ import org.springframework.core.annotation.Order;
|
||||
* @author Jon Schneider
|
||||
* @author Stephane Nicoll
|
||||
* @author Moritz Halbritter
|
||||
* @author Michael Berry
|
||||
* @author Phillip Webb
|
||||
* @since 4.0.0
|
||||
*/
|
||||
@@ -77,8 +76,8 @@ public final class MetricsAutoConfiguration {
|
||||
}
|
||||
|
||||
@Bean
|
||||
MeterRegistryCloser meterRegistryCloser(ObjectProvider<MeterRegistry> meterRegistries) {
|
||||
return new MeterRegistryCloser(meterRegistries.orderedStream().toList());
|
||||
MeterRegistryCloser meterRegistryCloser(ApplicationContext context) {
|
||||
return new MeterRegistryCloser(context);
|
||||
}
|
||||
|
||||
@Bean
|
||||
@@ -100,17 +99,22 @@ public final class MetricsAutoConfiguration {
|
||||
*/
|
||||
static class MeterRegistryCloser implements ApplicationListener<ContextClosedEvent> {
|
||||
|
||||
private final List<MeterRegistry> meterRegistries;
|
||||
private final ApplicationContext context;
|
||||
|
||||
MeterRegistryCloser(List<MeterRegistry> meterRegistries) {
|
||||
this.meterRegistries = meterRegistries;
|
||||
private final Iterable<MeterRegistry> meterRegistries;
|
||||
|
||||
MeterRegistryCloser(ApplicationContext context) {
|
||||
this.meterRegistries = context.getBeansOfType(MeterRegistry.class).values();
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(ContextClosedEvent event) {
|
||||
for (MeterRegistry meterRegistry : this.meterRegistries) {
|
||||
if (!meterRegistry.isClosed()) {
|
||||
meterRegistry.close();
|
||||
if (this.context.equals(event.getApplicationContext())) {
|
||||
for (MeterRegistry meterRegistry : this.meterRegistries) {
|
||||
if (!meterRegistry.isClosed()) {
|
||||
meterRegistry.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+19
@@ -46,6 +46,7 @@ import static org.mockito.Mockito.mock;
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
* @author Moritz Halbritter
|
||||
* @author Michael Berry
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
class MetricsAutoConfigurationTests {
|
||||
@@ -95,6 +96,24 @@ class MetricsAutoConfigurationTests {
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void meterRegistryCloserShouldOnlyCloseRegistriesBelongingToContextBeingClosed() {
|
||||
MeterRegistry parentMeterRegistry = new SimpleMeterRegistry();
|
||||
MeterRegistry childMeterRegistry = new SimpleMeterRegistry();
|
||||
this.contextRunner.withBean(MeterRegistry.class, () -> parentMeterRegistry).run((parent) -> {
|
||||
this.contextRunner.withBean(MeterRegistry.class, () -> childMeterRegistry)
|
||||
.withParent(parent)
|
||||
.run((child) -> {
|
||||
assertThat(childMeterRegistry.isClosed()).isFalse();
|
||||
assertThat(parentMeterRegistry.isClosed()).isFalse();
|
||||
});
|
||||
assertThat(childMeterRegistry.isClosed()).isTrue();
|
||||
assertThat(parentMeterRegistry.isClosed()).isFalse();
|
||||
});
|
||||
assertThat(childMeterRegistry.isClosed()).isTrue();
|
||||
assertThat(parentMeterRegistry.isClosed()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void supplyHandlerAndGroup() {
|
||||
this.contextRunner.run((context) -> {
|
||||
|
||||
Reference in New Issue
Block a user