From 53e7503fd4e0ca8ea973a2da868e5c953ea23a3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Nicoll?= Date: Wed, 18 Feb 2026 13:58:48 +0100 Subject: [PATCH] Polish "Add support for configuring publishMaxGaugeForHistograms" See gh-49242 --- .../export/otlp/OtlpMetricsProperties.java | 27 +++++++------- .../OtlpMetricsPropertiesConfigAdapter.java | 12 +++---- ...lpMetricsPropertiesConfigAdapterTests.java | 36 +++++++++---------- 3 files changed, 37 insertions(+), 38 deletions(-) diff --git a/module/spring-boot-micrometer-metrics/src/main/java/org/springframework/boot/micrometer/metrics/autoconfigure/export/otlp/OtlpMetricsProperties.java b/module/spring-boot-micrometer-metrics/src/main/java/org/springframework/boot/micrometer/metrics/autoconfigure/export/otlp/OtlpMetricsProperties.java index 770b824ef3e..0f4687adbdf 100644 --- a/module/spring-boot-micrometer-metrics/src/main/java/org/springframework/boot/micrometer/metrics/autoconfigure/export/otlp/OtlpMetricsProperties.java +++ b/module/spring-boot-micrometer-metrics/src/main/java/org/springframework/boot/micrometer/metrics/autoconfigure/export/otlp/OtlpMetricsProperties.java @@ -65,6 +65,11 @@ public class OtlpMetricsProperties extends StepRegistryProperties { */ private HistogramFlavor histogramFlavor = HistogramFlavor.EXPLICIT_BUCKET_HISTOGRAM; + /** + * Whether to publish a separate gauge for the max value of histogram-based meters. + */ + private @Nullable Boolean publishMaxGaugeForHistograms; + /** * Max scale to use for exponential histograms, if configured. */ @@ -86,12 +91,6 @@ public class OtlpMetricsProperties extends StepRegistryProperties { */ private final Map meter = new LinkedHashMap<>(); - /** - * Whether to publish a separate gauge for the max value of histogram-based meters. A - * null value defers to Micrometer's default. - */ - private @Nullable Boolean publishMaxGaugeForHistograms; - public @Nullable String getUrl() { return this.url; } @@ -132,6 +131,14 @@ public class OtlpMetricsProperties extends StepRegistryProperties { this.histogramFlavor = histogramFlavor; } + public @Nullable Boolean getPublishMaxGaugeForHistograms() { + return this.publishMaxGaugeForHistograms; + } + + public void setPublishMaxGaugeForHistograms(@Nullable Boolean publishMaxGaugeForHistograms) { + this.publishMaxGaugeForHistograms = publishMaxGaugeForHistograms; + } + public int getMaxScale() { return this.maxScale; } @@ -160,14 +167,6 @@ public class OtlpMetricsProperties extends StepRegistryProperties { return this.meter; } - public @Nullable Boolean getPublishMaxGaugeForHistograms() { - return this.publishMaxGaugeForHistograms; - } - - public void setPublishMaxGaugeForHistograms(@Nullable Boolean publishMaxGaugeForHistograms) { - this.publishMaxGaugeForHistograms = publishMaxGaugeForHistograms; - } - /** * Per-meter settings. */ diff --git a/module/spring-boot-micrometer-metrics/src/main/java/org/springframework/boot/micrometer/metrics/autoconfigure/export/otlp/OtlpMetricsPropertiesConfigAdapter.java b/module/spring-boot-micrometer-metrics/src/main/java/org/springframework/boot/micrometer/metrics/autoconfigure/export/otlp/OtlpMetricsPropertiesConfigAdapter.java index 3a6bebeb146..4cc7073c009 100644 --- a/module/spring-boot-micrometer-metrics/src/main/java/org/springframework/boot/micrometer/metrics/autoconfigure/export/otlp/OtlpMetricsPropertiesConfigAdapter.java +++ b/module/spring-boot-micrometer-metrics/src/main/java/org/springframework/boot/micrometer/metrics/autoconfigure/export/otlp/OtlpMetricsPropertiesConfigAdapter.java @@ -106,6 +106,12 @@ class OtlpMetricsPropertiesConfigAdapter extends StepRegistryPropertiesConfigAda return obtain(perMeter(Meter::getMaxBucketCount), OtlpConfig.super::maxBucketsPerMeter); } + @Override + public boolean publishMaxGaugeForHistograms() { + return obtain(OtlpMetricsProperties::getPublishMaxGaugeForHistograms, + OtlpConfig.super::publishMaxGaugeForHistograms); + } + @Override public int maxScale() { return obtain(OtlpMetricsProperties::getMaxScale, OtlpConfig.super::maxScale); @@ -121,12 +127,6 @@ class OtlpMetricsPropertiesConfigAdapter extends StepRegistryPropertiesConfigAda return obtain(OtlpMetricsProperties::getBaseTimeUnit, OtlpConfig.super::baseTimeUnit); } - @Override - public boolean publishMaxGaugeForHistograms() { - return obtain(OtlpMetricsProperties::getPublishMaxGaugeForHistograms, - OtlpConfig.super::publishMaxGaugeForHistograms); - } - private Getter> perMeter(Getter getter) { return (properties) -> { if (CollectionUtils.isEmpty(properties.getMeter())) { diff --git a/module/spring-boot-micrometer-metrics/src/test/java/org/springframework/boot/micrometer/metrics/autoconfigure/export/otlp/OtlpMetricsPropertiesConfigAdapterTests.java b/module/spring-boot-micrometer-metrics/src/test/java/org/springframework/boot/micrometer/metrics/autoconfigure/export/otlp/OtlpMetricsPropertiesConfigAdapterTests.java index ba30cf1ec06..1ab506cb9e3 100644 --- a/module/spring-boot-micrometer-metrics/src/test/java/org/springframework/boot/micrometer/metrics/autoconfigure/export/otlp/OtlpMetricsPropertiesConfigAdapterTests.java +++ b/module/spring-boot-micrometer-metrics/src/test/java/org/springframework/boot/micrometer/metrics/autoconfigure/export/otlp/OtlpMetricsPropertiesConfigAdapterTests.java @@ -137,6 +137,24 @@ class OtlpMetricsPropertiesConfigAdapterTests { HistogramFlavor.BASE2_EXPONENTIAL_BUCKET_HISTOGRAM); } + @Test + void useDefaultPublishMaxGaugeForHistogramsWhenNotSet() { + assertThat(this.properties.getPublishMaxGaugeForHistograms()).isNull(); + assertThat(createAdapter().publishMaxGaugeForHistograms()).isTrue(); + } + + @Test + void whenDefaultPublishMaxGaugeForHistogramsIsSetAdapterUsesIt() { + this.properties.setPublishMaxGaugeForHistograms(false); + assertThat(createAdapter().publishMaxGaugeForHistograms()).isFalse(); + } + + @Test + void whenAggregationTemporalityIsSetToDeltaThenPublishMaxGaugeForHistogramsDefaultChanges() { + this.properties.setAggregationTemporality(AggregationTemporality.DELTA); + assertThat(createAdapter().publishMaxGaugeForHistograms()).isFalse(); + } + @Test void whenPropertiesMaxScaleIsNotSetAdapterMaxScaleReturns20() { assertThat(createAdapter().maxScale()).isEqualTo(20); @@ -219,24 +237,6 @@ class OtlpMetricsPropertiesConfigAdapterTests { assertThat(createAdapter().resourceAttributes()).doesNotContainKey("service.namespace"); } - @Test - void useDefaultPublishMaxGaugeForHistogramsWhenNotSet() { - assertThat(this.properties.getPublishMaxGaugeForHistograms()).isNull(); - assertThat(createAdapter().publishMaxGaugeForHistograms()).isTrue(); - } - - @Test - void whenDefaultPublishMaxGaugeForHistogramsIsSetAdapterUsesIt() { - this.properties.setPublishMaxGaugeForHistograms(false); - assertThat(createAdapter().publishMaxGaugeForHistograms()).isFalse(); - } - - @Test - void whenAggregationTemporalityIsSetToDeltaThenPublishMaxGaugeForHistogramsDefaultChanges() { - this.properties.setAggregationTemporality(AggregationTemporality.DELTA); - assertThat(createAdapter().publishMaxGaugeForHistograms()).isFalse(); - } - private OtlpMetricsPropertiesConfigAdapter createAdapter() { return new OtlpMetricsPropertiesConfigAdapter(this.properties, this.openTelemetryProperties, this.connectionDetails, this.environment);