Browse Source

Simplify PropertiesMeterFilter

lookupWithFallbackToAll is the only method that's called with a
non-null default value. Default value support can be removed from the
other lookup methods.

Closes gh-48986
pull/49013/head
Andy Wilkinson 4 days ago
parent
commit
fcc327c1dd
  1. 23
      spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/PropertiesMeterFilter.java

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

@ -20,7 +20,6 @@ import java.util.Arrays;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Objects; import java.util.Objects;
import java.util.function.Supplier;
import io.micrometer.core.instrument.Meter; import io.micrometer.core.instrument.Meter;
import io.micrometer.core.instrument.Meter.Id; import io.micrometer.core.instrument.Meter.Id;
@ -86,12 +85,9 @@ public class PropertiesMeterFilter implements MeterFilter {
return DistributionStatisticConfig.builder() return DistributionStatisticConfig.builder()
.percentilesHistogram(lookupWithFallbackToAll(distribution.getPercentilesHistogram(), id, null)) .percentilesHistogram(lookupWithFallbackToAll(distribution.getPercentilesHistogram(), id, null))
.percentiles(lookupWithFallbackToAll(distribution.getPercentiles(), id, null)) .percentiles(lookupWithFallbackToAll(distribution.getPercentiles(), id, null))
.serviceLevelObjectives( .serviceLevelObjectives(convertServiceLevelObjectives(id.getType(), lookup(distribution.getSlo(), id)))
convertServiceLevelObjectives(id.getType(), lookup(distribution.getSlo(), id, null))) .minimumExpectedValue(convertMeterValue(id.getType(), lookup(distribution.getMinimumExpectedValue(), id)))
.minimumExpectedValue( .maximumExpectedValue(convertMeterValue(id.getType(), lookup(distribution.getMaximumExpectedValue(), id)))
convertMeterValue(id.getType(), lookup(distribution.getMinimumExpectedValue(), id, null)))
.maximumExpectedValue(
convertMeterValue(id.getType(), lookup(distribution.getMaximumExpectedValue(), id, null)))
.expiry(lookupWithFallbackToAll(distribution.getExpiry(), id, null)) .expiry(lookupWithFallbackToAll(distribution.getExpiry(), id, null))
.bufferLength(lookupWithFallbackToAll(distribution.getBufferLength(), id, null)) .bufferLength(lookupWithFallbackToAll(distribution.getBufferLength(), id, null))
.build() .build()
@ -114,21 +110,22 @@ public class PropertiesMeterFilter implements MeterFilter {
return (value != null) ? MeterValue.valueOf(value).getValue(meterType) : null; return (value != null) ? MeterValue.valueOf(value).getValue(meterType) : null;
} }
private <T> T lookup(Map<String, T> values, Id id, T defaultValue) { private <T> T lookup(Map<String, T> values, Id id) {
if (values.isEmpty()) { if (values.isEmpty()) {
return defaultValue; return null;
} }
return doLookup(values, id, () -> defaultValue); return doLookup(values, id);
} }
private <T> T lookupWithFallbackToAll(Map<String, T> values, Id id, T defaultValue) { private <T> T lookupWithFallbackToAll(Map<String, T> values, Id id, T defaultValue) {
if (values.isEmpty()) { if (values.isEmpty()) {
return defaultValue; return defaultValue;
} }
return doLookup(values, id, () -> values.getOrDefault("all", defaultValue)); T result = doLookup(values, id);
return (result != null) ? result : values.getOrDefault("all", defaultValue);
} }
private <T> T doLookup(Map<String, T> values, Id id, Supplier<T> defaultValue) { private <T> T doLookup(Map<String, T> values, Id id) {
String name = id.getName(); String name = id.getName();
while (StringUtils.hasLength(name)) { while (StringUtils.hasLength(name)) {
T result = values.get(name); T result = values.get(name);
@ -139,7 +136,7 @@ public class PropertiesMeterFilter implements MeterFilter {
name = (lastDot != -1) ? name.substring(0, lastDot) : ""; name = (lastDot != -1) ? name.substring(0, lastDot) : "";
} }
return defaultValue.get(); return null;
} }
} }

Loading…
Cancel
Save