Skip to content

Commit a2a8764

Browse files
committed
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 # Conflicts: # spring-integration-core/src/main/java/org/springframework/integration/config/EnableIntegrationManagement.java # spring-integration-core/src/main/java/org/springframework/integration/config/IntegrationManagementConfiguration.java # spring-integration-core/src/main/java/org/springframework/integration/config/IntegrationManagementConfigurer.java * Fix some deprecation warnings # Conflicts: # spring-integration-core/src/main/java/org/springframework/integration/config/IntegrationManagementConfigurer.java
1 parent 293cf73 commit a2a8764

File tree

12 files changed

+147
-54
lines changed

12 files changed

+147
-54
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
@@ -24,6 +24,7 @@
2424

2525
import org.springframework.context.annotation.Import;
2626
import org.springframework.core.annotation.AliasFor;
27+
import org.springframework.integration.support.management.micrometer.MicrometerMetricsCaptorRegistrar;
2728

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

4446
/**

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
@@ -21,6 +21,7 @@
2121
import java.util.List;
2222
import java.util.Map;
2323

24+
import org.springframework.beans.factory.ObjectProvider;
2425
import org.springframework.beans.factory.config.BeanDefinition;
2526
import org.springframework.context.EnvironmentAware;
2627
import org.springframework.context.annotation.Bean;
@@ -30,6 +31,7 @@
3031
import org.springframework.core.annotation.AnnotationAttributes;
3132
import org.springframework.core.env.Environment;
3233
import org.springframework.core.type.AnnotationMetadata;
34+
import org.springframework.integration.support.management.metrics.MetricsCaptor;
3335
import org.springframework.util.Assert;
3436
import org.springframework.util.StringUtils;
3537

@@ -67,13 +69,15 @@ public void setImportMetadata(AnnotationMetadata importMetadata) {
6769
@SuppressWarnings("deprecation")
6870
@Bean(name = IntegrationManagementConfigurer.MANAGEMENT_CONFIGURER_NAME)
6971
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
70-
public IntegrationManagementConfigurer managementConfigurer() {
72+
public IntegrationManagementConfigurer managementConfigurer(ObjectProvider<MetricsCaptor> metricsCaptorProvider) {
73+
7174
IntegrationManagementConfigurer configurer = new IntegrationManagementConfigurer();
7275
setupCountsEnabledNamePatterns(configurer);
7376
setupStatsEnabledNamePatterns(configurer);
7477
configurer.setDefaultLoggingEnabled(
7578
Boolean.parseBoolean(this.environment.resolvePlaceholders(
7679
(String) this.attributes.get("defaultLoggingEnabled"))));
80+
configurer.setMetricsCaptor(metricsCaptorProvider.getIfUnique());
7781
configurer.setDefaultCountsEnabled(
7882
Boolean.parseBoolean(this.environment.resolvePlaceholders(
7983
(String) this.attributes.get("defaultCountsEnabled"))));

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

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2019 the original author or authors.
2+
* Copyright 2015-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.
@@ -18,14 +18,17 @@
1818

1919
import java.util.Arrays;
2020
import java.util.HashMap;
21+
import java.util.HashSet;
2122
import java.util.Map;
2223
import java.util.Map.Entry;
24+
import java.util.Set;
2325

2426
import org.apache.commons.logging.Log;
2527
import org.apache.commons.logging.LogFactory;
2628

2729
import org.springframework.beans.BeansException;
2830
import org.springframework.beans.factory.BeanNameAware;
31+
import org.springframework.beans.factory.DisposableBean;
2932
import org.springframework.beans.factory.SmartInitializingSingleton;
3033
import org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor;
3134
import org.springframework.context.ApplicationContext;
@@ -37,13 +40,13 @@
3740
import org.springframework.integration.support.management.IntegrationManagement;
3841
import org.springframework.integration.support.management.IntegrationManagement.ManagementOverrides;
3942
import org.springframework.integration.support.management.PollableChannelManagement;
43+
import org.springframework.integration.support.management.metrics.MeterFacade;
4044
import org.springframework.integration.support.management.metrics.MetricsCaptor;
41-
import org.springframework.integration.support.management.micrometer.MicrometerMetricsCaptor;
4245
import org.springframework.integration.support.utils.PatternMatchUtils;
46+
import org.springframework.lang.Nullable;
4347
import org.springframework.messaging.MessageChannel;
4448
import org.springframework.messaging.MessageHandler;
4549
import org.springframework.util.Assert;
46-
import org.springframework.util.ClassUtils;
4750
import org.springframework.util.StringUtils;
4851

4952

@@ -61,12 +64,14 @@
6164
@SuppressWarnings("deprecation")
6265
public class IntegrationManagementConfigurer
6366
implements SmartInitializingSingleton, ApplicationContextAware, BeanNameAware,
64-
DestructionAwareBeanPostProcessor {
67+
DestructionAwareBeanPostProcessor, DisposableBean {
6568

6669
private static final Log LOGGER = LogFactory.getLog(IntegrationManagementConfigurer.class);
6770

6871
public static final String MANAGEMENT_CONFIGURER_NAME = "integrationManagementConfigurer";
6972

73+
private final Set<MeterFacade> gauges = new HashSet<>();
74+
7075
private final Map<String, org.springframework.integration.support.management.MessageChannelMetrics>
7176
channelsByName = new HashMap<>();
7277

@@ -244,15 +249,15 @@ public void setDefaultLoggingEnabled(boolean defaultLoggingEnabled) {
244249
this.defaultLoggingEnabled = defaultLoggingEnabled;
245250
}
246251

252+
public void setMetricsCaptor(@Nullable MetricsCaptor metricsCaptor) {
253+
this.metricsCaptor = metricsCaptor;
254+
}
255+
247256
@Override
248257
public void afterSingletonsInstantiated() {
249258
Assert.state(this.applicationContext != null, "'applicationContext' must not be null");
250259
Assert.state(MANAGEMENT_CONFIGURER_NAME.equals(this.beanName), getClass().getSimpleName()
251260
+ " bean name must be " + MANAGEMENT_CONFIGURER_NAME);
252-
if (ClassUtils.isPresent("io.micrometer.core.instrument.MeterRegistry",
253-
this.applicationContext.getClassLoader())) {
254-
this.metricsCaptor = MicrometerMetricsCaptor.loadCaptor(this.applicationContext);
255-
}
256261
if (this.metricsCaptor != null) {
257262
injectCaptor();
258263
registerComponentGauges();
@@ -440,20 +445,23 @@ private void configureSourceMetrics(String name,
440445
}
441446

442447
private void registerComponentGauges() {
443-
this.metricsCaptor.gaugeBuilder("spring.integration.channels", this,
444-
(c) -> this.applicationContext.getBeansOfType(MessageChannel.class).size())
445-
.description("The number of message channels")
446-
.build();
447-
448-
this.metricsCaptor.gaugeBuilder("spring.integration.handlers", this,
449-
(c) -> this.applicationContext.getBeansOfType(MessageHandler.class).size())
450-
.description("The number of message handlers")
451-
.build();
452-
453-
this.metricsCaptor.gaugeBuilder("spring.integration.sources", this,
454-
(c) -> this.applicationContext.getBeansOfType(MessageSource.class).size())
455-
.description("The number of message sources")
456-
.build();
448+
this.gauges.add(
449+
this.metricsCaptor.gaugeBuilder("spring.integration.channels", this,
450+
(c) -> this.applicationContext.getBeansOfType(MessageChannel.class).size())
451+
.description("The number of message channels")
452+
.build());
453+
454+
this.gauges.add(
455+
this.metricsCaptor.gaugeBuilder("spring.integration.handlers", this,
456+
(c) -> this.applicationContext.getBeansOfType(MessageHandler.class).size())
457+
.description("The number of message handlers")
458+
.build());
459+
460+
this.gauges.add(
461+
this.metricsCaptor.gaugeBuilder("spring.integration.sources", this,
462+
(c) -> this.applicationContext.getBeansOfType(MessageSource.class).size())
463+
.description("The number of message sources")
464+
.build());
457465
}
458466

459467
public String[] getChannelNames() {
@@ -506,4 +514,10 @@ public org.springframework.integration.support.management.MessageSourceMetrics g
506514
return null;
507515
}
508516

517+
@Override
518+
public void destroy() {
519+
this.gauges.forEach(MeterFacade::remove);
520+
this.gauges.clear();
521+
}
522+
509523
}

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/support/management/graph/IntegrationGraphServerTests.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,11 @@ void testIncludesDynamic() {
238238
@ImportResource("org/springframework/integration/support/management/graph/integration-graph-context.xml")
239239
public static class Config {
240240

241+
@Bean
242+
public static MeterRegistry meterRegistry() {
243+
return new SimpleMeterRegistry();
244+
}
245+
241246
@Bean
242247
public IntegrationGraphServer server() {
243248
IntegrationGraphServer server = new IntegrationGraphServer();
@@ -255,11 +260,6 @@ public IntegrationGraphServer server() {
255260
return server;
256261
}
257262

258-
@Bean
259-
public MeterRegistry meterRegistry() {
260-
return new SimpleMeterRegistry();
261-
}
262-
263263
@Bean
264264
public MessageProducer producer() {
265265
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
@@ -226,7 +226,7 @@ public void testMicrometerMetrics() {
226226
public static class Config {
227227

228228
@Bean
229-
public MeterRegistry meterRegistry() {
229+
public static MeterRegistry meterRegistry() {
230230
return new SimpleMeterRegistry();
231231
}
232232

spring-integration-mongodb/src/test/java/org/springframework/integration/mongodb/rules/MongoDbAvailableTests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ protected MongoDbFactory prepareMongoFactory(String... additionalCollectionsToDr
6060
protected void cleanupCollections(MongoDbFactory mongoDbFactory, String... additionalCollectionsToDrop) {
6161
MongoTemplate template = new MongoTemplate(mongoDbFactory);
6262
template.dropCollection("messages");
63+
template.dropCollection("channelMessages");
6364
template.dropCollection("configurableStoreMessages");
6465
template.dropCollection("data");
6566
for (String additionalCollection : additionalCollectionsToDrop) {

0 commit comments

Comments
 (0)