From 6db5ca97d344ecc73a090f901bb167eb3b6ac554 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Thu, 30 Jan 2020 14:05:53 +0000 Subject: [PATCH] Sort names alphabetically in metrics list response Closes gh-19934 --- .../boot/actuate/metrics/MetricsEndpoint.java | 6 +++--- .../actuate/metrics/MetricsEndpointTests.java | 17 +++++++++++------ 2 files changed, 14 insertions(+), 9 deletions(-) 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 1295eef949..8e88b24140 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 @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2020 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. @@ -21,10 +21,10 @@ import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedHashMap; -import java.util.LinkedHashSet; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.TreeSet; import java.util.function.BiFunction; import java.util.stream.Collectors; @@ -58,7 +58,7 @@ public class MetricsEndpoint { @ReadOperation public ListNamesResponse listNames() { - Set names = new LinkedHashSet<>(); + Set names = new TreeSet<>(); collectNames(names, this.registry); return new ListNamesResponse(names); } 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 12e3aae028..ef5e128999 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 @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2020 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. @@ -54,11 +54,16 @@ class MetricsEndpointTests { @Test void listNamesProducesListOfUniqueMeterNames() { - this.registry.counter("com.example.foo"); - this.registry.counter("com.example.bar"); - this.registry.counter("com.example.foo"); + this.registry.counter("com.example.alpha"); + this.registry.counter("com.example.charlie"); + this.registry.counter("com.example.bravo"); + this.registry.counter("com.example.delta"); + this.registry.counter("com.example.delta"); + this.registry.counter("com.example.echo"); + this.registry.counter("com.example.bravo"); MetricsEndpoint.ListNamesResponse result = this.endpoint.listNames(); - assertThat(result.getNames()).containsOnlyOnce("com.example.foo", "com.example.bar"); + assertThat(result.getNames()).containsExactly("com.example.alpha", "com.example.bravo", "com.example.charlie", + "com.example.delta", "com.example.echo"); } @Test @@ -71,7 +76,7 @@ class MetricsEndpointTests { reg1.counter("counter1").increment(); reg2.counter("counter2").increment(); MetricsEndpoint endpoint = new MetricsEndpoint(composite); - assertThat(endpoint.listNames().getNames()).containsOnly("counter1", "counter2"); + assertThat(endpoint.listNames().getNames()).containsExactly("counter1", "counter2"); } @Test