Skip to content

Commit 49bab63

Browse files
committed
Update metrics endpoint to return list of distinct names
Closes gh-10336
1 parent 2ac6c10 commit 49bab63

File tree

3 files changed

+75
-2
lines changed

3 files changed

+75
-2
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public MetricsEndpoint(MeterRegistry registry) {
5353
@ReadOperation
5454
public Map<String, List<String>> listNames() {
5555
return Collections.singletonMap("names", this.registry.getMeters().stream()
56-
.map(this::getMeterIdName).collect(Collectors.toList()));
56+
.map(this::getMeterIdName).distinct().collect(Collectors.toList()));
5757
}
5858

5959
private String getMeterIdName(Meter meter) {
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright 2012-2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.actuate.metrics;
18+
19+
import java.util.Arrays;
20+
import java.util.List;
21+
import java.util.Map;
22+
23+
import io.micrometer.core.instrument.Meter;
24+
import io.micrometer.core.instrument.Meter.Id;
25+
import io.micrometer.core.instrument.MeterRegistry;
26+
import io.micrometer.core.instrument.simple.SimpleCounter;
27+
import org.junit.Test;
28+
29+
import static org.assertj.core.api.Assertions.assertThat;
30+
import static org.mockito.BDDMockito.given;
31+
import static org.mockito.Mockito.mock;
32+
33+
/**
34+
* Tests for {@link MetricsEndpoint}.
35+
*
36+
* @author Andy Wilkinson
37+
*/
38+
public class MetricsEndpointTests {
39+
40+
private final MeterRegistry registry = mock(MeterRegistry.class);
41+
42+
private final MetricsEndpoint endpoint = new MetricsEndpoint(this.registry);
43+
44+
@Test
45+
public void listNamesHandlesEmptyListOfMeters() {
46+
given(this.registry.getMeters()).willReturn(Arrays.asList());
47+
Map<String, List<String>> result = this.endpoint.listNames();
48+
assertThat(result).containsOnlyKeys("names");
49+
assertThat(result.get("names")).isEmpty();
50+
}
51+
52+
@Test
53+
public void listNamesProducesListOfUniqueMeterNames() {
54+
List<Meter> meters = Arrays.asList(createCounter("com.example.foo"),
55+
createCounter("com.example.bar"), createCounter("com.example.foo"));
56+
given(this.registry.getMeters()).willReturn(meters);
57+
Map<String, List<String>> result = this.endpoint.listNames();
58+
assertThat(result).containsOnlyKeys("names");
59+
assertThat(result.get("names")).containsOnlyOnce("com.example.foo",
60+
"com.example.bar");
61+
}
62+
63+
private Meter createCounter(String name) {
64+
return new SimpleCounter(createMeterId(name));
65+
}
66+
67+
private Id createMeterId(String name) {
68+
Id id = mock(Id.class);
69+
given(id.getName()).willReturn(name);
70+
return id;
71+
}
72+
73+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public void listNames() throws IOException {
5454
.uri("/application/metrics").exchange().expectStatus().isOk()
5555
.expectBody(String.class).returnResult().getResponseBody();
5656
Map<String, List<String>> names = this.mapper.readValue(responseBody, Map.class);
57-
assertThat(names.get("names")).contains("jvm.memory.used");
57+
assertThat(names.get("names")).containsOnlyOnce("jvm.memory.used");
5858
}
5959

6060
@Test

0 commit comments

Comments
 (0)