Skip to content

Commit 30201a4

Browse files
committed
Update SLA boundaries to use double
See micrometer-metrics/micrometer#1909 Closes gh-20837
1 parent 4813606 commit 30201a4

File tree

5 files changed

+77
-36
lines changed

5 files changed

+77
-36
lines changed

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

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,17 +25,18 @@
2525

2626
/**
2727
* A meter value that is used when configuring micrometer. Can be a String representation
28-
* of either a {@link Long} (applicable to timers and distribution summaries) or a
28+
* of either a {@link Double} (applicable to timers and distribution summaries) or a
2929
* {@link Duration} (applicable to only timers).
3030
*
3131
* @author Phillip Webb
32+
* @author Stephane Nicoll
3233
* @since 2.2.0
3334
*/
3435
public final class MeterValue {
3536

3637
private final Object value;
3738

38-
MeterValue(long value) {
39+
MeterValue(double value) {
3940
this.value = value;
4041
}
4142

@@ -48,26 +49,29 @@ public final class MeterValue {
4849
* @param meterType the meter type
4950
* @return the value or {@code null} if the value cannot be applied
5051
*/
51-
public Long getValue(Type meterType) {
52+
public Double getValue(Type meterType) {
5253
if (meterType == Type.DISTRIBUTION_SUMMARY) {
5354
return getDistributionSummaryValue();
5455
}
5556
if (meterType == Type.TIMER) {
56-
return getTimerValue();
57+
Long timerValue = getTimerValue();
58+
if (timerValue != null) {
59+
return timerValue.doubleValue();
60+
}
5761
}
5862
return null;
5963
}
6064

61-
private Long getDistributionSummaryValue() {
62-
if (this.value instanceof Long) {
63-
return (Long) this.value;
65+
private Double getDistributionSummaryValue() {
66+
if (this.value instanceof Double) {
67+
return (Double) this.value;
6468
}
6569
return null;
6670
}
6771

6872
private Long getTimerValue() {
69-
if (this.value instanceof Long) {
70-
return TimeUnit.MILLISECONDS.toNanos((long) this.value);
73+
if (this.value instanceof Double) {
74+
return TimeUnit.MILLISECONDS.toNanos(((Double) this.value).longValue());
7175
}
7276
if (this.value instanceof Duration) {
7377
return ((Duration) this.value).toNanos();
@@ -82,8 +86,9 @@ private Long getTimerValue() {
8286
* @return a {@link MeterValue} instance
8387
*/
8488
public static MeterValue valueOf(String value) {
85-
if (isNumber(value)) {
86-
return new MeterValue(Long.parseLong(value));
89+
Double number = safeParseDouble(value);
90+
if (number != null) {
91+
return new MeterValue(number);
8792
}
8893
return new MeterValue(DurationStyle.detectAndParse(value));
8994
}
@@ -92,13 +97,29 @@ public static MeterValue valueOf(String value) {
9297
* Return a new {@link MeterValue} instance for the given long value.
9398
* @param value the source value
9499
* @return a {@link MeterValue} instance
100+
* @deprecated as of 2.3.0 in favor of {@link #valueOf(double)}
95101
*/
102+
@Deprecated
96103
public static MeterValue valueOf(long value) {
97104
return new MeterValue(value);
98105
}
99106

100-
private static boolean isNumber(String value) {
101-
return value.chars().allMatch(Character::isDigit);
107+
/**
108+
* Return a new {@link MeterValue} instance for the given double value.
109+
* @param value the source value
110+
* @return a {@link MeterValue} instance
111+
*/
112+
public static MeterValue valueOf(double value) {
113+
return new MeterValue(value);
114+
}
115+
116+
private static Double safeParseDouble(String value) {
117+
try {
118+
return Double.parseDouble(value);
119+
}
120+
catch (NumberFormatException nfe) {
121+
return null;
122+
}
102123
}
103124

104125
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -91,16 +91,16 @@ public DistributionStatisticConfig configure(Meter.Id id, DistributionStatisticC
9191
.build().merge(config);
9292
}
9393

94-
private long[] convertSla(Meter.Type meterType, ServiceLevelAgreementBoundary[] sla) {
94+
private double[] convertSla(Meter.Type meterType, ServiceLevelAgreementBoundary[] sla) {
9595
if (sla == null) {
9696
return null;
9797
}
98-
long[] converted = Arrays.stream(sla).map((candidate) -> candidate.getValue(meterType)).filter(Objects::nonNull)
99-
.mapToLong(Long::longValue).toArray();
98+
double[] converted = Arrays.stream(sla).map((candidate) -> candidate.getValue(meterType))
99+
.filter(Objects::nonNull).mapToDouble(Double::doubleValue).toArray();
100100
return (converted.length != 0) ? converted : null;
101101
}
102102

103-
private Long convertMeterValue(Meter.Type meterType, String value) {
103+
private Double convertMeterValue(Meter.Type meterType, String value) {
104104
return (value != null) ? MeterValue.valueOf(value).getValue(meterType) : null;
105105
}
106106

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,8 +22,8 @@
2222

2323
/**
2424
* A service level agreement boundary for use when configuring Micrometer. Can be
25-
* specified as either a {@link Long} (applicable to timers and distribution summaries) or
26-
* a {@link Duration} (applicable to only timers).
25+
* specified as either a {@link Double} (applicable to timers and distribution summaries)
26+
* or a {@link Duration} (applicable to only timers).
2727
*
2828
* @author Phillip Webb
2929
* @since 2.0.0
@@ -42,17 +42,17 @@ public final class ServiceLevelAgreementBoundary {
4242
* @param meterType the meter type
4343
* @return the value or {@code null} if the value cannot be applied
4444
*/
45-
public Long getValue(Meter.Type meterType) {
45+
public Double getValue(Meter.Type meterType) {
4646
return this.value.getValue(meterType);
4747
}
4848

4949
/**
50-
* Return a new {@link ServiceLevelAgreementBoundary} instance for the given long
50+
* Return a new {@link ServiceLevelAgreementBoundary} instance for the given double
5151
* value.
5252
* @param value the source value
5353
* @return a {@link ServiceLevelAgreementBoundary} instance
5454
*/
55-
public static ServiceLevelAgreementBoundary valueOf(long value) {
55+
public static ServiceLevelAgreementBoundary valueOf(double value) {
5656
return new ServiceLevelAgreementBoundary(MeterValue.valueOf(value));
5757
}
5858

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -30,17 +30,18 @@
3030
* Tests for {@link MeterValue}.
3131
*
3232
* @author Phillip Webb
33+
* @author Stephane Nicoll
3334
*/
3435
class MeterValueTests {
3536

3637
@Test
37-
void getValueForDistributionSummaryWhenFromLongShouldReturnLongValue() {
38-
MeterValue meterValue = MeterValue.valueOf(123L);
39-
assertThat(meterValue.getValue(Type.DISTRIBUTION_SUMMARY)).isEqualTo(123);
38+
void getValueForDistributionSummaryWhenFromNumberShouldReturnDoubleValue() {
39+
MeterValue meterValue = MeterValue.valueOf(123.42);
40+
assertThat(meterValue.getValue(Type.DISTRIBUTION_SUMMARY)).isEqualTo(123.42);
4041
}
4142

4243
@Test
43-
void getValueForDistributionSummaryWhenFromNumberStringShouldReturnLongValue() {
44+
void getValueForDistributionSummaryWhenFromNumberStringShouldReturnDoubleValue() {
4445
MeterValue meterValue = MeterValue.valueOf("123");
4546
assertThat(meterValue.getValue(Type.DISTRIBUTION_SUMMARY)).isEqualTo(123);
4647
}
@@ -52,8 +53,8 @@ void getValueForDistributionSummaryWhenFromDurationStringShouldReturnNull() {
5253
}
5354

5455
@Test
55-
void getValueForTimerWhenFromLongShouldReturnMsToNanosValue() {
56-
MeterValue meterValue = MeterValue.valueOf(123L);
56+
void getValueForTimerWhenFromNumberShouldReturnMsToNanosValue() {
57+
MeterValue meterValue = MeterValue.valueOf(123d);
5758
assertThat(meterValue.getValue(Type.TIMER)).isEqualTo(123000000);
5859
}
5960

@@ -81,11 +82,11 @@ void getValueForOthersShouldReturnNull() {
8182
@Test
8283
void valueOfShouldWorkInBinder() {
8384
MockEnvironment environment = new MockEnvironment();
84-
TestPropertyValues.of("duration=10ms", "long=20").applyTo(environment);
85+
TestPropertyValues.of("duration=10ms", "number=20.42").applyTo(environment);
8586
assertThat(Binder.get(environment).bind("duration", Bindable.of(MeterValue.class)).get().getValue(Type.TIMER))
8687
.isEqualTo(10000000);
87-
assertThat(Binder.get(environment).bind("long", Bindable.of(MeterValue.class)).get().getValue(Type.TIMER))
88-
.isEqualTo(20000000);
88+
assertThat(Binder.get(environment).bind("number", Bindable.of(MeterValue.class)).get()
89+
.getValue(Type.DISTRIBUTION_SUMMARY)).isEqualTo(20.42);
8990
}
9091

9192
}

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,6 +25,7 @@
2525
* Tests for {@link ServiceLevelAgreementBoundary}.
2626
*
2727
* @author Phillip Webb
28+
* @author Stephane Nicoll
2829
*/
2930
class ServiceLevelAgreementBoundaryTests {
3031

@@ -46,4 +47,22 @@ void getValueForTimerWhenFromDurationStringShouldReturnDurationNanos() {
4647
assertThat(sla.getValue(Type.TIMER)).isEqualTo(123000000);
4748
}
4849

50+
@Test
51+
void getValueForDistributionSummaryWhenFromDoubleShouldReturnDoubleValue() {
52+
ServiceLevelAgreementBoundary sla = ServiceLevelAgreementBoundary.valueOf(123.42);
53+
assertThat(sla.getValue(Type.DISTRIBUTION_SUMMARY)).isEqualTo(123.42);
54+
}
55+
56+
@Test
57+
void getValueForDistributionSummaryWhenFromStringShouldReturnDoubleValue() {
58+
ServiceLevelAgreementBoundary sla = ServiceLevelAgreementBoundary.valueOf("123.42");
59+
assertThat(sla.getValue(Type.DISTRIBUTION_SUMMARY)).isEqualTo(123.42);
60+
}
61+
62+
@Test
63+
void getValueForDistributionSummaryWhenFromDurationShouldReturnNull() {
64+
ServiceLevelAgreementBoundary sla = ServiceLevelAgreementBoundary.valueOf("123ms");
65+
assertThat(sla.getValue(Type.DISTRIBUTION_SUMMARY)).isNull();
66+
}
67+
4968
}

0 commit comments

Comments
 (0)