From 4cc1b5e3b4bc1a071302c51c5949960b7d383381 Mon Sep 17 00:00:00 2001 From: izeye Date: Sun, 8 May 2022 23:52:36 +0900 Subject: [PATCH 1/2] Expose buffered property for StatsdConfig See https://github.com/micrometer-metrics/micrometer/issues/1375 --- .../export/statsd/StatsdProperties.java | 13 +++ .../statsd/StatsdPropertiesConfigAdapter.java | 5 + .../StatsdPropertiesConfigAdapterTests.java | 100 ++++++++++++++++++ .../export/statsd/StatsdPropertiesTests.java | 3 +- 4 files changed, 120 insertions(+), 1 deletion(-) create mode 100644 spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/statsd/StatsdPropertiesConfigAdapterTests.java diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/statsd/StatsdProperties.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/statsd/StatsdProperties.java index a3ba9f0b1806..b158ae150080 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/statsd/StatsdProperties.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/statsd/StatsdProperties.java @@ -76,6 +76,11 @@ public class StatsdProperties { */ private boolean publishUnchangedMeters = true; + /** + * Whether measurements should be buffered before sending to the StatsD server. + */ + private boolean buffered = true; + public boolean isEnabled() { return this.enabled; } @@ -140,4 +145,12 @@ public void setPublishUnchangedMeters(boolean publishUnchangedMeters) { this.publishUnchangedMeters = publishUnchangedMeters; } + public boolean isBuffered() { + return this.buffered; + } + + public void setBuffered(boolean buffered) { + this.buffered = buffered; + } + } diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/statsd/StatsdPropertiesConfigAdapter.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/statsd/StatsdPropertiesConfigAdapter.java index 6cdbb66094d4..e048aa6aafb5 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/statsd/StatsdPropertiesConfigAdapter.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/statsd/StatsdPropertiesConfigAdapter.java @@ -86,4 +86,9 @@ public boolean publishUnchangedMeters() { return get(StatsdProperties::isPublishUnchangedMeters, StatsdConfig.super::publishUnchangedMeters); } + @Override + public boolean buffered() { + return get(StatsdProperties::isBuffered, StatsdConfig.super::buffered); + } + } diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/statsd/StatsdPropertiesConfigAdapterTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/statsd/StatsdPropertiesConfigAdapterTests.java new file mode 100644 index 000000000000..ac8150909c21 --- /dev/null +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/statsd/StatsdPropertiesConfigAdapterTests.java @@ -0,0 +1,100 @@ +/* + * Copyright 2012-2022 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.actuate.autoconfigure.metrics.export.statsd; + +import java.time.Duration; + +import io.micrometer.statsd.StatsdFlavor; +import io.micrometer.statsd.StatsdProtocol; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests for {@link StatsdPropertiesConfigAdapter}. + * + * @author Johnny Lim + */ +class StatsdPropertiesConfigAdapterTests { + + @Test + void whenPropertiesEnabledIsSetAdapterEnabledReturnsIt() { + StatsdProperties properties = new StatsdProperties(); + properties.setEnabled(false); + assertThat(new StatsdPropertiesConfigAdapter(properties).enabled()).isEqualTo(properties.isEnabled()); + } + + @Test + void whenPropertiesFlavorIsSetAdapterFlavorReturnsIt() { + StatsdProperties properties = new StatsdProperties(); + properties.setFlavor(StatsdFlavor.ETSY); + assertThat(new StatsdPropertiesConfigAdapter(properties).flavor()).isEqualTo(properties.getFlavor()); + } + + @Test + void whenPropertiesHostIsSetAdapterHostReturnsIt() { + StatsdProperties properties = new StatsdProperties(); + properties.setHost("my-host"); + assertThat(new StatsdPropertiesConfigAdapter(properties).host()).isEqualTo(properties.getHost()); + } + + @Test + void whenPropertiesPortIsSetAdapterPortReturnsIt() { + StatsdProperties properties = new StatsdProperties(); + properties.setPort(1234); + assertThat(new StatsdPropertiesConfigAdapter(properties).port()).isEqualTo(properties.getPort()); + } + + @Test + void whenPropertiesProtocolIsSetAdapterProtocolReturnsIt() { + StatsdProperties properties = new StatsdProperties(); + properties.setProtocol(StatsdProtocol.TCP); + assertThat(new StatsdPropertiesConfigAdapter(properties).protocol()).isEqualTo(properties.getProtocol()); + } + + @Test + void whenPropertiesMaxPacketLengthIsSetAdapterMaxPacketLengthReturnsIt() { + StatsdProperties properties = new StatsdProperties(); + properties.setMaxPacketLength(1234); + assertThat(new StatsdPropertiesConfigAdapter(properties).maxPacketLength()) + .isEqualTo(properties.getMaxPacketLength()); + } + + @Test + void whenPropertiesPollingFrequencyIsSetAdapterPollingFrequencyReturnsIt() { + StatsdProperties properties = new StatsdProperties(); + properties.setPollingFrequency(Duration.ofSeconds(1)); + assertThat(new StatsdPropertiesConfigAdapter(properties).pollingFrequency()) + .isEqualTo(properties.getPollingFrequency()); + } + + @Test + void whenPropertiesPublishUnchangedMetersIsSetAdapterPublishUnchangedMetersReturnsIt() { + StatsdProperties properties = new StatsdProperties(); + properties.setPublishUnchangedMeters(false); + assertThat(new StatsdPropertiesConfigAdapter(properties).publishUnchangedMeters()) + .isEqualTo(properties.isPublishUnchangedMeters()); + } + + @Test + void whenPropertiesBufferedIsSetAdapterBufferedReturnsIt() { + StatsdProperties properties = new StatsdProperties(); + properties.setBuffered(false); + assertThat(new StatsdPropertiesConfigAdapter(properties).buffered()).isEqualTo(properties.isBuffered()); + } + +} diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/statsd/StatsdPropertiesTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/statsd/StatsdPropertiesTests.java index ae1f6fc60894..d402a1468d49 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/statsd/StatsdPropertiesTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/statsd/StatsdPropertiesTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -40,6 +40,7 @@ void defaultValuesAreConsistent() { assertThat(properties.getMaxPacketLength()).isEqualTo(config.maxPacketLength()); assertThat(properties.getPollingFrequency()).isEqualTo(config.pollingFrequency()); assertThat(properties.isPublishUnchangedMeters()).isEqualTo(config.publishUnchangedMeters()); + assertThat(properties.isBuffered()).isEqualTo(config.buffered()); } } From ccbe758c3e37340a5c4651a3548b9f7166cd0d05 Mon Sep 17 00:00:00 2001 From: izeye Date: Mon, 16 May 2022 21:56:35 +0900 Subject: [PATCH 2/2] Expose step property for StatsdConfig --- .../metrics/export/statsd/StatsdProperties.java | 15 +++++++++++++++ .../statsd/StatsdPropertiesConfigAdapter.java | 5 +++++ .../StatsdPropertiesConfigAdapterTests.java | 7 +++++++ .../export/statsd/StatsdPropertiesTests.java | 1 + 4 files changed, 28 insertions(+) diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/statsd/StatsdProperties.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/statsd/StatsdProperties.java index b158ae150080..37fc9297ca56 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/statsd/StatsdProperties.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/statsd/StatsdProperties.java @@ -71,6 +71,13 @@ public class StatsdProperties { */ private Duration pollingFrequency = Duration.ofSeconds(10); + /** + * The step size to use in computing windowed statistics like max. The default is 1 + * minute. To get the most out of these statistics, align the step interval to be + * close to your scrape interval. + */ + private Duration step = Duration.ofMinutes(1); + /** * Whether to send unchanged meters to the StatsD server. */ @@ -137,6 +144,14 @@ public void setPollingFrequency(Duration pollingFrequency) { this.pollingFrequency = pollingFrequency; } + public Duration getStep() { + return this.step; + } + + public void setStep(Duration step) { + this.step = step; + } + public boolean isPublishUnchangedMeters() { return this.publishUnchangedMeters; } diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/statsd/StatsdPropertiesConfigAdapter.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/statsd/StatsdPropertiesConfigAdapter.java index e048aa6aafb5..c40fff082a78 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/statsd/StatsdPropertiesConfigAdapter.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/statsd/StatsdPropertiesConfigAdapter.java @@ -81,6 +81,11 @@ public Duration pollingFrequency() { return get(StatsdProperties::getPollingFrequency, StatsdConfig.super::pollingFrequency); } + @Override + public Duration step() { + return get(StatsdProperties::getStep, StatsdConfig.super::step); + } + @Override public boolean publishUnchangedMeters() { return get(StatsdProperties::isPublishUnchangedMeters, StatsdConfig.super::publishUnchangedMeters); diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/statsd/StatsdPropertiesConfigAdapterTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/statsd/StatsdPropertiesConfigAdapterTests.java index ac8150909c21..e6bce8be431d 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/statsd/StatsdPropertiesConfigAdapterTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/statsd/StatsdPropertiesConfigAdapterTests.java @@ -82,6 +82,13 @@ void whenPropertiesPollingFrequencyIsSetAdapterPollingFrequencyReturnsIt() { .isEqualTo(properties.getPollingFrequency()); } + @Test + void whenPropertiesStepIsSetAdapterStepReturnsIt() { + StatsdProperties properties = new StatsdProperties(); + properties.setStep(Duration.ofSeconds(1)); + assertThat(new StatsdPropertiesConfigAdapter(properties).step()).isEqualTo(properties.getStep()); + } + @Test void whenPropertiesPublishUnchangedMetersIsSetAdapterPublishUnchangedMetersReturnsIt() { StatsdProperties properties = new StatsdProperties(); diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/statsd/StatsdPropertiesTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/statsd/StatsdPropertiesTests.java index d402a1468d49..b41ac2ff9b7c 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/statsd/StatsdPropertiesTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/statsd/StatsdPropertiesTests.java @@ -39,6 +39,7 @@ void defaultValuesAreConsistent() { assertThat(properties.getProtocol()).isEqualTo(config.protocol()); assertThat(properties.getMaxPacketLength()).isEqualTo(config.maxPacketLength()); assertThat(properties.getPollingFrequency()).isEqualTo(config.pollingFrequency()); + assertThat(properties.getStep()).isEqualTo(config.step()); assertThat(properties.isPublishUnchangedMeters()).isEqualTo(config.publishUnchangedMeters()); assertThat(properties.isBuffered()).isEqualTo(config.buffered()); }