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

@@ -16,13 +16,18 @@
package org.springframework.boot.actuate.autoconfigure;
import java.util.Collection;
import java.util.Collections;
import java.util.concurrent.Executor;
import org.junit.Test;
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.Metric;
import org.springframework.boot.actuate.metrics.reader.MetricReader;
import org.springframework.boot.actuate.metrics.rich.RichGauge;
import org.springframework.boot.actuate.metrics.rich.RichGaugeReader;
import org.springframework.boot.actuate.metrics.writer.DefaultCounterService;
import org.springframework.boot.actuate.metrics.writer.DefaultGaugeService;
import org.springframework.boot.actuate.metrics.writer.MetricWriter;
@@ -41,9 +46,11 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/**
* Tests for {@link MetricRepositoryAutoConfiguration}.
@@ -115,6 +122,44 @@ public class MetricRepositoryAutoConfigurationTests {
context.close();
}
@Test
public void richGaugePublicMetrics() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
RichGaugeReaderConfig.class, MetricRepositoryAutoConfiguration.class);
RichGaugeReader richGaugeReader = context.getBean(RichGaugeReader.class);
assertNotNull(richGaugeReader);
when(richGaugeReader.findAll()).thenReturn(
Collections.singletonList(new RichGauge("bar", 3.7d)));
RichGaugeReaderPublicMetrics publicMetrics = context
.getBean(RichGaugeReaderPublicMetrics.class);
assertNotNull(publicMetrics);
Collection<Metric<?>> metrics = publicMetrics.metrics();
assertNotNull(metrics);
assertEquals(metrics.size(), 6);
assertHasMetric(metrics, new Metric<Double>("bar.val", 3.7d));
assertHasMetric(metrics, new Metric<Double>("bar.avg", 3.7d));
assertHasMetric(metrics, new Metric<Double>("bar.min", 3.7d));
assertHasMetric(metrics, new Metric<Double>("bar.max", 3.7d));
assertHasMetric(metrics, new Metric<Double>("bar.alpha", -1.d));
assertHasMetric(metrics, new Metric<Long>("bar.count", 1L));
context.close();
}
private void assertHasMetric(Collection<Metric<?>> metrics, Metric<?> metric) {
for (Metric<?> m : metrics) {
if (m.getValue().equals(metric.getValue())
&& m.getName().equals(metric.getName())) {
return;
}
}
fail("Metric " + metric.toString() + " not found in " + metrics.toString());
}
@Configuration
public static class SyncTaskExecutorConfiguration {
@@ -149,4 +194,12 @@ public class MetricRepositoryAutoConfigurationTests {
}
}
@Configuration
static class RichGaugeReaderConfig {
@Bean
public RichGaugeReader richGaugeReader() {
return mock(RichGaugeReader.class);
}
}
}

View File

@@ -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));
}
}