Commit 42135cd5 authored by Andy Wilkinson's avatar Andy Wilkinson

Polish "Aggregate max statistics in metrics endpoint with Double#max"

Closes gh-11852
parent 92287f75
...@@ -120,8 +120,10 @@ public class MetricsEndpoint { ...@@ -120,8 +120,10 @@ 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(), mergeFunction(measurement.getStatistic()))); .forEach((measurement) -> samples.merge(measurement.getStatistic(),
measurement.getValue(),
mergeFunction(measurement.getStatistic())));
} }
private BiFunction<Double, Double, Double> mergeFunction(Statistic statistic) { private BiFunction<Double, Double, Double> mergeFunction(Statistic statistic) {
......
...@@ -135,7 +135,6 @@ public class MetricsEndpointTests { ...@@ -135,7 +135,6 @@ public class MetricsEndpointTests {
SimpleMeterRegistry reg = new SimpleMeterRegistry(); SimpleMeterRegistry reg = new SimpleMeterRegistry();
reg.timer("timer", "k", "v1").record(1, TimeUnit.SECONDS); reg.timer("timer", "k", "v1").record(1, TimeUnit.SECONDS);
reg.timer("timer", "k", "v2").record(2, TimeUnit.SECONDS); reg.timer("timer", "k", "v2").record(2, TimeUnit.SECONDS);
assertMetricHasStatisticEqualTo(reg, "timer", Statistic.MAX, 2.0); assertMetricHasStatisticEqualTo(reg, "timer", Statistic.MAX, 2.0);
} }
...@@ -144,21 +143,22 @@ public class MetricsEndpointTests { ...@@ -144,21 +143,22 @@ public class MetricsEndpointTests {
SimpleMeterRegistry reg = new SimpleMeterRegistry(); SimpleMeterRegistry reg = new SimpleMeterRegistry();
reg.counter("counter", "k", "v1").increment(); reg.counter("counter", "k", "v1").increment();
reg.counter("counter", "k", "v2").increment(); reg.counter("counter", "k", "v2").increment();
assertMetricHasStatisticEqualTo(reg, "counter", Statistic.COUNT, 2.0); assertMetricHasStatisticEqualTo(reg, "counter", Statistic.COUNT, 2.0);
} }
private void assertMetricHasStatisticEqualTo(MeterRegistry registry, String metricName, Statistic stat, Double value) { private void assertMetricHasStatisticEqualTo(MeterRegistry registry,
String metricName, Statistic stat, Double value) {
MetricsEndpoint endpoint = new MetricsEndpoint(registry); MetricsEndpoint endpoint = new MetricsEndpoint(registry);
assertThat(endpoint.metric(metricName, Collections.emptyList()).getMeasurements() assertThat(endpoint.metric(metricName, Collections.emptyList()).getMeasurements()
.stream().filter(s -> s.getStatistic().equals(stat)).findAny()) .stream().filter((sample) -> sample.getStatistic().equals(stat))
.hasValueSatisfying(s -> assertThat(s.getValue()).isEqualTo(value)); .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) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment