Browse Source

Update metrics endpoint to return list of distinct names

Closes gh-10336
pull/10351/head
Andy Wilkinson 8 years ago
parent
commit
49bab63ca6
  1. 2
      spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/MetricsEndpoint.java
  2. 73
      spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/MetricsEndpointTests.java
  3. 2
      spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/MetricsEndpointWebIntegrationTests.java

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

@ -53,7 +53,7 @@ public class MetricsEndpoint { @@ -53,7 +53,7 @@ public class MetricsEndpoint {
@ReadOperation
public Map<String, List<String>> listNames() {
return Collections.singletonMap("names", this.registry.getMeters().stream()
.map(this::getMeterIdName).collect(Collectors.toList()));
.map(this::getMeterIdName).distinct().collect(Collectors.toList()));
}
private String getMeterIdName(Meter meter) {

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

@ -0,0 +1,73 @@ @@ -0,0 +1,73 @@
/*
* Copyright 2012-2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.actuate.metrics;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import io.micrometer.core.instrument.Meter;
import io.micrometer.core.instrument.Meter.Id;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.simple.SimpleCounter;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
/**
* Tests for {@link MetricsEndpoint}.
*
* @author Andy Wilkinson
*/
public class MetricsEndpointTests {
private final MeterRegistry registry = mock(MeterRegistry.class);
private final MetricsEndpoint endpoint = new MetricsEndpoint(this.registry);
@Test
public void listNamesHandlesEmptyListOfMeters() {
given(this.registry.getMeters()).willReturn(Arrays.asList());
Map<String, List<String>> result = this.endpoint.listNames();
assertThat(result).containsOnlyKeys("names");
assertThat(result.get("names")).isEmpty();
}
@Test
public void listNamesProducesListOfUniqueMeterNames() {
List<Meter> meters = Arrays.asList(createCounter("com.example.foo"),
createCounter("com.example.bar"), createCounter("com.example.foo"));
given(this.registry.getMeters()).willReturn(meters);
Map<String, List<String>> result = this.endpoint.listNames();
assertThat(result).containsOnlyKeys("names");
assertThat(result.get("names")).containsOnlyOnce("com.example.foo",
"com.example.bar");
}
private Meter createCounter(String name) {
return new SimpleCounter(createMeterId(name));
}
private Id createMeterId(String name) {
Id id = mock(Id.class);
given(id.getName()).willReturn(name);
return id;
}
}

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

@ -54,7 +54,7 @@ public class MetricsEndpointWebIntegrationTests { @@ -54,7 +54,7 @@ public class MetricsEndpointWebIntegrationTests {
.uri("/application/metrics").exchange().expectStatus().isOk()
.expectBody(String.class).returnResult().getResponseBody();
Map<String, List<String>> names = this.mapper.readValue(responseBody, Map.class);
assertThat(names.get("names")).contains("jvm.memory.used");
assertThat(names.get("names")).containsOnlyOnce("jvm.memory.used");
}
@Test

Loading…
Cancel
Save