GH-3376: Remove gauges on application ctx close (#3377)

* GH-3376: Remove gauges on application ctx close

Fixes https://github.com/spring-projects/spring-integration/issues/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
This commit is contained in:
Artem Bilan
2020-09-16 10:32:39 -04:00
parent 411e9945c3
commit d0cab670eb
12 changed files with 146 additions and 52 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2018-2019 the original author or authors.
* Copyright 2018-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -112,7 +112,7 @@ public class BeanNameTests {
public static class Config {
@Bean
public MeterRegistry meterRegistry() {
public static MeterRegistry meterRegistry() {
return new SimpleMeterRegistry();
}

View File

@@ -264,6 +264,11 @@ public class IntegrationGraphServerTests {
@ImportResource("org/springframework/integration/graph/integration-graph-context.xml")
public static class Config {
@Bean
public static MeterRegistry meterRegistry() {
return new SimpleMeterRegistry();
}
@Bean
public IntegrationGraphServer server() {
IntegrationGraphServer server = new IntegrationGraphServer();
@@ -281,11 +286,6 @@ public class IntegrationGraphServerTests {
return server;
}
@Bean
public MeterRegistry meterRegistry() {
return new SimpleMeterRegistry();
}
@Bean
public MessageProducer producer() {
MessageProducerSupport producer = new MessageProducerSupport() {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2018-2019 the original author or authors.
* Copyright 2018-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,10 +17,9 @@
package org.springframework.integration.support.management.micrometer;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ConfigurableApplicationContext;
@@ -32,7 +31,7 @@ import org.springframework.integration.config.EnableIntegrationManagement;
import org.springframework.integration.support.management.metrics.MetricsCaptor;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
import io.micrometer.core.instrument.MeterRegistry;
@@ -46,7 +45,7 @@ import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
* @since 5.1
*
*/
@RunWith(SpringRunner.class)
@SpringJUnitConfig
@TestExecutionListeners(DependencyInjectionTestExecutionListener.class)
public class MicrometerCustomMetricsTests {
@@ -81,20 +80,18 @@ public class MicrometerCustomMetricsTests {
// Test meter removal
this.context.close();
try {
registry.get("myTimer").timers();
fail("Expected MeterNotFoundException");
}
catch (MeterNotFoundException e) {
assertThat(e).hasMessageContaining("No meter with name 'myTimer' was found");
}
try {
registry.get("myCounter").counters();
fail("Expected MeterNotFoundException");
}
catch (MeterNotFoundException e) {
assertThat(e).hasMessageContaining("No meter with name 'myCounter' was found");
}
assertThatExceptionOfType(MeterNotFoundException.class)
.isThrownBy(() -> registry.get("myTimer").timers())
.withMessageContaining("No meter with name 'myTimer' was found");
assertThatExceptionOfType(MeterNotFoundException.class)
.isThrownBy(() -> registry.get("myCounter").counters())
.withMessageContaining("No meter with name 'myCounter' was found");
assertThatExceptionOfType(MeterNotFoundException.class)
.isThrownBy(() -> registry.get("spring.integration.channels").gauge())
.withMessageContaining("No meter with name 'spring.integration.channels' was found");
}
@Configuration

View File

@@ -226,7 +226,7 @@ public class MicrometerMetricsTests {
public static class Config {
@Bean
public MeterRegistry meterRegistry() {
public static MeterRegistry meterRegistry() {
return new SimpleMeterRegistry();
}