parent
1f9515cd31
commit
9af8fdb8a1
@ -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;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,71 @@
|
||||
/*
|
||||
* 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.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.boot.actuate.metrics.Metric;
|
||||
import org.springframework.boot.actuate.metrics.rich.InMemoryRichGaugeRepository;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* Tests for {@link RichGaugeReaderPublicMetrics}.
|
||||
*
|
||||
* @author Johannes Stelzer
|
||||
*/
|
||||
public class RichGaugeReaderPublicMetricsTests {
|
||||
|
||||
@Test
|
||||
public void testMetrics() throws Exception {
|
||||
InMemoryRichGaugeRepository repository = new InMemoryRichGaugeRepository();
|
||||
|
||||
repository.set(new Metric<Double>("a", 0.d, new Date()));
|
||||
repository.set(new Metric<Double>("a", 0.5d, new Date()));
|
||||
|
||||
RichGaugeReaderPublicMetrics metrics = new RichGaugeReaderPublicMetrics(
|
||||
repository);
|
||||
|
||||
Map<String, Metric<?>> results = new HashMap<String, Metric<?>>();
|
||||
for (Metric<?> metric : metrics.metrics()) {
|
||||
results.put(metric.getName(), metric);
|
||||
}
|
||||
assertTrue(results.containsKey("a.val"));
|
||||
assertThat(results.get("a.val").getValue().doubleValue(), equalTo(0.5d));
|
||||
|
||||
assertTrue(results.containsKey("a.avg"));
|
||||
assertThat(results.get("a.avg").getValue().doubleValue(), equalTo(0.25d));
|
||||
|
||||
assertTrue(results.containsKey("a.min"));
|
||||
assertThat(results.get("a.min").getValue().doubleValue(), equalTo(0.0d));
|
||||
|
||||
assertTrue(results.containsKey("a.max"));
|
||||
assertThat(results.get("a.max").getValue().doubleValue(), equalTo(0.5d));
|
||||
|
||||
assertTrue(results.containsKey("a.count"));
|
||||
assertThat(results.get("a.count").getValue().longValue(), equalTo(2L));
|
||||
|
||||
assertTrue(results.containsKey("a.alpha"));
|
||||
assertThat(results.get("a.alpha").getValue().doubleValue(), equalTo(-1.d));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue