Skip to content

Commit e154067

Browse files
authored
GH-3376: Remove gauges on application ctx close (#3377)
* GH-3376: Remove gauges on application ctx close Fixes #3376 The `MeterRegistry` may request meters on application shutdown. The gauges for channels, handlers and message sources don't make sense at the moment since all those beans are going to be destroyed. * Remove gauges for channel, handler and message source numbers from the `IntegrationManagementConfigurer.destroy()` **Cherry-pick to 5.3.x & 5.2.x** * * Add `MicrometerImportSelector` to conditionally load a `MicrometerMetricsCaptorConfiguration` when `MeterRegistry` is on class path. * Make `MicrometerMetricsCaptorConfiguration.integrationMicrometerMetricsCaptor()` bean dependant on the `ObjectProvider<MeterRegistry>` * Make `IntegrationManagementConfiguration.managementConfigurer()` dependant on the `ObjectProvider<MetricsCaptor>`. This way the `IntegrationManagementConfigurer` is destroyed before `MeterRegistry` when application context is closed * Deprecate `MicrometerMetricsCaptor.loadCaptor()` in favor of `@Import(MicrometerImportSelector.class)` * * Add `MicrometerMetricsCaptorRegistrar` to register a `MICROMETER_CAPTOR_NAME` bean when `MeterRegistry` is on class path and no `MICROMETER_CAPTOR_NAME` bean yet. * Make `IntegrationManagementConfiguration.managementConfigurer()` dependant on the `ObjectProvider<MetricsCaptor>`. This way the `IntegrationManagementConfigurer` is destroyed before `MeterRegistry` when application context is closed * Deprecate `MicrometerMetricsCaptor.loadCaptor()` in favor of `@Import(MicrometerMetricsCaptorRegistrar.class)` * Fix test to make a `MeterRegistry` bean as `static` since `@EnableIntegrationManagement` depends on this bean definition now
1 parent 7a97eb6 commit e154067

File tree

9 files changed

+143
-52
lines changed

9 files changed

+143
-52
lines changed

spring-integration-core/src/main/java/org/springframework/integration/config/EnableIntegrationManagement.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.lang.annotation.Target;
2424

2525
import org.springframework.context.annotation.Import;
26+
import org.springframework.integration.support.management.micrometer.MicrometerMetricsCaptorRegistrar;
2627

2728
/**
2829
* Enables default configuring of management in Spring Integration components in an existing application.
@@ -31,13 +32,14 @@
3132
* bean is defined under the name {@code integrationManagementConfigurer}.
3233
*
3334
* @author Gary Russell
35+
* @author Artem Bilan
3436
*
3537
* @since 4.2
3638
*/
3739
@Target(ElementType.TYPE)
3840
@Retention(RetentionPolicy.RUNTIME)
3941
@Documented
40-
@Import(IntegrationManagementConfiguration.class)
42+
@Import({ MicrometerMetricsCaptorRegistrar.class, IntegrationManagementConfiguration.class })
4143
public @interface EnableIntegrationManagement {
4244

4345
/**

spring-integration-core/src/main/java/org/springframework/integration/config/IntegrationManagementConfiguration.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.util.Map;
2020

21+
import org.springframework.beans.factory.ObjectProvider;
2122
import org.springframework.beans.factory.config.BeanDefinition;
2223
import org.springframework.context.EnvironmentAware;
2324
import org.springframework.context.annotation.Bean;
@@ -27,6 +28,7 @@
2728
import org.springframework.core.annotation.AnnotationAttributes;
2829
import org.springframework.core.env.Environment;
2930
import org.springframework.core.type.AnnotationMetadata;
31+
import org.springframework.integration.support.management.metrics.MetricsCaptor;
3032
import org.springframework.util.Assert;
3133

3234
/**
@@ -62,11 +64,13 @@ public void setImportMetadata(AnnotationMetadata importMetadata) {
6264

6365
@Bean(name = IntegrationManagementConfigurer.MANAGEMENT_CONFIGURER_NAME)
6466
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
65-
public IntegrationManagementConfigurer managementConfigurer() {
67+
public IntegrationManagementConfigurer managementConfigurer(ObjectProvider<MetricsCaptor> metricsCaptorProvider) {
68+
6669
IntegrationManagementConfigurer configurer = new IntegrationManagementConfigurer();
6770
configurer.setDefaultLoggingEnabled(
6871
Boolean.parseBoolean(this.environment.resolvePlaceholders(
6972
(String) this.attributes.get("defaultLoggingEnabled"))));
73+
configurer.setMetricsCaptor(metricsCaptorProvider.getIfUnique());
7074
return configurer;
7175
}
7276

spring-integration-core/src/main/java/org/springframework/integration/config/IntegrationManagementConfigurer.java

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,27 @@
1616

1717
package org.springframework.integration.config;
1818

19+
import java.util.HashSet;
1920
import java.util.Map;
2021
import java.util.Map.Entry;
22+
import java.util.Set;
2123

2224
import org.springframework.beans.BeansException;
2325
import org.springframework.beans.factory.BeanNameAware;
26+
import org.springframework.beans.factory.DisposableBean;
2427
import org.springframework.beans.factory.SmartInitializingSingleton;
2528
import org.springframework.beans.factory.config.BeanPostProcessor;
2629
import org.springframework.context.ApplicationContext;
2730
import org.springframework.context.ApplicationContextAware;
2831
import org.springframework.integration.core.MessageSource;
2932
import org.springframework.integration.support.management.IntegrationManagement;
3033
import org.springframework.integration.support.management.IntegrationManagement.ManagementOverrides;
34+
import org.springframework.integration.support.management.metrics.MeterFacade;
3135
import org.springframework.integration.support.management.metrics.MetricsCaptor;
32-
import org.springframework.integration.support.management.micrometer.MicrometerMetricsCaptor;
36+
import org.springframework.lang.Nullable;
3337
import org.springframework.messaging.MessageChannel;
3438
import org.springframework.messaging.MessageHandler;
3539
import org.springframework.util.Assert;
36-
import org.springframework.util.ClassUtils;
3740

3841

3942
/**
@@ -49,13 +52,16 @@
4952
*
5053
*/
5154
public class IntegrationManagementConfigurer
52-
implements SmartInitializingSingleton, ApplicationContextAware, BeanNameAware, BeanPostProcessor {
55+
implements SmartInitializingSingleton, ApplicationContextAware, BeanNameAware, BeanPostProcessor,
56+
DisposableBean {
5357

5458
/**
5559
* Bean name of the configurer.
5660
*/
5761
public static final String MANAGEMENT_CONFIGURER_NAME = "integrationManagementConfigurer";
5862

63+
private final Set<MeterFacade> gauges = new HashSet<>();
64+
5965
private ApplicationContext applicationContext;
6066

6167
private String beanName;
@@ -99,15 +105,15 @@ public void setDefaultLoggingEnabled(boolean defaultLoggingEnabled) {
99105
this.defaultLoggingEnabled = defaultLoggingEnabled;
100106
}
101107

108+
public void setMetricsCaptor(@Nullable MetricsCaptor metricsCaptor) {
109+
this.metricsCaptor = metricsCaptor;
110+
}
111+
102112
@Override
103113
public void afterSingletonsInstantiated() {
104114
Assert.state(this.applicationContext != null, "'applicationContext' must not be null");
105115
Assert.state(MANAGEMENT_CONFIGURER_NAME.equals(this.beanName), getClass().getSimpleName()
106116
+ " bean name must be " + MANAGEMENT_CONFIGURER_NAME);
107-
if (ClassUtils.isPresent("io.micrometer.core.instrument.MeterRegistry",
108-
this.applicationContext.getClassLoader())) {
109-
this.metricsCaptor = MicrometerMetricsCaptor.loadCaptor(this.applicationContext);
110-
}
111117
if (this.metricsCaptor != null) {
112118
injectCaptor();
113119
registerComponentGauges();
@@ -144,20 +150,29 @@ public Object postProcessAfterInitialization(Object bean, String name) throws Be
144150
}
145151

146152
private void registerComponentGauges() {
147-
this.metricsCaptor.gaugeBuilder("spring.integration.channels", this,
148-
(c) -> this.applicationContext.getBeansOfType(MessageChannel.class).size())
149-
.description("The number of message channels")
150-
.build();
151-
152-
this.metricsCaptor.gaugeBuilder("spring.integration.handlers", this,
153-
(c) -> this.applicationContext.getBeansOfType(MessageHandler.class).size())
154-
.description("The number of message handlers")
155-
.build();
156-
157-
this.metricsCaptor.gaugeBuilder("spring.integration.sources", this,
158-
(c) -> this.applicationContext.getBeansOfType(MessageSource.class).size())
159-
.description("The number of message sources")
160-
.build();
153+
this.gauges.add(
154+
this.metricsCaptor.gaugeBuilder("spring.integration.channels", this,
155+
(c) -> this.applicationContext.getBeansOfType(MessageChannel.class).size())
156+
.description("The number of message channels")
157+
.build());
158+
159+
this.gauges.add(
160+
this.metricsCaptor.gaugeBuilder("spring.integration.handlers", this,
161+
(c) -> this.applicationContext.getBeansOfType(MessageHandler.class).size())
162+
.description("The number of message handlers")
163+
.build());
164+
165+
this.gauges.add(
166+
this.metricsCaptor.gaugeBuilder("spring.integration.sources", this,
167+
(c) -> this.applicationContext.getBeansOfType(MessageSource.class).size())
168+
.description("The number of message sources")
169+
.build());
170+
}
171+
172+
@Override
173+
public void destroy() {
174+
this.gauges.forEach(MeterFacade::remove);
175+
this.gauges.clear();
161176
}
162177

163178
private static ManagementOverrides getOverrides(IntegrationManagement bean) {

spring-integration-core/src/main/java/org/springframework/integration/support/management/micrometer/MicrometerMetricsCaptor.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018-2019 the original author or authors.
2+
* Copyright 2018-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -86,7 +86,10 @@ public MeterFacade removeMeter(MeterFacade facade) {
8686
* there's already a {@link MetricsCaptor} bean, return that.
8787
* @param applicationContext the application context.
8888
* @return the instance.
89+
* @deprecated since 5.2.9 in favor of {@code @Import(MicrometerMetricsCaptorRegistrar.class)};
90+
* will be removed in 6.0.
8991
*/
92+
@Deprecated
9093
public static MetricsCaptor loadCaptor(ApplicationContext applicationContext) {
9194
try {
9295
MeterRegistry registry = applicationContext.getBean(MeterRegistry.class);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright 2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.integration.support.management.micrometer;
18+
19+
import org.springframework.beans.factory.ListableBeanFactory;
20+
import org.springframework.beans.factory.config.BeanDefinition;
21+
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
22+
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
23+
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
24+
import org.springframework.core.type.AnnotationMetadata;
25+
import org.springframework.util.ClassUtils;
26+
27+
/**
28+
* An {@link ImportBeanDefinitionRegistrar} to conditionally add a {@link MicrometerMetricsCaptor}
29+
* bean when {@code io.micrometer.core.instrument.MeterRegistry} is present in classpath and
30+
* no {@link MicrometerMetricsCaptor#MICROMETER_CAPTOR_NAME} bean present yet.
31+
*
32+
* @author Artem Bilan
33+
*
34+
* @since 5.2.9
35+
*/
36+
public class MicrometerMetricsCaptorRegistrar implements ImportBeanDefinitionRegistrar {
37+
38+
private static final Class<?> METER_REGISTRY_CLASS;
39+
40+
static {
41+
Class<?> aClass = null;
42+
try {
43+
aClass = ClassUtils.forName("io.micrometer.core.instrument.MeterRegistry",
44+
ClassUtils.getDefaultClassLoader());
45+
}
46+
catch (ClassNotFoundException e) {
47+
// Ignore - no Micrometer in classpath
48+
}
49+
METER_REGISTRY_CLASS = aClass;
50+
}
51+
52+
@Override
53+
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
54+
if (METER_REGISTRY_CLASS != null
55+
&& !registry.containsBeanDefinition(MicrometerMetricsCaptor.MICROMETER_CAPTOR_NAME)) {
56+
String[] beanNamesForType =
57+
((ListableBeanFactory) registry).getBeanNamesForType(METER_REGISTRY_CLASS, false, false);
58+
for (String beanName : beanNamesForType) {
59+
registry.registerBeanDefinition(MicrometerMetricsCaptor.MICROMETER_CAPTOR_NAME,
60+
BeanDefinitionBuilder.genericBeanDefinition(MicrometerMetricsCaptor.class)
61+
.addConstructorArgReference(beanName)
62+
.setRole(BeanDefinition.ROLE_INFRASTRUCTURE)
63+
.getBeanDefinition());
64+
return;
65+
}
66+
67+
}
68+
}
69+
70+
}

spring-integration-core/src/test/java/org/springframework/integration/endpoint/BeanNameTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018-2019 the original author or authors.
2+
* Copyright 2018-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -112,7 +112,7 @@ public void contextLoads() {
112112
public static class Config {
113113

114114
@Bean
115-
public MeterRegistry meterRegistry() {
115+
public static MeterRegistry meterRegistry() {
116116
return new SimpleMeterRegistry();
117117
}
118118

spring-integration-core/src/test/java/org/springframework/integration/graph/IntegrationGraphServerTests.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,11 @@ void testIncludesDynamic() {
257257
@ImportResource("org/springframework/integration/graph/integration-graph-context.xml")
258258
public static class Config {
259259

260+
@Bean
261+
public static MeterRegistry meterRegistry() {
262+
return new SimpleMeterRegistry();
263+
}
264+
260265
@Bean
261266
public IntegrationGraphServer server() {
262267
IntegrationGraphServer server = new IntegrationGraphServer();
@@ -274,11 +279,6 @@ public IntegrationGraphServer server() {
274279
return server;
275280
}
276281

277-
@Bean
278-
public MeterRegistry meterRegistry() {
279-
return new SimpleMeterRegistry();
280-
}
281-
282282
@Bean
283283
public MessageProducer producer() {
284284
MessageProducerSupport producer = new MessageProducerSupport() {

spring-integration-core/src/test/java/org/springframework/integration/support/management/micrometer/MicrometerCustomMetricsTests.java

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018-2019 the original author or authors.
2+
* Copyright 2018-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,10 +17,9 @@
1717
package org.springframework.integration.support.management.micrometer;
1818

1919
import static org.assertj.core.api.Assertions.assertThat;
20-
import static org.assertj.core.api.Assertions.fail;
20+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
2121

22-
import org.junit.Test;
23-
import org.junit.runner.RunWith;
22+
import org.junit.jupiter.api.Test;
2423

2524
import org.springframework.beans.factory.annotation.Autowired;
2625
import org.springframework.context.ConfigurableApplicationContext;
@@ -32,7 +31,7 @@
3231
import org.springframework.integration.support.management.metrics.MetricsCaptor;
3332
import org.springframework.messaging.support.GenericMessage;
3433
import org.springframework.test.context.TestExecutionListeners;
35-
import org.springframework.test.context.junit4.SpringRunner;
34+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
3635
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
3736

3837
import io.micrometer.core.instrument.MeterRegistry;
@@ -46,7 +45,7 @@
4645
* @since 5.1
4746
*
4847
*/
49-
@RunWith(SpringRunner.class)
48+
@SpringJUnitConfig
5049
@TestExecutionListeners(DependencyInjectionTestExecutionListener.class)
5150
public class MicrometerCustomMetricsTests {
5251

@@ -81,20 +80,18 @@ public void testSend() {
8180

8281
// Test meter removal
8382
this.context.close();
84-
try {
85-
registry.get("myTimer").timers();
86-
fail("Expected MeterNotFoundException");
87-
}
88-
catch (MeterNotFoundException e) {
89-
assertThat(e).hasMessageContaining("No meter with name 'myTimer' was found");
90-
}
91-
try {
92-
registry.get("myCounter").counters();
93-
fail("Expected MeterNotFoundException");
94-
}
95-
catch (MeterNotFoundException e) {
96-
assertThat(e).hasMessageContaining("No meter with name 'myCounter' was found");
97-
}
83+
84+
assertThatExceptionOfType(MeterNotFoundException.class)
85+
.isThrownBy(() -> registry.get("myTimer").timers())
86+
.withMessageContaining("No meter with name 'myTimer' was found");
87+
88+
assertThatExceptionOfType(MeterNotFoundException.class)
89+
.isThrownBy(() -> registry.get("myCounter").counters())
90+
.withMessageContaining("No meter with name 'myCounter' was found");
91+
92+
assertThatExceptionOfType(MeterNotFoundException.class)
93+
.isThrownBy(() -> registry.get("spring.integration.channels").gauge())
94+
.withMessageContaining("No meter with name 'spring.integration.channels' was found");
9895
}
9996

10097
@Configuration

spring-integration-core/src/test/java/org/springframework/integration/support/management/micrometer/MicrometerMetricsTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ public void testMicrometerMetrics() {
255255
public static class Config {
256256

257257
@Bean
258-
public MeterRegistry meterRegistry() {
258+
public static MeterRegistry meterRegistry() {
259259
return new SimpleMeterRegistry();
260260
}
261261

0 commit comments

Comments
 (0)