Polish Spring Integration metrics support

See gh-7722
This commit is contained in:
Phillip Webb 2016-12-21 15:34:46 -08:00
parent d69e43b433
commit b87e02dde0
8 changed files with 86 additions and 67 deletions

View File

@ -358,6 +358,11 @@
<artifactId>spring-data-rest-webmvc</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-jmx</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
@ -368,10 +373,5 @@
<artifactId>snakeyaml</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-jmx</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -148,9 +148,7 @@ public class PublicMetricsAutoConfiguration {
static class IntegrationMetricsConfiguration {
@Bean(name = IntegrationManagementConfigurer.MANAGEMENT_CONFIGURER_NAME)
@ConditionalOnMissingBean(value = IntegrationManagementConfigurer.class,
name = IntegrationManagementConfigurer.MANAGEMENT_CONFIGURER_NAME,
search = SearchStrategy.CURRENT)
@ConditionalOnMissingBean(value = IntegrationManagementConfigurer.class, name = IntegrationManagementConfigurer.MANAGEMENT_CONFIGURER_NAME, search = SearchStrategy.CURRENT)
public IntegrationManagementConfigurer managementConfigurer() {
IntegrationManagementConfigurer configurer = new IntegrationManagementConfigurer();
configurer.setDefaultCountsEnabled(true);

View File

@ -41,10 +41,10 @@ import org.springframework.lang.UsesJava7;
@UsesJava7
public class SpringIntegrationMetricReader implements MetricReader {
private final IntegrationManagementConfigurer managementConfigurer;
private final IntegrationManagementConfigurer configurer;
public SpringIntegrationMetricReader(IntegrationManagementConfigurer managementConfigurer) {
this.managementConfigurer = managementConfigurer;
public SpringIntegrationMetricReader(IntegrationManagementConfigurer configurer) {
this.configurer = configurer;
}
@Override
@ -54,60 +54,79 @@ public class SpringIntegrationMetricReader implements MetricReader {
@Override
public Iterable<Metric<?>> findAll() {
List<Metric<?>> metrics = new ArrayList<Metric<?>>();
for (String name : this.managementConfigurer.getChannelNames()) {
MessageChannelMetrics channelMetrics = this.managementConfigurer.getChannelMetrics(name);
String prefix = "integration.channel." + 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 : 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.handlerCount",
this.managementConfigurer.getHandlerNames().length));
metrics.add(new Metric<Integer>("integration.channelCount",
this.managementConfigurer.getChannelNames().length));
metrics.add(new Metric<Integer>("integration.sourceCount",
this.managementConfigurer.getSourceNames().length));
return metrics;
List<Metric<?>> result = new ArrayList<Metric<?>>();
String[] channelNames = this.configurer.getChannelNames();
String[] handlerNames = this.configurer.getHandlerNames();
String[] sourceNames = this.configurer.getSourceNames();
addChannelMetrics(result, channelNames);
addHandlerMetrics(result, handlerNames);
addSourceMetrics(result, sourceNames);
result.add(new Metric<Integer>("integration.handlerCount", handlerNames.length));
result.add(new Metric<Integer>("integration.channelCount", channelNames.length));
result.add(new Metric<Integer>("integration.sourceCount", sourceNames.length));
return result;
}
private Collection<? extends Metric<?>> getStatistics(String name,
Statistics statistic) {
private void addChannelMetrics(List<Metric<?>> result, String[] names) {
for (String name : names) {
addChannelMetrics(result, name, this.configurer.getChannelMetrics(name));
}
}
private void addChannelMetrics(List<Metric<?>> result, String name,
MessageChannelMetrics metrics) {
String prefix = "integration.channel." + name;
result.addAll(getStatistics(prefix + ".errorRate", metrics.getErrorRate()));
result.add(new Metric<Long>(prefix + ".sendCount", metrics.getSendCountLong()));
result.addAll(getStatistics(prefix + ".sendRate", metrics.getSendRate()));
if (metrics instanceof PollableChannelManagement) {
result.add(new Metric<Long>(prefix + ".receiveCount",
((PollableChannelManagement) metrics).getReceiveCountLong()));
}
}
private void addHandlerMetrics(List<Metric<?>> result, String[] names) {
for (String name : names) {
addHandlerMetrics(result, name, this.configurer.getHandlerMetrics(name));
}
}
private void addHandlerMetrics(List<Metric<?>> result, String name,
MessageHandlerMetrics metrics) {
String prefix = "integration.handler." + name;
result.addAll(getStatistics(prefix + ".duration", metrics.getDuration()));
long activeCount = metrics.getActiveCountLong();
result.add(new Metric<Long>(prefix + ".activeCount", activeCount));
}
private void addSourceMetrics(List<Metric<?>> result, String[] names) {
for (String name : names) {
addSourceMetrics(result, name, this.configurer.getSourceMetrics(name));
}
}
private void addSourceMetrics(List<Metric<?>> result, String name,
MessageSourceMetrics sourceMetrics) {
String prefix = "integration.source." + name;
result.add(new Metric<Long>(prefix + ".messageCount",
sourceMetrics.getMessageCountLong()));
}
private Collection<? extends Metric<?>> getStatistics(String name, Statistics stats) {
List<Metric<?>> metrics = new ArrayList<Metric<?>>();
metrics.add(new Metric<Double>(name + ".mean", statistic.getMean()));
metrics.add(new Metric<Double>(name + ".max", statistic.getMax()));
metrics.add(new Metric<Double>(name + ".min", statistic.getMin()));
metrics.add(
new Metric<Double>(name + ".stdev", statistic.getStandardDeviation()));
metrics.add(new Metric<Long>(name + ".count", statistic.getCountLong()));
metrics.add(new Metric<Double>(name + ".mean", stats.getMean()));
metrics.add(new Metric<Double>(name + ".max", stats.getMax()));
metrics.add(new Metric<Double>(name + ".min", stats.getMin()));
metrics.add(new Metric<Double>(name + ".stdev", stats.getStandardDeviation()));
metrics.add(new Metric<Long>(name + ".count", stats.getCountLong()));
return metrics;
}
@Override
public long count() {
int totalChannelCount = this.managementConfigurer.getChannelNames().length;
int totalHandlerCount = this.managementConfigurer.getHandlerNames().length;
int totalSourceCount = this.managementConfigurer.getSourceNames().length;
int totalChannelCount = this.configurer.getChannelNames().length;
int totalHandlerCount = this.configurer.getHandlerNames().length;
int totalSourceCount = this.configurer.getSourceNames().length;
return totalChannelCount + totalHandlerCount + totalSourceCount;
}

View File

@ -92,7 +92,7 @@ public class PublicMetricsAutoConfigurationTests {
public void metricReaderPublicMetrics() throws Exception {
load();
assertThat(this.context.getBeansOfType(MetricReaderPublicMetrics.class))
.hasSize(1);
.hasSize(2);
}
@Test

View File

@ -16,7 +16,6 @@
package org.springframework.boot.actuate.metrics.integration;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -53,7 +52,7 @@ public class SpringIntegrationMetricReaderNoJmxTests {
}
@Configuration
@Import({IntegrationAutoConfiguration.class, PublicMetricsAutoConfiguration.class})
@Import({ IntegrationAutoConfiguration.class, PublicMetricsAutoConfiguration.class })
protected static class TestConfiguration {
}

View File

@ -56,7 +56,8 @@ public class SpringIntegrationMetricReaderTests {
protected static class TestConfiguration {
@Bean
public SpringIntegrationMetricReader reader(IntegrationManagementConfigurer managementConfigurer) {
public SpringIntegrationMetricReader reader(
IntegrationManagementConfigurer managementConfigurer) {
return new SpringIntegrationMetricReader(managementConfigurer);
}

View File

@ -98,10 +98,9 @@ public class IntegrationAutoConfiguration {
}
@Configuration
@ConditionalOnClass({EnableIntegrationManagement.class, EnableIntegrationMBeanExport.class})
@ConditionalOnMissingBean(value = IntegrationManagementConfigurer.class,
name = IntegrationManagementConfigurer.MANAGEMENT_CONFIGURER_NAME,
search = SearchStrategy.CURRENT)
@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 {

View File

@ -87,14 +87,17 @@ 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();
Object bean = this.context
.getBean(IntegrationManagementConfigurer.MANAGEMENT_CONFIGURER_NAME);
assertThat(bean).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();
assertThat(this.context.getBeansOfType(IntegrationManagementConfigurer.class))
.isEmpty();
}
@Test