diff --git a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/MetricsEndpoint.java b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/MetricsEndpoint.java index 9a69fc97ba..542c4c5d90 100644 --- a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/MetricsEndpoint.java +++ b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/MetricsEndpoint.java @@ -121,7 +121,11 @@ public class MetricsEndpoint { private void mergeMeasurements(Map samples, Meter meter) { meter.measure().forEach((measurement) -> samples.merge(measurement.getStatistic(), - measurement.getValue(), Double::sum)); + measurement.getValue(), mergeFunction(measurement.getStatistic()))); + } + + private BiFunction mergeFunction(Statistic statistic) { + return Statistic.MAX.equals(statistic) ? Double::max : Double::sum; } private Map> getAvailableTags(List meters) { diff --git a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/MetricsEndpointTests.java b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/MetricsEndpointTests.java index 5f724c000e..059084f4c0 100644 --- a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/MetricsEndpointTests.java +++ b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/MetricsEndpointTests.java @@ -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 getCount(MetricsEndpoint.MetricResponse response) { return response.getMeasurements().stream() .filter((ms) -> ms.getStatistic().equals(Statistic.COUNT)).findAny()