Merge branch '3.5.x' into 4.0.x

Closes gh-50077
This commit is contained in:
Andy Wilkinson
2026-04-15 14:43:43 +01:00
3 changed files with 61 additions and 5 deletions
@@ -23,6 +23,7 @@ import io.micrometer.core.instrument.Meter.Type;
import org.jspecify.annotations.Nullable;
import org.springframework.boot.convert.DurationStyle;
import org.springframework.util.StringUtils;
/**
* A meter value that is used when configuring micrometer. Can be a String representation
@@ -87,6 +88,9 @@ public final class MeterValue {
* @return a {@link MeterValue} instance
*/
public static MeterValue valueOf(String value) {
if (onlyDigits(value)) {
return new MeterValue(Double.parseDouble(value));
}
Duration duration = safeParseDuration(value);
if (duration != null) {
return new MeterValue(duration);
@@ -94,6 +98,18 @@ public final class MeterValue {
return new MeterValue(Double.parseDouble(value));
}
private static boolean onlyDigits(String value) {
if (!StringUtils.hasLength(value)) {
return false;
}
for (char c : value.toCharArray()) {
if (!Character.isDigit(c)) {
return false;
}
}
return true;
}
/**
* Return a new {@link MeterValue} instance for the given double value.
* @param value the source value
@@ -35,17 +35,29 @@ import static org.assertj.core.api.Assertions.assertThat;
class MeterValueTests {
@Test
void getValueForDistributionSummaryWhenFromNumberShouldReturnDoubleValue() {
void getValueForDistributionSummaryWhenFromDecimalNumberShouldReturnDoubleValue() {
MeterValue meterValue = MeterValue.valueOf(123.42);
assertThat(meterValue.getValue(Type.DISTRIBUTION_SUMMARY)).isEqualTo(123.42);
}
@Test
void getValueForDistributionSummaryWhenFromNumberStringShouldReturnDoubleValue() {
void getValueForDistributionSummaryWhenFromWholeNumberShouldReturnDoubleValue() {
MeterValue meterValue = MeterValue.valueOf(123);
assertThat(meterValue.getValue(Type.DISTRIBUTION_SUMMARY)).isEqualTo(123);
}
@Test
void getValueForDistributionSummaryWhenFromDecimalNumberStringShouldReturnDoubleValue() {
MeterValue meterValue = MeterValue.valueOf("123.42");
assertThat(meterValue.getValue(Type.DISTRIBUTION_SUMMARY)).isEqualTo(123.42);
}
@Test
void getValueForDistributionSummaryWhenFromWholeNumberStringShouldReturnDoubleValue() {
MeterValue meterValue = MeterValue.valueOf("123");
assertThat(meterValue.getValue(Type.DISTRIBUTION_SUMMARY)).isEqualTo(123);
}
@Test
void getValueForDistributionSummaryWhenFromDurationStringShouldReturnNull() {
MeterValue meterValue = MeterValue.valueOf("123ms");
@@ -206,13 +206,21 @@ class PropertiesMeterFilterTests {
}
@Test
void configureWhenHasSloShouldSetSloToValue() {
void configureWhenTimerMeterHasSloShouldSetSloToValue() {
PropertiesMeterFilter filter = new PropertiesMeterFilter(
createProperties("distribution.slo.spring.boot=1,2,3"));
assertThat(filter.configure(createMeterId("spring.boot"), DistributionStatisticConfig.DEFAULT)
.getServiceLevelObjectiveBoundaries()).containsExactly(1000000, 2000000, 3000000);
}
@Test
void configureWhenDistributionSummaryMeterHasSloShouldSetSloToValue() {
PropertiesMeterFilter filter = new PropertiesMeterFilter(createProperties("distribution.slo.example=1,2,3"));
assertThat(filter
.configure(createMeterId("example", Meter.Type.DISTRIBUTION_SUMMARY), DistributionStatisticConfig.DEFAULT)
.getServiceLevelObjectiveBoundaries()).containsExactly(1.0, 2.0, 3.0);
}
@Test
void configureWhenHasHigherSloShouldSetPercentilesToValue() {
PropertiesMeterFilter filter = new PropertiesMeterFilter(createProperties("distribution.slo.spring=1,2,3"));
@@ -229,13 +237,23 @@ class PropertiesMeterFilterTests {
}
@Test
void configureWhenHasMinimumExpectedValueShouldSetMinimumExpectedToValue() {
void configureWhenTimerMeterHasMinimumExpectedValueShouldSetMinimumExpectedToValue() {
PropertiesMeterFilter filter = new PropertiesMeterFilter(
createProperties("distribution.minimum-expected-value.spring.boot=10"));
assertThat(filter.configure(createMeterId("spring.boot"), DistributionStatisticConfig.DEFAULT)
.getMinimumExpectedValueAsDouble()).isEqualTo(Duration.ofMillis(10).toNanos());
}
@Test
void configureWhenDistributionSummaryMeterHasMinimumExpectedValueShouldSetMinimumExpectedToValue() {
PropertiesMeterFilter filter = new PropertiesMeterFilter(
createProperties("distribution.minimum-expected-value.spring.boot=10"));
assertThat(filter
.configure(createMeterId("spring.boot", Meter.Type.DISTRIBUTION_SUMMARY),
DistributionStatisticConfig.DEFAULT)
.getMinimumExpectedValueAsDouble()).isEqualTo(10);
}
@Test
void configureWhenHasHigherMinimumExpectedValueShouldSetMinimumExpectedValueToValue() {
PropertiesMeterFilter filter = new PropertiesMeterFilter(
@@ -253,13 +271,23 @@ class PropertiesMeterFilterTests {
}
@Test
void configureWhenHasMaximumExpectedValueShouldSetMaximumExpectedToValue() {
void configureWhenTimerMeterHasMaximumExpectedValueShouldSetMaximumExpectedToValue() {
PropertiesMeterFilter filter = new PropertiesMeterFilter(
createProperties("distribution.maximum-expected-value.spring.boot=5000"));
assertThat(filter.configure(createMeterId("spring.boot"), DistributionStatisticConfig.DEFAULT)
.getMaximumExpectedValueAsDouble()).isEqualTo(Duration.ofMillis(5000).toNanos());
}
@Test
void configureWhenDistributionSummaryMeterHasMaximumExpectedValueShouldSetMaximumExpectedToValue() {
PropertiesMeterFilter filter = new PropertiesMeterFilter(
createProperties("distribution.maximum-expected-value.spring.boot=10"));
assertThat(filter
.configure(createMeterId("spring.boot", Meter.Type.DISTRIBUTION_SUMMARY),
DistributionStatisticConfig.DEFAULT)
.getMaximumExpectedValueAsDouble()).isEqualTo(10);
}
@Test
void configureWhenHasHigherMaximumExpectedValueShouldSetMaximumExpectedValueToValue() {
PropertiesMeterFilter filter = new PropertiesMeterFilter(