Skip to content

Commit b0e1dc2

Browse files
committed
Merge pull request #14684 from Artsiom Yudovin
* gh-14684: Polish "Drop support for "all" from management.metrics.distribution.sla" Drop support for "all" from management.metrics.distribution.sla
2 parents ef7c2bc + c9da881 commit b0e1dc2

File tree

4 files changed

+25
-49
lines changed

4 files changed

+25
-49
lines changed

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -192,10 +192,9 @@ public static class Distribution {
192192

193193
/**
194194
* Specific SLA boundaries for meter IDs starting-with the specified name. The
195-
* longest match wins, the key `all` can also be used to configure all meters.
196-
* Counters will be published for each specified boundary. Values can be specified
197-
* as a long or as a Duration value (for timer meters, defaulting to ms if no unit
198-
* specified).
195+
* longest match wins. Counters will be published for each specified boundary.
196+
* Values can be specified as a long or as a Duration value (for timer meters,
197+
* defaulting to ms if no unit specified).
199198
*/
200199
private final Map<String, ServiceLevelAgreementBoundary[]> sla = new LinkedHashMap<>();
201200

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

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.Arrays;
2020
import java.util.Map;
2121
import java.util.Objects;
22+
import java.util.function.Supplier;
2223
import java.util.stream.Collectors;
2324

2425
import io.micrometer.core.instrument.Meter;
@@ -39,6 +40,7 @@
3940
* @author Jon Schneider
4041
* @author Phillip Webb
4142
* @author Stephane Nicoll
43+
* @author Artsiom Yudovin
4244
* @since 2.0.0
4345
*/
4446
public class PropertiesMeterFilter implements MeterFilter {
@@ -66,7 +68,7 @@ private static MeterFilter createMapFilter(Map<String, String> tags) {
6668

6769
@Override
6870
public MeterFilterReply accept(Meter.Id id) {
69-
boolean enabled = lookup(this.properties.getEnable(), id, true);
71+
boolean enabled = lookupWithFallbackToAll(this.properties.getEnable(), id, true);
7072
return enabled ? MeterFilterReply.NEUTRAL : MeterFilterReply.DENY;
7173
}
7274

@@ -80,9 +82,10 @@ public DistributionStatisticConfig configure(Meter.Id id,
8082
DistributionStatisticConfig config) {
8183
Distribution distribution = this.properties.getDistribution();
8284
return DistributionStatisticConfig.builder()
83-
.percentilesHistogram(
84-
lookup(distribution.getPercentilesHistogram(), id, null))
85-
.percentiles(lookup(distribution.getPercentiles(), id, null))
85+
.percentilesHistogram(lookupWithFallbackToAll(
86+
distribution.getPercentilesHistogram(), id, null))
87+
.percentiles(
88+
lookupWithFallbackToAll(distribution.getPercentiles(), id, null))
8689
.sla(convertSla(id.getType(), lookup(distribution.getSla(), id, null)))
8790
.build().merge(config);
8891
}
@@ -101,6 +104,17 @@ private <T> T lookup(Map<String, T> values, Id id, T defaultValue) {
101104
if (values.isEmpty()) {
102105
return defaultValue;
103106
}
107+
return doLookup(values, id, () -> defaultValue);
108+
}
109+
110+
private <T> T lookupWithFallbackToAll(Map<String, T> values, Id id, T defaultValue) {
111+
if (values.isEmpty()) {
112+
return defaultValue;
113+
}
114+
return doLookup(values, id, () -> values.getOrDefault("all", defaultValue));
115+
}
116+
117+
private <T> T doLookup(Map<String, T> values, Id id, Supplier<T> defaultValue) {
104118
String name = id.getName();
105119
while (StringUtils.hasLength(name)) {
106120
T result = values.get(name);
@@ -110,7 +124,8 @@ private <T> T lookup(Map<String, T> values, Id id, T defaultValue) {
110124
int lastDot = name.lastIndexOf('.');
111125
name = (lastDot != -1) ? name.substring(0, lastDot) : "";
112126
}
113-
return values.getOrDefault("all", defaultValue);
127+
128+
return defaultValue.get();
114129
}
115130

116131
}

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

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
*
4343
* @author Phillip Webb
4444
* @author Jon Schneider
45+
* @author Artsiom Yudovin
4546
*/
4647
public class PropertiesMeterFilterTests {
4748

@@ -261,45 +262,6 @@ public void configureWhenHasHigherSlaAndLowerShouldSetSlaToHigher() {
261262
.containsExactly(4000000, 5000000, 6000000);
262263
}
263264

264-
@Test
265-
public void configureWhenAllSlaSetShouldSetSlaToValue() {
266-
PropertiesMeterFilter filter = new PropertiesMeterFilter(
267-
createProperties("distribution.sla.all=1,2,3"));
268-
assertThat(filter.configure(createMeterId("spring.boot"),
269-
DistributionStatisticConfig.DEFAULT).getSlaBoundaries())
270-
.containsExactly(1000000, 2000000, 3000000);
271-
}
272-
273-
@Test
274-
public void configureWhenSlaDurationShouldOnlyApplyToTimer() {
275-
PropertiesMeterFilter filter = new PropertiesMeterFilter(
276-
createProperties("distribution.sla.all=1ms,2ms,3ms"));
277-
Meter.Id timer = createMeterId("spring.boot", Meter.Type.TIMER);
278-
Meter.Id summary = createMeterId("spring.boot", Meter.Type.DISTRIBUTION_SUMMARY);
279-
Meter.Id counter = createMeterId("spring.boot", Meter.Type.COUNTER);
280-
assertThat(filter.configure(timer, DistributionStatisticConfig.DEFAULT)
281-
.getSlaBoundaries()).containsExactly(1000000, 2000000, 3000000);
282-
assertThat(filter.configure(summary, DistributionStatisticConfig.DEFAULT)
283-
.getSlaBoundaries()).isNullOrEmpty();
284-
assertThat(filter.configure(counter, DistributionStatisticConfig.DEFAULT)
285-
.getSlaBoundaries()).isNullOrEmpty();
286-
}
287-
288-
@Test
289-
public void configureWhenSlaLongShouldOnlyApplyToTimerAndDistributionSummary() {
290-
PropertiesMeterFilter filter = new PropertiesMeterFilter(
291-
createProperties("distribution.sla.all=1,2,3"));
292-
Meter.Id timer = createMeterId("spring.boot", Meter.Type.TIMER);
293-
Meter.Id summary = createMeterId("spring.boot", Meter.Type.DISTRIBUTION_SUMMARY);
294-
Meter.Id counter = createMeterId("spring.boot", Meter.Type.COUNTER);
295-
assertThat(filter.configure(timer, DistributionStatisticConfig.DEFAULT)
296-
.getSlaBoundaries()).containsExactly(1000000, 2000000, 3000000);
297-
assertThat(filter.configure(summary, DistributionStatisticConfig.DEFAULT)
298-
.getSlaBoundaries()).containsExactly(1, 2, 3);
299-
assertThat(filter.configure(counter, DistributionStatisticConfig.DEFAULT)
300-
.getSlaBoundaries()).isNullOrEmpty();
301-
}
302-
303265
private Id createMeterId(String name) {
304266
Meter.Type meterType = Type.TIMER;
305267
return createMeterId(name, meterType);

spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1382,7 +1382,7 @@ content into your application. Rather, pick only the properties that you need.
13821382
# METRICS
13831383
management.metrics.distribution.percentiles-histogram.*= # Whether meter IDs starting with the specified name should publish percentile histograms.
13841384
management.metrics.distribution.percentiles.*= # Specific computed non-aggregable percentiles to ship to the backend for meter IDs starting-with the specified name.
1385-
management.metrics.distribution.sla.*= # Specific SLA boundaries for meter IDs starting-with the specified name. The longest match wins, the key `all` can also be used to configure all meters.
1385+
management.metrics.distribution.sla.*= # Specific SLA boundaries for meter IDs starting-with the specified name. The longest match wins.
13861386
management.metrics.enable.*= # Whether meter IDs starting-with the specified name should be enabled. The longest match wins, the key `all` can also be used to configure all meters.
13871387
management.metrics.export.atlas.batch-size=10000 # Number of measurements per request to use for this backend. If more measurements are found, then multiple requests will be made.
13881388
management.metrics.export.atlas.config-refresh-frequency=10s # Frequency for refreshing config settings from the LWC service.

0 commit comments

Comments
 (0)