Expose RichGauges in MetricsEndpoint via PublicMetrics

Fixes gh-1635
This commit is contained in:
joshiste
2014-09-28 21:11:57 +02:00
committed by Dave Syer
parent 1f9515cd31
commit 9af8fdb8a1
6 changed files with 216 additions and 14 deletions

View File

@@ -21,11 +21,14 @@ import java.util.concurrent.Executor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.actuate.endpoint.PublicMetrics;
import org.springframework.boot.actuate.endpoint.RichGaugeReaderPublicMetrics;
import org.springframework.boot.actuate.metrics.CounterService;
import org.springframework.boot.actuate.metrics.GaugeService;
import org.springframework.boot.actuate.metrics.export.Exporter;
import org.springframework.boot.actuate.metrics.repository.InMemoryMetricRepository;
import org.springframework.boot.actuate.metrics.repository.MetricRepository;
import org.springframework.boot.actuate.metrics.rich.RichGaugeReader;
import org.springframework.boot.actuate.metrics.writer.CodahaleMetricWriter;
import org.springframework.boot.actuate.metrics.writer.CompositeMetricWriter;
import org.springframework.boot.actuate.metrics.writer.DefaultCounterService;
@@ -34,6 +37,7 @@ import org.springframework.boot.actuate.metrics.writer.MessageChannelMetricWrite
import org.springframework.boot.actuate.metrics.writer.MetricWriter;
import org.springframework.boot.actuate.metrics.writer.MetricWriterMessageHandler;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
@@ -69,7 +73,8 @@ import com.codahale.metrics.MetricRegistry;
* In addition if Codahale's metrics library is on the classpath a {@link MetricRegistry}
* will be created and wired up to the counter and gauge services in addition to the basic
* repository. Users can create Codahale metrics by prefixing their metric names with the
* appropriate type (e.g. "histogram.*", "meter.*").
* appropriate type (e.g. "histogram.*", "meter.*") and sending them to the standard
* <code>GaugeService</code> or <code>CounterService</code>.
* </p>
* <p>
* By default all metric updates go to all {@link MetricWriter} instances in the
@@ -117,6 +122,12 @@ public class MetricRepositoryAutoConfiguration {
}
@Bean
@ConditionalOnBean(RichGaugeReader.class)
public PublicMetrics richGaugePublicMetrics(RichGaugeReader richGaugeReader) {
return new RichGaugeReaderPublicMetrics(richGaugeReader);
}
@Configuration
@ConditionalOnClass(MessageChannel.class)
static class MetricsChannelConfiguration {

View File

@@ -0,0 +1,67 @@
/*
* Copyright 2012-2014 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.endpoint;
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.rich.RichGauge;
import org.springframework.boot.actuate.metrics.rich.RichGaugeReader;
import org.springframework.util.Assert;
/**
* {@link PublicMetrics} exposed from a {@link RichGaugeReader}.
*
* @author Johannes Stelzer
* @since 1.2
*/
public class RichGaugeReaderPublicMetrics implements PublicMetrics {
private final RichGaugeReader richGaugeReader;
public RichGaugeReaderPublicMetrics(RichGaugeReader richGaugeReader) {
Assert.notNull(richGaugeReader, "RichGaugeReader must not be null");
this.richGaugeReader = richGaugeReader;
}
@Override
public Collection<Metric<?>> metrics() {
List<Metric<?>> result = new ArrayList<Metric<?>>();
for (RichGauge richGauge : this.richGaugeReader.findAll()) {
result.addAll(convert(richGauge));
}
return result;
}
private List<Metric<?>> convert(RichGauge richGauge) {
List<Metric<?>> result = new ArrayList<Metric<?>>(6);
result.add(new Metric<Double>(richGauge.getName() + RichGauge.AVG, richGauge
.getAverage()));
result.add(new Metric<Double>(richGauge.getName() + RichGauge.VAL, richGauge.getValue()));
result.add(new Metric<Double>(richGauge.getName() + RichGauge.MIN, richGauge.getMin()));
result.add(new Metric<Double>(richGauge.getName() + RichGauge.MAX, richGauge.getMax()));
result.add(new Metric<Double>(richGauge.getName() + RichGauge.ALPHA, richGauge
.getAlpha()));
result.add(new Metric<Long>(richGauge.getName() + RichGauge.COUNT, richGauge.getCount()));
return result;
}
}

View File

@@ -33,13 +33,6 @@ import org.springframework.boot.actuate.metrics.repository.MultiMetricRepository
*/
public class MultiMetricRichGaugeReader implements RichGaugeReader {
private static final String COUNT = ".count";
private static final String MAX = ".max";
private static final String MIN = ".min";
private static final String AVG = ".avg";
private static final String ALPHA = ".alpha";
private static final String VAL = ".val";
private final MultiMetricRepository repository;
public MultiMetricRichGaugeReader(MultiMetricRepository repository) {
@@ -56,22 +49,22 @@ public class MultiMetricRichGaugeReader implements RichGaugeReader {
double max = 0.;
long count = 0;
for (Metric<?> metric : metrics) {
if (metric.getName().endsWith(VAL)) {
if (metric.getName().endsWith(RichGauge.VAL)) {
value = metric.getValue().doubleValue();
}
else if (metric.getName().endsWith(ALPHA)) {
else if (metric.getName().endsWith(RichGauge.ALPHA)) {
alpha = metric.getValue().doubleValue();
}
else if (metric.getName().endsWith(AVG)) {
else if (metric.getName().endsWith(RichGauge.AVG)) {
average = metric.getValue().doubleValue();
}
else if (metric.getName().endsWith(MIN)) {
else if (metric.getName().endsWith(RichGauge.MIN)) {
min = metric.getValue().doubleValue();
}
else if (metric.getName().endsWith(MAX)) {
else if (metric.getName().endsWith(RichGauge.MAX)) {
max = metric.getValue().doubleValue();
}
else if (metric.getName().endsWith(COUNT)) {
else if (metric.getName().endsWith(RichGauge.COUNT)) {
count = metric.getValue().longValue();
}
}

View File

@@ -31,6 +31,13 @@ import org.springframework.util.Assert;
*/
public final class RichGauge {
public static final String COUNT = ".count";
public static final String MAX = ".max";
public static final String MIN = ".min";
public static final String AVG = ".avg";
public static final String ALPHA = ".alpha";
public static final String VAL = ".val";
private final String name;
private double value;