Skip to content

Commit 888acb9

Browse files
polarbear567snicoll
authored andcommitted
Add expiry and bufferLength configuration properties
See gh-27584
1 parent 042279e commit 888acb9

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.boot.actuate.autoconfigure.metrics;
1818

19+
import java.time.Duration;
1920
import java.util.LinkedHashMap;
2021
import java.util.Map;
2122

@@ -29,6 +30,7 @@
2930
* @author Jon Schneider
3031
* @author Alexander Abramov
3132
* @author Tadaya Tsuyukubo
33+
* @author Leo Li
3234
* @since 2.0.0
3335
*/
3436
@ConfigurationProperties("management.metrics")
@@ -296,6 +298,21 @@ public static class Distribution {
296298
*/
297299
private final Map<String, String> maximumExpectedValue = new LinkedHashMap<>();
298300

301+
/**
302+
* Specific statistic's expiry for meter IDs starting-with the specified name.
303+
* Values should be a Duration value, the key `all` can also be used to configure
304+
* all meters.
305+
*/
306+
private final Map<String, Duration> expiry = new LinkedHashMap<>();
307+
308+
/**
309+
* Specific statistic's bufferLength for meter IDs starting-with the specified
310+
* name. Samples are accumulated to statistics in ring buffers, and bufferLength
311+
* is the number to keep in the ring buffer, the key `all` can also be used to
312+
* configure all meters.
313+
*/
314+
private final Map<String, Integer> bufferLength = new LinkedHashMap<>();
315+
299316
public Map<String, Boolean> getPercentilesHistogram() {
300317
return this.percentilesHistogram;
301318
}
@@ -316,6 +333,14 @@ public Map<String, String> getMaximumExpectedValue() {
316333
return this.maximumExpectedValue;
317334
}
318335

336+
public Map<String, Duration> getExpiry() {
337+
return this.expiry;
338+
}
339+
340+
public Map<String, Integer> getBufferLength() {
341+
return this.bufferLength;
342+
}
343+
319344
}
320345

321346
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
* @author Stephane Nicoll
4343
* @author Artsiom Yudovin
4444
* @author Alexander Abramov
45+
* @author Leo Li
4546
* @since 2.0.0
4647
*/
4748
public class PropertiesMeterFilter implements MeterFilter {
@@ -83,6 +84,8 @@ public DistributionStatisticConfig configure(Meter.Id id, DistributionStatisticC
8384
return DistributionStatisticConfig.builder()
8485
.percentilesHistogram(lookupWithFallbackToAll(distribution.getPercentilesHistogram(), id, null))
8586
.percentiles(lookupWithFallbackToAll(distribution.getPercentiles(), id, null))
87+
.expiry(lookupWithFallbackToAll(distribution.getExpiry(), id, null))
88+
.bufferLength(lookupWithFallbackToAll(distribution.getBufferLength(), id, null))
8689
.serviceLevelObjectives(
8790
convertServiceLevelObjectives(id.getType(), lookup(distribution.getSlo(), id, null)))
8891
.minimumExpectedValue(

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

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
* @author Phillip Webb
4242
* @author Jon Schneider
4343
* @author Artsiom Yudovin
44+
* @author Leo Li
4445
*/
4546
class PropertiesMeterFilterTests {
4647

@@ -275,6 +276,71 @@ void configureWhenHasHigherMaximumExpectedValueAndLowerShouldSetMaximumExpectedV
275276
.getMaximumExpectedValueAsDouble()).isEqualTo(Duration.ofMillis(10000).toNanos());
276277
}
277278

279+
@Test
280+
void configureWhenHasExpiryShouldSetExpiryToValue() {
281+
PropertiesMeterFilter filter = new PropertiesMeterFilter(
282+
createProperties("distribution.expiry[spring.boot]=5ms"));
283+
assertThat(filter.configure(createMeterId("spring.boot"), DistributionStatisticConfig.DEFAULT).getExpiry())
284+
.isEqualTo(Duration.ofMillis(5));
285+
}
286+
287+
@Test
288+
void configureWhenHasHigherExpiryShouldSetExpiryToValue() {
289+
PropertiesMeterFilter filter = new PropertiesMeterFilter(createProperties("distribution.expiry.spring=5ms"));
290+
assertThat(filter.configure(createMeterId("spring.boot"), DistributionStatisticConfig.DEFAULT).getExpiry())
291+
.isEqualTo(Duration.ofMillis(5));
292+
}
293+
294+
@Test
295+
void configureWhenHasHigherExpiryAndLowerShouldSetExpiryToHigher() {
296+
PropertiesMeterFilter filter = new PropertiesMeterFilter(
297+
createProperties("distribution.expiry.spring=5ms", "distribution.expiry[spring.boot]=10ms"));
298+
assertThat(filter.configure(createMeterId("spring.boot"), DistributionStatisticConfig.DEFAULT).getExpiry())
299+
.isEqualTo(Duration.ofMillis(10));
300+
}
301+
302+
@Test
303+
void configureWhenAllExpirySetShouldSetExpiryToValue() {
304+
PropertiesMeterFilter filter = new PropertiesMeterFilter(createProperties("distribution.expiry.all=5ms"));
305+
assertThat(filter.configure(createMeterId("spring.boot"), DistributionStatisticConfig.DEFAULT).getExpiry())
306+
.isEqualTo(Duration.ofMillis(5));
307+
}
308+
309+
@Test
310+
void configureWhenHasBufferLengthShouldSetBufferLengthToValue() {
311+
PropertiesMeterFilter filter = new PropertiesMeterFilter(
312+
createProperties("distribution.bufferLength[spring.boot]=3"));
313+
assertThat(
314+
filter.configure(createMeterId("spring.boot"), DistributionStatisticConfig.DEFAULT).getBufferLength())
315+
.isEqualTo(3);
316+
}
317+
318+
@Test
319+
void configureWhenHasHigherBufferLengthShouldSetBufferLengthToValue() {
320+
PropertiesMeterFilter filter = new PropertiesMeterFilter(
321+
createProperties("distribution.bufferLength.spring=3"));
322+
assertThat(
323+
filter.configure(createMeterId("spring.boot"), DistributionStatisticConfig.DEFAULT).getBufferLength())
324+
.isEqualTo(3);
325+
}
326+
327+
@Test
328+
void configureWhenHasHigherBufferLengthAndLowerShouldSetBufferLengthToHigher() {
329+
PropertiesMeterFilter filter = new PropertiesMeterFilter(
330+
createProperties("distribution.bufferLength.spring=2", "distribution.bufferLength[spring.boot]=3"));
331+
assertThat(
332+
filter.configure(createMeterId("spring.boot"), DistributionStatisticConfig.DEFAULT).getBufferLength())
333+
.isEqualTo(3);
334+
}
335+
336+
@Test
337+
void configureWhenAllBufferLengthSetShouldSetBufferLengthToValue() {
338+
PropertiesMeterFilter filter = new PropertiesMeterFilter(createProperties("distribution.bufferLength.all=3"));
339+
assertThat(
340+
filter.configure(createMeterId("spring.boot"), DistributionStatisticConfig.DEFAULT).getBufferLength())
341+
.isEqualTo(3);
342+
}
343+
278344
private Id createMeterId(String name) {
279345
Meter.Type meterType = Type.TIMER;
280346
return createMeterId(name, meterType);

0 commit comments

Comments
 (0)