Allow multiple readers/writers to be @ActuatorMetric*

In principle you might have multiple "system" repositories, all
of which you want to go to public metrics or not be metrics exporters.
This change adds a new annotation and renames the old one, so that
reades and writers can be distinguished, and also changes the
autowiring of them to accept multiple values.

Also adds automatic public metrics for Spring Integration.
pull/3002/head
Dave Syer 10 years ago
parent 6f38b54800
commit 5ceb35473d

@ -127,6 +127,16 @@
<artifactId>spring-data-solr</artifactId>
<optional>true</optional>
</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>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>

@ -37,6 +37,6 @@ import org.springframework.beans.factory.annotation.Qualifier;
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface ActuatorMetricRepository {
public @interface ActuatorMetricReader {
}

@ -0,0 +1,42 @@
/*
* Copyright 2012-2015 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.autoconfigure;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.beans.factory.annotation.Qualifier;
/**
* Qualifier annotation for a metric repository that is used by the actuator (to
* distinguish it from others that might be installed by the user).
*
* @author Dave Syer
*/
@Qualifier
@Target({ ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.TYPE,
ElementType.ANNOTATION_TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface ActuatorMetricWriter {
}

@ -18,6 +18,7 @@ package org.springframework.boot.actuate.autoconfigure;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
@ -48,11 +49,11 @@ public class MetricExportAutoConfiguration {
private MetricExportProperties metrics;
@Autowired(required = false)
@ActuatorMetricRepository
private MetricWriter actuatorMetricRepository;
@ActuatorMetricWriter
private List<MetricWriter> actuatorMetrics = Collections.emptyList();
@Autowired(required = false)
@ActuatorMetricRepository
@ActuatorMetricReader
private MetricReader reader;
@Bean
@ -61,12 +62,9 @@ public class MetricExportAutoConfiguration {
Map<String, MetricWriter> writers = new HashMap<String, MetricWriter>();
if (this.reader != null) {
writers.putAll(this.writers);
if (this.actuatorMetricRepository != null
&& writers.containsValue(this.actuatorMetricRepository)) {
for (String name : this.writers.keySet()) {
if (writers.get(name).equals(this.actuatorMetricRepository)) {
writers.remove(name);
}
for (String name : this.writers.keySet()) {
if (this.actuatorMetrics.contains(writers.get(name))) {
writers.remove(name);
}
}
MetricExporters exporters = new MetricExporters(this.reader, writers,
@ -80,5 +78,4 @@ public class MetricExportAutoConfiguration {
}
};
}
}

@ -90,7 +90,7 @@ public class MetricRepositoryAutoConfiguration {
static class LegacyMetricServicesConfiguration {
@Autowired
@ActuatorMetricRepository
@ActuatorMetricReader
private MetricWriter writer;
@Bean
@ -125,7 +125,7 @@ public class MetricRepositoryAutoConfiguration {
}
@Bean
@ActuatorMetricRepository
@ActuatorMetricReader
@ConditionalOnMissingBean
public BufferMetricReader actuatorMetricReader(CounterBuffers counters,
GaugeBuffers gauges) {
@ -151,7 +151,7 @@ public class MetricRepositoryAutoConfiguration {
static class LegacyMetricRepositoryConfiguration {
@Bean
@ActuatorMetricRepository
@ActuatorMetricReader
public InMemoryMetricRepository actuatorMetricRepository() {
return new InMemoryMetricRepository();
}

@ -16,6 +16,9 @@
package org.springframework.boot.actuate.autoconfigure;
import java.util.Collections;
import java.util.List;
import javax.servlet.Servlet;
import javax.sql.DataSource;
@ -29,8 +32,9 @@ import org.springframework.boot.actuate.endpoint.PublicMetrics;
import org.springframework.boot.actuate.endpoint.RichGaugeReaderPublicMetrics;
import org.springframework.boot.actuate.endpoint.SystemPublicMetrics;
import org.springframework.boot.actuate.endpoint.TomcatPublicMetrics;
import org.springframework.boot.actuate.metrics.integration.SpringIntegrationMetricReader;
import org.springframework.boot.actuate.metrics.reader.CompositeMetricReader;
import org.springframework.boot.actuate.metrics.reader.MetricReader;
import org.springframework.boot.actuate.metrics.repository.InMemoryMetricRepository;
import org.springframework.boot.actuate.metrics.rich.RichGaugeReader;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
@ -38,12 +42,17 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
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.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.lang.UsesJava7;
/**
* {@link EnableAutoConfiguration Auto-configuration} for {@link PublicMetrics}.
@ -56,12 +65,13 @@ import org.springframework.context.annotation.Configuration;
@Configuration
@AutoConfigureBefore(EndpointAutoConfiguration.class)
@AutoConfigureAfter({ DataSourceAutoConfiguration.class, CacheAutoConfiguration.class,
MetricRepositoryAutoConfiguration.class, CacheStatisticsAutoConfiguration.class })
MetricRepositoryAutoConfiguration.class, CacheStatisticsAutoConfiguration.class,
IntegrationAutoConfiguration.class })
public class PublicMetricsAutoConfiguration {
@Autowired(required = false)
@ActuatorMetricRepository
private MetricReader metricReader = new InMemoryMetricRepository();
@ActuatorMetricReader
private List<MetricReader> metricReaders = Collections.emptyList();
@Bean
public SystemPublicMetrics systemPublicMetrics() {
@ -70,7 +80,7 @@ public class PublicMetricsAutoConfiguration {
@Bean
public MetricReaderPublicMetrics metricReaderPublicMetrics() {
return new MetricReaderPublicMetrics(this.metricReader);
return new MetricReaderPublicMetrics(new CompositeMetricReader(this.metricReaders.toArray(new MetricReader[0])));
}
@Bean
@ -120,4 +130,21 @@ public class PublicMetricsAutoConfiguration {
}
@Configuration
@ConditionalOnClass(IntegrationMBeanExporter.class)
@ConditionalOnBean(IntegrationMBeanExporter.class)
@ConditionalOnJava(JavaVersion.SEVEN)
@UsesJava7
static class IntegrationMetricsConfiguration {
@Bean
@ConditionalOnMissingBean
public MetricReaderPublicMetrics springIntegrationPublicMetrics(
IntegrationMBeanExporter exporter) {
return new MetricReaderPublicMetrics(new SpringIntegrationMetricReader(
exporter));
}
}
}

@ -0,0 +1,81 @@
/*
* Copyright 2015 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 java.util.ArrayList;
import java.util.Collection;
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.Statistics;
import org.springframework.lang.UsesJava7;
/**
* A {@link MetricReader} for Spring Integration metrics (as provided by spring-integration-jmx).
*
* @author Dave Syer
*
*/
@UsesJava7
public class SpringIntegrationMetricReader implements MetricReader {
private final IntegrationMBeanExporter exporter;
public SpringIntegrationMetricReader(IntegrationMBeanExporter exporter) {
this.exporter = exporter;
}
@Override
public Metric<?> findOne(String metricName) {
return null;
}
@Override
public Iterable<Metric<?>> findAll() {
List<Metric<?>> metrics = new ArrayList<Metric<?>>();
for (String name : exporter.getChannelNames()) {
metrics.addAll(getStatistics("integration.channel." + name + ".errorRate", exporter.getChannelErrorRate(name)));
metrics.addAll(getStatistics("integration.channel." + name + ".sendRate", exporter.getChannelSendRate(name)));
metrics.add(new Metric<Long>("integration.channel." + name + ".receiveCount", exporter.getChannelReceiveCountLong(name)));
}
for (String name : exporter.getHandlerNames()) {
metrics.addAll(getStatistics("integration.handler." + name + ".duration", exporter.getHandlerDuration(name)));
}
metrics.add(new Metric<Long>("integration.activeHandlerCount", exporter.getActiveHandlerCountLong()));
metrics.add(new Metric<Integer>("integration.handlerCount", exporter.getHandlerCount()));
metrics.add(new Metric<Integer>("integration.channelCount", exporter.getChannelCount()));
metrics.add(new Metric<Integer>("integration.queuedMessageCount", exporter.getQueuedMessageCount()));
return metrics;
}
private Collection<? extends Metric<?>> getStatistics(String name, Statistics statistic) {
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()));
return metrics;
}
@Override
public long count() {
return exporter.getChannelCount()*11 + exporter.getHandlerCount()*5 + 4;
}
}

@ -0,0 +1,43 @@
package org.springframework.boot.actuate.metrics.integration;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.metrics.integration.SpringIntegrationMetricReaderTests.TestConfiguration;
import org.springframework.boot.autoconfigure.integration.IntegrationAutoConfiguration;
import org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration;
import org.springframework.boot.test.IntegrationTest;
import org.springframework.boot.test.SpringApplicationConfiguration;
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.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes=TestConfiguration.class)
@IntegrationTest("spring.jmx.enabled=true")
@DirtiesContext
public class SpringIntegrationMetricReaderTests {
@Autowired
private SpringIntegrationMetricReader reader;
@Test
public void test() {
assertTrue(reader.count()>0);
}
@Configuration
@Import({JmxAutoConfiguration.class, IntegrationAutoConfiguration.class})
protected static class TestConfiguration {
@Bean
public SpringIntegrationMetricReader reader(IntegrationMBeanExporter exporter) {
return new SpringIntegrationMetricReader(exporter);
}
}
}
Loading…
Cancel
Save