Skip to content

Commit 30ab4f9

Browse files
committed
Polish "Stop MetricsEndpoint from summing up same metrics"
See gh-14497
1 parent 950480d commit 30ab4f9

File tree

2 files changed

+24
-38
lines changed

2 files changed

+24
-38
lines changed

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

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.boot.actuate.metrics;
1818

19+
import java.util.ArrayList;
1920
import java.util.Collection;
2021
import java.util.Collections;
2122
import java.util.HashMap;
@@ -115,19 +116,17 @@ private Tag parseTag(String tag) {
115116
private Collection<Meter> findFirstMatchingMeters(MeterRegistry registry, String name,
116117
Iterable<Tag> tags) {
117118
if (registry instanceof CompositeMeterRegistry) {
118-
return ((CompositeMeterRegistry) registry).getRegistries().stream()
119-
.map((r) -> findFirstMatchingMeters(r, name, tags))
120-
.filter((match) -> !match.isEmpty()).findFirst()
121-
.orElse(Collections.emptyList());
122-
123-
}
124-
else {
125-
Collection<Meter> metersFound = registry.find(name).tags(tags).meters();
126-
if (!metersFound.isEmpty()) {
127-
return metersFound;
128-
}
119+
return findFirstMatchingMeters((CompositeMeterRegistry) registry, name, tags);
129120
}
130-
return Collections.emptyList();
121+
return registry.find(name).tags(tags).meters();
122+
}
123+
124+
private Collection<Meter> findFirstMatchingMeters(CompositeMeterRegistry composite,
125+
String name, Iterable<Tag> tags) {
126+
return composite.getRegistries().stream()
127+
.map((registry) -> findFirstMatchingMeters(registry, name, tags))
128+
.filter((matching) -> !matching.isEmpty()).findFirst()
129+
.orElse(Collections.emptyList());
131130
}
132131

133132
private Map<Statistic, Double> getSamples(Collection<Meter> meters) {

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

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -98,30 +98,21 @@ public void metricValuesAreTheSumOfAllTimeSeriesMatchingTags() {
9898
@Test
9999
public void findFirstMatchingMetersFromNestedRegistries() {
100100
CompositeMeterRegistry composite = new CompositeMeterRegistry();
101-
SimpleMeterRegistry reg1 = new SimpleMeterRegistry();
102-
CompositeMeterRegistry reg2 = new CompositeMeterRegistry();
103-
SimpleMeterRegistry reg3 = new SimpleMeterRegistry();
104-
105-
// 1st level nesting
106-
composite.add(reg1);
107-
108-
// 2st level nesting
109-
reg2.add(reg3);
110-
composite.add(reg2);
111-
112-
// 2nd level registry has metrics
113-
reg3.counter("cache", "result", "hit", "host", "1").increment(2);
114-
reg3.counter("cache", "result", "miss", "host", "1").increment(2);
115-
reg3.counter("cache", "result", "hit", "host", "2").increment(2);
116-
101+
SimpleMeterRegistry firstLevel0 = new SimpleMeterRegistry();
102+
CompositeMeterRegistry firstLevel1 = new CompositeMeterRegistry();
103+
SimpleMeterRegistry secondLevel = new SimpleMeterRegistry();
104+
composite.add(firstLevel0);
105+
composite.add(firstLevel1);
106+
firstLevel1.add(secondLevel);
107+
secondLevel.counter("cache", "result", "hit", "host", "1").increment(2);
108+
secondLevel.counter("cache", "result", "miss", "host", "1").increment(2);
109+
secondLevel.counter("cache", "result", "hit", "host", "2").increment(2);
117110
MetricsEndpoint endpoint = new MetricsEndpoint(composite);
118-
119111
MetricsEndpoint.MetricResponse response = endpoint.metric("cache",
120112
Collections.emptyList());
121113
assertThat(response.getName()).isEqualTo("cache");
122114
assertThat(availableTagKeys(response)).containsExactly("result", "host");
123115
assertThat(getCount(response)).hasValue(6.0);
124-
125116
response = endpoint.metric("cache", Collections.singletonList("result:hit"));
126117
assertThat(availableTagKeys(response)).containsExactly("host");
127118
assertThat(getCount(response)).hasValue(4.0);
@@ -130,15 +121,11 @@ public void findFirstMatchingMetersFromNestedRegistries() {
130121
@Test
131122
public void matchingMeterNotFoundInNestedRegistries() {
132123
CompositeMeterRegistry composite = new CompositeMeterRegistry();
133-
CompositeMeterRegistry reg2 = new CompositeMeterRegistry();
134-
SimpleMeterRegistry reg3 = new SimpleMeterRegistry();
135-
136-
// nested registries
137-
reg2.add(reg3);
138-
composite.add(reg2);
139-
124+
CompositeMeterRegistry firstLevel = new CompositeMeterRegistry();
125+
SimpleMeterRegistry secondLevel = new SimpleMeterRegistry();
126+
composite.add(firstLevel);
127+
firstLevel.add(secondLevel);
140128
MetricsEndpoint endpoint = new MetricsEndpoint(composite);
141-
142129
MetricsEndpoint.MetricResponse response = endpoint.metric("invalid.metric.name",
143130
Collections.emptyList());
144131
assertThat(response).isNull();

0 commit comments

Comments
 (0)