Skip to content

Commit 0d67a1a

Browse files
committed
Merge pull request #27584 from polarbear567
* pr/27584: Polish "Add expiry and bufferLength configuration properties" Add expiry and bufferLength configuration properties Closes gh-27584
2 parents 042279e + 1475309 commit 0d67a1a

File tree

4 files changed

+97
-2
lines changed

4 files changed

+97
-2
lines changed

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

Lines changed: 24 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

@@ -296,6 +297,21 @@ public static class Distribution {
296297
*/
297298
private final Map<String, String> maximumExpectedValue = new LinkedHashMap<>();
298299

300+
/**
301+
* Maximum amount of time that samples for meter IDs starting with the specified
302+
* name are accumulated to decaying distribution statistics before they are reset
303+
* and rotated. The longest match wins, the key `all` can also be used to
304+
* configure all meters.
305+
*/
306+
private final Map<String, Duration> expiry = new LinkedHashMap<>();
307+
308+
/**
309+
* Number of histograms for meter IDs starting with the specified name to keep in
310+
* the ring buffer. The longest match wins, the key `all` can also be used to
311+
* configure all meters.
312+
*/
313+
private final Map<String, Integer> bufferLength = new LinkedHashMap<>();
314+
299315
public Map<String, Boolean> getPercentilesHistogram() {
300316
return this.percentilesHistogram;
301317
}
@@ -316,6 +332,14 @@ public Map<String, String> getMaximumExpectedValue() {
316332
return this.maximumExpectedValue;
317333
}
318334

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

321345
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2021 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.
@@ -89,7 +89,8 @@ public DistributionStatisticConfig configure(Meter.Id id, DistributionStatisticC
8989
convertMeterValue(id.getType(), lookup(distribution.getMinimumExpectedValue(), id, null)))
9090
.maximumExpectedValue(
9191
convertMeterValue(id.getType(), lookup(distribution.getMaximumExpectedValue(), id, null)))
92-
.build().merge(config);
92+
.expiry(lookupWithFallbackToAll(distribution.getExpiry(), id, null))
93+
.bufferLength(lookupWithFallbackToAll(distribution.getBufferLength(), id, null)).build().merge(config);
9394
}
9495

9596
private double[] convertServiceLevelObjectives(Meter.Type meterType, ServiceLevelObjectiveBoundary[] slo) {

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.buffer-length.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.buffer-length.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.buffer-length.spring=2", "distribution.buffer-length.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.buffer-length.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);

spring-boot-project/spring-boot-docs/src/docs/asciidoc/actuator/metrics.adoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,6 +1153,10 @@ The following properties allow per-meter customization:
11531153
| configprop:management.metrics.distribution.percentiles[]
11541154
| Publish percentile values computed in your application
11551155

1156+
| configprop:management.metrics.distribution.expiry[], configprop:management.metrics.distribution.buffer-length[]
1157+
| Give greater weight to recent samples by accumulating them in ring buffers which rotate after a configurable expiry, with a
1158+
configurable buffer length.
1159+
11561160
| configprop:management.metrics.distribution.slo[]
11571161
| Publish a cumulative histogram with buckets defined by your service-level objectives.
11581162
|===

0 commit comments

Comments
 (0)