Aggregate max statistics in metrics endpoint with Double#max

See gh-11852
pull/11848/merge
Jon Schneider 7 years ago committed by Andy Wilkinson
parent ba54cbddf6
commit 92287f75f7

@ -121,7 +121,11 @@ public class MetricsEndpoint {
private void mergeMeasurements(Map<Statistic, Double> samples, Meter meter) {
meter.measure().forEach((measurement) -> samples.merge(measurement.getStatistic(),
measurement.getValue(), Double::sum));
measurement.getValue(), mergeFunction(measurement.getStatistic())));
}
private BiFunction<Double, Double, Double> mergeFunction(Statistic statistic) {
return Statistic.MAX.equals(statistic) ? Double::max : Double::sum;
}
private Map<String, Set<String>> getAvailableTags(List<Meter> meters) {

@ -18,6 +18,7 @@ package org.springframework.boot.actuate.metrics;
import java.util.Collections;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;
import io.micrometer.core.instrument.MeterRegistry;
@ -129,6 +130,31 @@ public class MetricsEndpointTests {
assertThat(response).isNull();
}
@Test
public void maxAggregation() {
SimpleMeterRegistry reg = new SimpleMeterRegistry();
reg.timer("timer", "k", "v1").record(1, TimeUnit.SECONDS);
reg.timer("timer", "k", "v2").record(2, TimeUnit.SECONDS);
assertMetricHasStatisticEqualTo(reg, "timer", Statistic.MAX, 2.0);
}
@Test
public void countAggregation() {
SimpleMeterRegistry reg = new SimpleMeterRegistry();
reg.counter("counter", "k", "v1").increment();
reg.counter("counter", "k", "v2").increment();
assertMetricHasStatisticEqualTo(reg, "counter", Statistic.COUNT, 2.0);
}
private void assertMetricHasStatisticEqualTo(MeterRegistry registry, String metricName, Statistic stat, Double value) {
MetricsEndpoint endpoint = new MetricsEndpoint(registry);
assertThat(endpoint.metric(metricName, Collections.emptyList()).getMeasurements()
.stream().filter(s -> s.getStatistic().equals(stat)).findAny())
.hasValueSatisfying(s -> assertThat(s.getValue()).isEqualTo(value));
}
private Optional<Double> getCount(MetricsEndpoint.MetricResponse response) {
return response.getMeasurements().stream()
.filter((ms) -> ms.getStatistic().equals(Statistic.COUNT)).findAny()

Loading…
Cancel
Save