Skip to content

Commit fc426a7

Browse files
committed
Merge branch '3.5.x' into 4.0.x
Closes gh-50077
2 parents 9ca5caa + 1463686 commit fc426a7

3 files changed

Lines changed: 61 additions & 5 deletions

File tree

module/spring-boot-micrometer-metrics/src/main/java/org/springframework/boot/micrometer/metrics/autoconfigure/MeterValue.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.jspecify.annotations.Nullable;
2424

2525
import org.springframework.boot.convert.DurationStyle;
26+
import org.springframework.util.StringUtils;
2627

2728
/**
2829
* A meter value that is used when configuring micrometer. Can be a String representation
@@ -87,13 +88,28 @@ public final class MeterValue {
8788
* @return a {@link MeterValue} instance
8889
*/
8990
public static MeterValue valueOf(String value) {
91+
if (onlyDigits(value)) {
92+
return new MeterValue(Double.parseDouble(value));
93+
}
9094
Duration duration = safeParseDuration(value);
9195
if (duration != null) {
9296
return new MeterValue(duration);
9397
}
9498
return new MeterValue(Double.parseDouble(value));
9599
}
96100

101+
private static boolean onlyDigits(String value) {
102+
if (!StringUtils.hasLength(value)) {
103+
return false;
104+
}
105+
for (char c : value.toCharArray()) {
106+
if (!Character.isDigit(c)) {
107+
return false;
108+
}
109+
}
110+
return true;
111+
}
112+
97113
/**
98114
* Return a new {@link MeterValue} instance for the given double value.
99115
* @param value the source value

module/spring-boot-micrometer-metrics/src/test/java/org/springframework/boot/micrometer/metrics/autoconfigure/MeterValueTests.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,29 @@
3535
class MeterValueTests {
3636

3737
@Test
38-
void getValueForDistributionSummaryWhenFromNumberShouldReturnDoubleValue() {
38+
void getValueForDistributionSummaryWhenFromDecimalNumberShouldReturnDoubleValue() {
3939
MeterValue meterValue = MeterValue.valueOf(123.42);
4040
assertThat(meterValue.getValue(Type.DISTRIBUTION_SUMMARY)).isEqualTo(123.42);
4141
}
4242

4343
@Test
44-
void getValueForDistributionSummaryWhenFromNumberStringShouldReturnDoubleValue() {
44+
void getValueForDistributionSummaryWhenFromWholeNumberShouldReturnDoubleValue() {
45+
MeterValue meterValue = MeterValue.valueOf(123);
46+
assertThat(meterValue.getValue(Type.DISTRIBUTION_SUMMARY)).isEqualTo(123);
47+
}
48+
49+
@Test
50+
void getValueForDistributionSummaryWhenFromDecimalNumberStringShouldReturnDoubleValue() {
4551
MeterValue meterValue = MeterValue.valueOf("123.42");
4652
assertThat(meterValue.getValue(Type.DISTRIBUTION_SUMMARY)).isEqualTo(123.42);
4753
}
4854

55+
@Test
56+
void getValueForDistributionSummaryWhenFromWholeNumberStringShouldReturnDoubleValue() {
57+
MeterValue meterValue = MeterValue.valueOf("123");
58+
assertThat(meterValue.getValue(Type.DISTRIBUTION_SUMMARY)).isEqualTo(123);
59+
}
60+
4961
@Test
5062
void getValueForDistributionSummaryWhenFromDurationStringShouldReturnNull() {
5163
MeterValue meterValue = MeterValue.valueOf("123ms");

module/spring-boot-micrometer-metrics/src/test/java/org/springframework/boot/micrometer/metrics/autoconfigure/PropertiesMeterFilterTests.java

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,13 +206,21 @@ void configureWhenAllPercentilesSetShouldSetPercentilesToValue() {
206206
}
207207

208208
@Test
209-
void configureWhenHasSloShouldSetSloToValue() {
209+
void configureWhenTimerMeterHasSloShouldSetSloToValue() {
210210
PropertiesMeterFilter filter = new PropertiesMeterFilter(
211211
createProperties("distribution.slo.spring.boot=1,2,3"));
212212
assertThat(filter.configure(createMeterId("spring.boot"), DistributionStatisticConfig.DEFAULT)
213213
.getServiceLevelObjectiveBoundaries()).containsExactly(1000000, 2000000, 3000000);
214214
}
215215

216+
@Test
217+
void configureWhenDistributionSummaryMeterHasSloShouldSetSloToValue() {
218+
PropertiesMeterFilter filter = new PropertiesMeterFilter(createProperties("distribution.slo.example=1,2,3"));
219+
assertThat(filter
220+
.configure(createMeterId("example", Meter.Type.DISTRIBUTION_SUMMARY), DistributionStatisticConfig.DEFAULT)
221+
.getServiceLevelObjectiveBoundaries()).containsExactly(1.0, 2.0, 3.0);
222+
}
223+
216224
@Test
217225
void configureWhenHasHigherSloShouldSetPercentilesToValue() {
218226
PropertiesMeterFilter filter = new PropertiesMeterFilter(createProperties("distribution.slo.spring=1,2,3"));
@@ -229,13 +237,23 @@ void configureWhenHasHigherSloAndLowerShouldSetSloToLower() {
229237
}
230238

231239
@Test
232-
void configureWhenHasMinimumExpectedValueShouldSetMinimumExpectedToValue() {
240+
void configureWhenTimerMeterHasMinimumExpectedValueShouldSetMinimumExpectedToValue() {
233241
PropertiesMeterFilter filter = new PropertiesMeterFilter(
234242
createProperties("distribution.minimum-expected-value.spring.boot=10"));
235243
assertThat(filter.configure(createMeterId("spring.boot"), DistributionStatisticConfig.DEFAULT)
236244
.getMinimumExpectedValueAsDouble()).isEqualTo(Duration.ofMillis(10).toNanos());
237245
}
238246

247+
@Test
248+
void configureWhenDistributionSummaryMeterHasMinimumExpectedValueShouldSetMinimumExpectedToValue() {
249+
PropertiesMeterFilter filter = new PropertiesMeterFilter(
250+
createProperties("distribution.minimum-expected-value.spring.boot=10"));
251+
assertThat(filter
252+
.configure(createMeterId("spring.boot", Meter.Type.DISTRIBUTION_SUMMARY),
253+
DistributionStatisticConfig.DEFAULT)
254+
.getMinimumExpectedValueAsDouble()).isEqualTo(10);
255+
}
256+
239257
@Test
240258
void configureWhenHasHigherMinimumExpectedValueShouldSetMinimumExpectedValueToValue() {
241259
PropertiesMeterFilter filter = new PropertiesMeterFilter(
@@ -253,13 +271,23 @@ void configureWhenHasHigherMinimumExpectedValueAndLowerShouldSetMinimumExpectedV
253271
}
254272

255273
@Test
256-
void configureWhenHasMaximumExpectedValueShouldSetMaximumExpectedToValue() {
274+
void configureWhenTimerMeterHasMaximumExpectedValueShouldSetMaximumExpectedToValue() {
257275
PropertiesMeterFilter filter = new PropertiesMeterFilter(
258276
createProperties("distribution.maximum-expected-value.spring.boot=5000"));
259277
assertThat(filter.configure(createMeterId("spring.boot"), DistributionStatisticConfig.DEFAULT)
260278
.getMaximumExpectedValueAsDouble()).isEqualTo(Duration.ofMillis(5000).toNanos());
261279
}
262280

281+
@Test
282+
void configureWhenDistributionSummaryMeterHasMaximumExpectedValueShouldSetMaximumExpectedToValue() {
283+
PropertiesMeterFilter filter = new PropertiesMeterFilter(
284+
createProperties("distribution.maximum-expected-value.spring.boot=10"));
285+
assertThat(filter
286+
.configure(createMeterId("spring.boot", Meter.Type.DISTRIBUTION_SUMMARY),
287+
DistributionStatisticConfig.DEFAULT)
288+
.getMaximumExpectedValueAsDouble()).isEqualTo(10);
289+
}
290+
263291
@Test
264292
void configureWhenHasHigherMaximumExpectedValueShouldSetMaximumExpectedValueToValue() {
265293
PropertiesMeterFilter filter = new PropertiesMeterFilter(

0 commit comments

Comments
 (0)