Merge pull request #11852 from Jon Schneider

* gh-11852:
  Polish "Aggregate max statistics in metrics endpoint with Double#max"
  Aggregate max statistics in metrics endpoint with Double#max
pull/11848/merge
Andy Wilkinson 7 years ago
commit ef49c029a1

@ -120,8 +120,14 @@ public class MetricsEndpoint {
} }
private void mergeMeasurements(Map<Statistic, Double> samples, Meter meter) { private void mergeMeasurements(Map<Statistic, Double> samples, Meter meter) {
meter.measure().forEach((measurement) -> samples.merge(measurement.getStatistic(), meter.measure()
measurement.getValue(), Double::sum)); .forEach((measurement) -> samples.merge(measurement.getStatistic(),
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) { 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.Collections;
import java.util.Optional; import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream; import java.util.stream.Stream;
import io.micrometer.core.instrument.MeterRegistry; import io.micrometer.core.instrument.MeterRegistry;
@ -129,10 +130,35 @@ public class MetricsEndpointTests {
assertThat(response).isNull(); 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((sample) -> sample.getStatistic().equals(stat))
.findAny()).hasValueSatisfying(
(sample) -> assertThat(sample.getValue()).isEqualTo(value));
}
private Optional<Double> getCount(MetricsEndpoint.MetricResponse response) { private Optional<Double> getCount(MetricsEndpoint.MetricResponse response) {
return response.getMeasurements().stream() return response.getMeasurements().stream()
.filter((ms) -> ms.getStatistic().equals(Statistic.COUNT)).findAny() .filter((sample) -> sample.getStatistic().equals(Statistic.COUNT))
.map(MetricsEndpoint.Sample::getValue); .findAny().map(MetricsEndpoint.Sample::getValue);
} }
private Stream<String> availableTagKeys(MetricsEndpoint.MetricResponse response) { private Stream<String> availableTagKeys(MetricsEndpoint.MetricResponse response) {

Loading…
Cancel
Save