Refactor Spring Integration metrics support

Update Spring Integration metrics support since Spring Integration
`4.3.6`+ no longer needs `spring-integration-jmx` enable
`MessageChannel`, `MessageHandler` and `MessageSource` metrics.

- Add `IntegrationManagementConfiguration` conditional auto-configuration
  to provide `@EnableIntegrationManagement` when JMX is `enabled` or there
  is no `IntegrationManagementConfigurer.MANAGEMENT_CONFIGURER_NAME` bean.
  By default this bean doesn't exist and you explicitly should declare it
  (e.g. via `@EnableIntegrationManagement`) if you would like to collect
  metrics. At the same time Spring Integration enables it when JMX
  management is present (that is a purpose of that new
  `IntegrationManagementConfiguration`)

- Change `SpringIntegrationMetricReader` to read metrics from the
  `IntegrationManagementConfigurer`, not `IntegrationMBeanExporter`

- Change `PublicMetricsAutoConfiguration` to register
  `IntegrationManagementConfigurer.MANAGEMENT_CONFIGURER_NAME` bean if
  not present. Since we are here in `actuator`, therefore we are
  interested in the metrics for SI as well.

- Since we don't need JMX for the metrics any more, remove SI-JMX
  dependency from the `spring-boot-starter-integration`.

- Remove `IntegrationManagementConfiguration` modification from the
  `integrationMbeanExporter()`, since that looks like mutation of an
  external object, when end-user would prefer their own options.
  Therefore we don't need `ObjectProvider<IntegrationManagementConfigurer>`, too

- Add missed `MessageSourceMetrics` gathering for the
  `SpringIntegrationMetricReader`

Closes gh-7722
pull/7763/head
Artem Bilan 8 years ago committed by Phillip Webb
parent 2e0f87e753
commit d69e43b433

@ -193,11 +193,6 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-jmx</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-core</artifactId>
@ -373,5 +368,10 @@
<artifactId>snakeyaml</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-jmx</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

@ -46,13 +46,15 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnJava;
import org.springframework.boot.autoconfigure.condition.ConditionalOnJava.JavaVersion;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.condition.SearchStrategy;
import org.springframework.boot.autoconfigure.integration.IntegrationAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.metadata.DataSourcePoolMetadataProvider;
import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.monitor.IntegrationMBeanExporter;
import org.springframework.integration.config.EnableIntegrationManagement;
import org.springframework.integration.support.management.IntegrationManagementConfigurer;
import org.springframework.lang.UsesJava7;
/**
@ -61,6 +63,7 @@ import org.springframework.lang.UsesJava7;
* @author Stephane Nicoll
* @author Phillip Webb
* @author Johannes Edmeier
* @author Artem Bilan
* @since 1.2.0
*/
@Configuration
@ -139,18 +142,28 @@ public class PublicMetricsAutoConfiguration {
}
@Configuration
@ConditionalOnClass(IntegrationMBeanExporter.class)
@ConditionalOnBean(IntegrationMBeanExporter.class)
@ConditionalOnClass(EnableIntegrationManagement.class)
@ConditionalOnJava(JavaVersion.SEVEN)
@UsesJava7
static class IntegrationMetricsConfiguration {
@Bean(name = IntegrationManagementConfigurer.MANAGEMENT_CONFIGURER_NAME)
@ConditionalOnMissingBean(value = IntegrationManagementConfigurer.class,
name = IntegrationManagementConfigurer.MANAGEMENT_CONFIGURER_NAME,
search = SearchStrategy.CURRENT)
public IntegrationManagementConfigurer managementConfigurer() {
IntegrationManagementConfigurer configurer = new IntegrationManagementConfigurer();
configurer.setDefaultCountsEnabled(true);
configurer.setDefaultStatsEnabled(true);
return configurer;
}
@Bean
@ConditionalOnMissingBean(name = "springIntegrationPublicMetrics")
public MetricReaderPublicMetrics springIntegrationPublicMetrics(
IntegrationMBeanExporter exporter) {
IntegrationManagementConfigurer managementConfigurer) {
return new MetricReaderPublicMetrics(
new SpringIntegrationMetricReader(exporter));
new SpringIntegrationMetricReader(managementConfigurer));
}
}

@ -22,24 +22,29 @@ import java.util.List;
import org.springframework.boot.actuate.metrics.Metric;
import org.springframework.boot.actuate.metrics.reader.MetricReader;
import org.springframework.integration.monitor.IntegrationMBeanExporter;
import org.springframework.integration.support.management.IntegrationManagementConfigurer;
import org.springframework.integration.support.management.MessageChannelMetrics;
import org.springframework.integration.support.management.MessageHandlerMetrics;
import org.springframework.integration.support.management.MessageSourceMetrics;
import org.springframework.integration.support.management.PollableChannelManagement;
import org.springframework.integration.support.management.Statistics;
import org.springframework.lang.UsesJava7;
/**
* A {@link MetricReader} for Spring Integration metrics (as provided by
* spring-integration-jmx).
* {@link IntegrationManagementConfigurer}).
*
* @author Dave Syer
* @author Artem Bilan
* @since 1.3.0
*/
@UsesJava7
public class SpringIntegrationMetricReader implements MetricReader {
private final IntegrationMBeanExporter exporter;
private final IntegrationManagementConfigurer managementConfigurer;
public SpringIntegrationMetricReader(IntegrationMBeanExporter exporter) {
this.exporter = exporter;
public SpringIntegrationMetricReader(IntegrationManagementConfigurer managementConfigurer) {
this.managementConfigurer = managementConfigurer;
}
@Override
@ -49,31 +54,40 @@ public class SpringIntegrationMetricReader implements MetricReader {
@Override
public Iterable<Metric<?>> findAll() {
IntegrationMBeanExporter exporter = this.exporter;
List<Metric<?>> metrics = new ArrayList<Metric<?>>();
for (String name : exporter.getChannelNames()) {
for (String name : this.managementConfigurer.getChannelNames()) {
MessageChannelMetrics channelMetrics = this.managementConfigurer.getChannelMetrics(name);
String prefix = "integration.channel." + name;
metrics.addAll(getStatistics(prefix + ".errorRate",
exporter.getChannelErrorRate(name)));
metrics.add(new Metric<Long>(prefix + ".sendCount",
exporter.getChannelSendCountLong(name)));
metrics.addAll(getStatistics(prefix + ".sendRate",
exporter.getChannelSendRate(name)));
metrics.add(new Metric<Long>(prefix + ".receiveCount",
exporter.getChannelReceiveCountLong(name)));
metrics.addAll(getStatistics(prefix + ".errorRate", channelMetrics.getErrorRate()));
metrics.add(new Metric<Long>(prefix + ".sendCount", channelMetrics.getSendCountLong()));
metrics.addAll(getStatistics(prefix + ".sendRate", channelMetrics.getSendRate()));
if (channelMetrics instanceof PollableChannelManagement) {
metrics.add(new Metric<Long>(prefix + ".receiveCount",
((PollableChannelManagement) channelMetrics).getReceiveCountLong()));
}
}
for (String name : this.managementConfigurer.getHandlerNames()) {
MessageHandlerMetrics handlerMetrics = this.managementConfigurer.getHandlerMetrics(name);
String prefix = "integration.handler." + name;
metrics.addAll(getStatistics(prefix + ".duration", handlerMetrics.getDuration()));
metrics.add(new Metric<Long>(prefix + ".activeCount", handlerMetrics.getActiveCountLong()));
}
for (String name : exporter.getHandlerNames()) {
metrics.addAll(getStatistics("integration.handler." + name + ".duration",
exporter.getHandlerDuration(name)));
for (String name : this.managementConfigurer.getSourceNames()) {
MessageSourceMetrics sourceMetrics = this.managementConfigurer.getSourceMetrics(name);
String prefix = "integration.source." + name;
metrics.add(new Metric<Long>(prefix + ".messageCount", sourceMetrics.getMessageCountLong()));
}
metrics.add(new Metric<Integer>("integration.activeHandlerCount",
exporter.getActiveHandlerCount()));
metrics.add(new Metric<Integer>("integration.handlerCount",
exporter.getHandlerCount()));
this.managementConfigurer.getHandlerNames().length));
metrics.add(new Metric<Integer>("integration.channelCount",
exporter.getChannelCount()));
metrics.add(new Metric<Integer>("integration.queuedMessageCount",
exporter.getQueuedMessageCount()));
this.managementConfigurer.getChannelNames().length));
metrics.add(new Metric<Integer>("integration.sourceCount",
this.managementConfigurer.getSourceNames().length));
return metrics;
}
@ -91,9 +105,10 @@ public class SpringIntegrationMetricReader implements MetricReader {
@Override
public long count() {
int totalChannelCount = this.exporter.getChannelCount() * 11;
int totalHandlerCount = this.exporter.getHandlerCount() * 5;
return totalChannelCount + totalHandlerCount + 4;
int totalChannelCount = this.managementConfigurer.getChannelNames().length;
int totalHandlerCount = this.managementConfigurer.getHandlerNames().length;
int totalSourceCount = this.managementConfigurer.getSourceNames().length;
return totalChannelCount + totalHandlerCount + totalSourceCount;
}
}

@ -0,0 +1,61 @@
/*
* Copyright 2012-2016 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.actuate.metrics.integration;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.actuate.autoconfigure.PublicMetricsAutoConfiguration;
import org.springframework.boot.actuate.endpoint.MetricReaderPublicMetrics;
import org.springframework.boot.autoconfigure.integration.IntegrationAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link SpringIntegrationMetricReader}.
*
* @author Artem Bilan
*/
@RunWith(SpringRunner.class)
@SpringBootTest("spring.jmx.enabled=false")
@DirtiesContext
public class SpringIntegrationMetricReaderNoJmxTests {
@Autowired
@Qualifier("springIntegrationPublicMetrics")
private MetricReaderPublicMetrics integrationMetricReader;
@Test
public void test() {
assertThat(this.integrationMetricReader.metrics().size() > 0).isTrue();
}
@Configuration
@Import({IntegrationAutoConfiguration.class, PublicMetricsAutoConfiguration.class})
protected static class TestConfiguration {
}
}

@ -26,7 +26,7 @@ import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.integration.monitor.IntegrationMBeanExporter;
import org.springframework.integration.support.management.IntegrationManagementConfigurer;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringRunner;
@ -36,6 +36,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* Tests for {@link SpringIntegrationMetricReader}.
*
* @author Dave Syer
* @author Artem Bilan
*/
@RunWith(SpringRunner.class)
@SpringBootTest("spring.jmx.enabled=true")
@ -55,8 +56,8 @@ public class SpringIntegrationMetricReaderTests {
protected static class TestConfiguration {
@Bean
public SpringIntegrationMetricReader reader(IntegrationMBeanExporter exporter) {
return new SpringIntegrationMetricReader(exporter);
public SpringIntegrationMetricReader reader(IntegrationManagementConfigurer managementConfigurer) {
return new SpringIntegrationMetricReader(managementConfigurer);
}
}

@ -21,8 +21,6 @@ import javax.management.MBeanServer;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
@ -35,6 +33,7 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.integration.config.EnableIntegration;
import org.springframework.integration.config.EnableIntegrationManagement;
import org.springframework.integration.jmx.config.EnableIntegrationMBeanExport;
import org.springframework.integration.monitor.IntegrationMBeanExporter;
import org.springframework.integration.support.management.IntegrationManagementConfigurer;
@ -67,17 +66,10 @@ public class IntegrationAutoConfiguration {
protected static class IntegrationJmxConfiguration
implements EnvironmentAware, BeanFactoryAware {
private final IntegrationManagementConfigurer configurer;
private BeanFactory beanFactory;
private RelaxedPropertyResolver propertyResolver;
protected IntegrationJmxConfiguration(
@Qualifier(IntegrationManagementConfigurer.MANAGEMENT_CONFIGURER_NAME) ObjectProvider<IntegrationManagementConfigurer> configurerProvider) {
this.configurer = configurerProvider.getIfAvailable();
}
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
this.beanFactory = beanFactory;
@ -100,17 +92,25 @@ public class IntegrationAutoConfiguration {
if (StringUtils.hasLength(server)) {
exporter.setServer(this.beanFactory.getBean(server, MBeanServer.class));
}
if (this.configurer != null) {
if (this.configurer.getDefaultCountsEnabled() == null) {
this.configurer.setDefaultCountsEnabled(true);
}
if (this.configurer.getDefaultStatsEnabled() == null) {
this.configurer.setDefaultStatsEnabled(true);
}
}
return exporter;
}
}
@Configuration
@ConditionalOnClass({EnableIntegrationManagement.class, EnableIntegrationMBeanExport.class})
@ConditionalOnMissingBean(value = IntegrationManagementConfigurer.class,
name = IntegrationManagementConfigurer.MANAGEMENT_CONFIGURER_NAME,
search = SearchStrategy.CURRENT)
@ConditionalOnProperty(prefix = "spring.jmx", name = "enabled", havingValue = "true", matchIfMissing = true)
protected static class IntegrationManagementConfiguration {
@Configuration
@EnableIntegrationManagement(defaultCountsEnabled = "true", defaultStatsEnabled = "true")
protected static class EnableIntegrationManagementConfiguration {
}
}
}

@ -31,6 +31,7 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.integration.support.channel.HeaderChannelRegistry;
import org.springframework.integration.support.management.IntegrationManagementConfigurer;
import org.springframework.jmx.export.MBeanExporter;
import org.springframework.test.context.support.TestPropertySourceUtils;
@ -86,12 +87,14 @@ public class IntegrationAutoConfigurationTests {
MBeanServer mBeanServer = this.context.getBean(MBeanServer.class);
assertDomains(mBeanServer, true, "org.springframework.integration",
"org.springframework.integration.monitor");
assertThat(this.context.getBean(IntegrationManagementConfigurer.MANAGEMENT_CONFIGURER_NAME)).isNotNull();
}
@Test
public void disableJmxIntegration() {
load("spring.jmx.enabled=false");
assertThat(this.context.getBeansOfType(MBeanServer.class)).hasSize(0);
assertThat(this.context.getBeansOfType(IntegrationManagementConfigurer.class)).isEmpty();
}
@Test

@ -34,9 +34,5 @@
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-java-dsl</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-jmx</artifactId>
</dependency>
</dependencies>
</project>

@ -1 +1 @@
provides: spring-integration-core,spring-integration-java-dsl,spring-integration-jmx
provides: spring-integration-core,spring-integration-java-dsl

Loading…
Cancel
Save