Skip to content

Commit 6db5ca9

Browse files
committed
Sort names alphabetically in metrics list response
Closes gh-19934
1 parent 28442b5 commit 6db5ca9

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/MetricsEndpoint.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,10 +21,10 @@
2121
import java.util.HashMap;
2222
import java.util.HashSet;
2323
import java.util.LinkedHashMap;
24-
import java.util.LinkedHashSet;
2524
import java.util.List;
2625
import java.util.Map;
2726
import java.util.Set;
27+
import java.util.TreeSet;
2828
import java.util.function.BiFunction;
2929
import java.util.stream.Collectors;
3030

@@ -58,7 +58,7 @@ public MetricsEndpoint(MeterRegistry registry) {
5858

5959
@ReadOperation
6060
public ListNamesResponse listNames() {
61-
Set<String> names = new LinkedHashSet<>();
61+
Set<String> names = new TreeSet<>();
6262
collectNames(names, this.registry);
6363
return new ListNamesResponse(names);
6464
}

spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/MetricsEndpointTests.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -54,11 +54,16 @@ void listNamesHandlesEmptyListOfMeters() {
5454

5555
@Test
5656
void listNamesProducesListOfUniqueMeterNames() {
57-
this.registry.counter("com.example.foo");
58-
this.registry.counter("com.example.bar");
59-
this.registry.counter("com.example.foo");
57+
this.registry.counter("com.example.alpha");
58+
this.registry.counter("com.example.charlie");
59+
this.registry.counter("com.example.bravo");
60+
this.registry.counter("com.example.delta");
61+
this.registry.counter("com.example.delta");
62+
this.registry.counter("com.example.echo");
63+
this.registry.counter("com.example.bravo");
6064
MetricsEndpoint.ListNamesResponse result = this.endpoint.listNames();
61-
assertThat(result.getNames()).containsOnlyOnce("com.example.foo", "com.example.bar");
65+
assertThat(result.getNames()).containsExactly("com.example.alpha", "com.example.bravo", "com.example.charlie",
66+
"com.example.delta", "com.example.echo");
6267
}
6368

6469
@Test
@@ -71,7 +76,7 @@ void listNamesRecursesOverCompositeRegistries() {
7176
reg1.counter("counter1").increment();
7277
reg2.counter("counter2").increment();
7378
MetricsEndpoint endpoint = new MetricsEndpoint(composite);
74-
assertThat(endpoint.listNames().getNames()).containsOnly("counter1", "counter2");
79+
assertThat(endpoint.listNames().getNames()).containsExactly("counter1", "counter2");
7580
}
7681

7782
@Test

0 commit comments

Comments
 (0)