From 6947d330f4dc7b4d7b3bdea1aebd8631cb139db4 Mon Sep 17 00:00:00 2001 From: Moritz Halbritter Date: Mon, 10 Jun 2024 15:02:12 +0200 Subject: [PATCH] WIP --- ...metheusMetricsExportAutoConfiguration.java | 5 +++-- ...theusScrapeEndpointDocumentationTests.java | 3 ++- ...usMetricsExportAutoConfigurationTests.java | 6 ++++-- ...usMetricsExportAutoConfigurationTests.java | 6 ++++-- .../prometheus/PrometheusOutputFormat.java | 20 ++++++++++--------- .../prometheus/PrometheusScrapeEndpoint.java | 20 ++++++++++++++++++- ...metheusScrapeEndpointIntegrationTests.java | 3 ++- ...metheusScrapeEndpointIntegrationTests.java | 3 ++- 8 files changed, 47 insertions(+), 19 deletions(-) diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/prometheus/PrometheusMetricsExportAutoConfiguration.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/prometheus/PrometheusMetricsExportAutoConfiguration.java index 9aba0ae77e6a..039867e8d112 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/prometheus/PrometheusMetricsExportAutoConfiguration.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/prometheus/PrometheusMetricsExportAutoConfiguration.java @@ -82,8 +82,9 @@ static class PrometheusScrapeEndpointConfiguration { @SuppressWarnings("removal") @Bean @ConditionalOnMissingBean({ PrometheusScrapeEndpoint.class, PrometheusSimpleclientScrapeEndpoint.class }) - PrometheusScrapeEndpoint prometheusEndpoint(PrometheusRegistry prometheusRegistry) { - return new PrometheusScrapeEndpoint(prometheusRegistry); + PrometheusScrapeEndpoint prometheusEndpoint(PrometheusRegistry prometheusRegistry, + PrometheusConfig prometheusConfig) { + return new PrometheusScrapeEndpoint(prometheusRegistry, prometheusConfig); } } diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/endpoint/web/documentation/PrometheusScrapeEndpointDocumentationTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/endpoint/web/documentation/PrometheusScrapeEndpointDocumentationTests.java index 429d2e34d4da..fdcdae0543d8 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/endpoint/web/documentation/PrometheusScrapeEndpointDocumentationTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/endpoint/web/documentation/PrometheusScrapeEndpointDocumentationTests.java @@ -18,6 +18,7 @@ import io.micrometer.core.instrument.Clock; import io.micrometer.core.instrument.binder.jvm.JvmMemoryMetrics; +import io.micrometer.prometheusmetrics.PrometheusConfig; import io.micrometer.prometheusmetrics.PrometheusMeterRegistry; import io.prometheus.client.exporter.common.TextFormat; import io.prometheus.metrics.model.registry.PrometheusRegistry; @@ -78,7 +79,7 @@ PrometheusScrapeEndpoint endpoint() { PrometheusMeterRegistry meterRegistry = new PrometheusMeterRegistry((key) -> null, prometheusRegistry, Clock.SYSTEM); new JvmMemoryMetrics().bindTo(meterRegistry); - return new PrometheusScrapeEndpoint(prometheusRegistry); + return new PrometheusScrapeEndpoint(prometheusRegistry, PrometheusConfig.DEFAULT); } } diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/prometheus/DualPrometheusMetricsExportAutoConfigurationTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/prometheus/DualPrometheusMetricsExportAutoConfigurationTests.java index 9b607d834e43..f54440df8719 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/prometheus/DualPrometheusMetricsExportAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/prometheus/DualPrometheusMetricsExportAutoConfigurationTests.java @@ -17,6 +17,7 @@ package org.springframework.boot.actuate.autoconfigure.metrics.export.prometheus; import io.micrometer.core.instrument.Clock; +import io.micrometer.prometheusmetrics.PrometheusConfig; import io.prometheus.client.CollectorRegistry; import io.prometheus.client.exemplars.ExemplarSampler; import io.prometheus.client.exemplars.tracer.common.SpanContextSupplier; @@ -358,8 +359,9 @@ PrometheusSimpleclientScrapeEndpoint customEndpoint(CollectorRegistry collectorR static class CustomSecondEndpointConfiguration { @Bean - PrometheusScrapeEndpoint prometheusScrapeEndpoint(PrometheusRegistry prometheusRegistry) { - return new PrometheusScrapeEndpoint(prometheusRegistry); + PrometheusScrapeEndpoint prometheusScrapeEndpoint(PrometheusRegistry prometheusRegistry, + PrometheusConfig prometheusConfig) { + return new PrometheusScrapeEndpoint(prometheusRegistry, prometheusConfig); } @Bean diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/prometheus/PrometheusMetricsExportAutoConfigurationTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/prometheus/PrometheusMetricsExportAutoConfigurationTests.java index 319ee2393f05..dbaeea58f5f6 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/prometheus/PrometheusMetricsExportAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/prometheus/PrometheusMetricsExportAutoConfigurationTests.java @@ -17,6 +17,7 @@ package org.springframework.boot.actuate.autoconfigure.metrics.export.prometheus; import io.micrometer.core.instrument.Clock; +import io.micrometer.prometheusmetrics.PrometheusConfig; import io.micrometer.prometheusmetrics.PrometheusMeterRegistry; import io.prometheus.metrics.model.registry.PrometheusRegistry; import io.prometheus.metrics.tracer.common.SpanContext; @@ -207,8 +208,9 @@ PrometheusRegistry customPrometheusRegistry() { static class CustomEndpointConfiguration { @Bean - PrometheusScrapeEndpoint customEndpoint(PrometheusRegistry prometheusRegistry) { - return new PrometheusScrapeEndpoint(prometheusRegistry); + PrometheusScrapeEndpoint customEndpoint(PrometheusRegistry prometheusRegistry, + PrometheusConfig prometheusConfig) { + return new PrometheusScrapeEndpoint(prometheusRegistry, prometheusConfig); } } diff --git a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/export/prometheus/PrometheusOutputFormat.java b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/export/prometheus/PrometheusOutputFormat.java index 25d18012e787..312b688fd5e5 100644 --- a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/export/prometheus/PrometheusOutputFormat.java +++ b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/export/prometheus/PrometheusOutputFormat.java @@ -43,8 +43,9 @@ public enum PrometheusOutputFormat implements Producible CONTENT_TYPE_004(PrometheusTextFormatWriter.CONTENT_TYPE) { @Override - void write(OutputStream outputStream, MetricSnapshots snapshots) throws IOException { - EXPOSITION_FORMATS.getPrometheusTextFormatWriter().write(outputStream, snapshots); + void write(OutputStream outputStream, MetricSnapshots snapshots, ExpositionFormats expositionFormats) + throws IOException { + expositionFormats.getPrometheusTextFormatWriter().write(outputStream, snapshots); } @Override @@ -60,8 +61,9 @@ public boolean isDefault() { CONTENT_TYPE_OPENMETRICS_100(OpenMetricsTextFormatWriter.CONTENT_TYPE) { @Override - void write(OutputStream outputStream, MetricSnapshots snapshots) throws IOException { - EXPOSITION_FORMATS.getOpenMetricsTextFormatWriter().write(outputStream, snapshots); + void write(OutputStream outputStream, MetricSnapshots snapshots, ExpositionFormats expositionFormats) + throws IOException { + expositionFormats.getOpenMetricsTextFormatWriter().write(outputStream, snapshots); } }, @@ -72,14 +74,13 @@ void write(OutputStream outputStream, MetricSnapshots snapshots) throws IOExcept CONTENT_TYPE_PROTOBUF(PrometheusProtobufWriter.CONTENT_TYPE) { @Override - void write(OutputStream outputStream, MetricSnapshots snapshots) throws IOException { - EXPOSITION_FORMATS.getPrometheusProtobufWriter().write(outputStream, snapshots); + void write(OutputStream outputStream, MetricSnapshots snapshots, ExpositionFormats expositionFormats) + throws IOException { + expositionFormats.getPrometheusProtobufWriter().write(outputStream, snapshots); } }; - private static final ExpositionFormats EXPOSITION_FORMATS = ExpositionFormats.init(); - private final MimeType mimeType; PrometheusOutputFormat(String mimeType) { @@ -91,6 +92,7 @@ public MimeType getProducedMimeType() { return this.mimeType; } - abstract void write(OutputStream outputStream, MetricSnapshots snapshots) throws IOException; + abstract void write(OutputStream outputStream, MetricSnapshots snapshots, ExpositionFormats expositionFormats) + throws IOException; } diff --git a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/export/prometheus/PrometheusScrapeEndpoint.java b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/export/prometheus/PrometheusScrapeEndpoint.java index 06376ed30b8d..ce0aacd5dd9e 100644 --- a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/export/prometheus/PrometheusScrapeEndpoint.java +++ b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/export/prometheus/PrometheusScrapeEndpoint.java @@ -20,6 +20,10 @@ import java.io.IOException; import java.util.Set; +import io.micrometer.prometheusmetrics.PrometheusConfig; +import io.prometheus.metrics.config.PrometheusProperties; +import io.prometheus.metrics.config.PrometheusPropertiesLoader; +import io.prometheus.metrics.expositionformats.ExpositionFormats; import io.prometheus.metrics.model.registry.PrometheusRegistry; import io.prometheus.metrics.model.snapshots.MetricSnapshots; @@ -45,10 +49,24 @@ public class PrometheusScrapeEndpoint { private final PrometheusRegistry prometheusRegistry; + private final ExpositionFormats expositionFormats; + private volatile int nextMetricsScrapeSize = 16; + /** + * Creates a new {@link PrometheusScrapeEndpoint}. + * @param prometheusRegistry the Prometheus registry to use + * @deprecated since 3.3.1 for removal in 3.5.0 + */ + @Deprecated(since = "3.3.1", forRemoval = true) public PrometheusScrapeEndpoint(PrometheusRegistry prometheusRegistry) { + this(prometheusRegistry, PrometheusConfig.DEFAULT); + } + + public PrometheusScrapeEndpoint(PrometheusRegistry prometheusRegistry, PrometheusConfig prometheusConfig) { this.prometheusRegistry = prometheusRegistry; + PrometheusProperties properties = PrometheusPropertiesLoader.load(prometheusConfig.prometheusProperties()); + this.expositionFormats = ExpositionFormats.init(properties.getExporterProperties()); } @ReadOperation(producesFrom = PrometheusOutputFormat.class) @@ -57,7 +75,7 @@ public WebEndpointResponse scrape(PrometheusOutputFormat format, @Nullab ByteArrayOutputStream outputStream = new ByteArrayOutputStream(this.nextMetricsScrapeSize); MetricSnapshots metricSnapshots = (includedNames != null) ? this.prometheusRegistry.scrape(includedNames::contains) : this.prometheusRegistry.scrape(); - format.write(outputStream, metricSnapshots); + format.write(outputStream, metricSnapshots, this.expositionFormats); byte[] content = outputStream.toByteArray(); this.nextMetricsScrapeSize = content.length + METRICS_SCRAPE_CHARS_EXTRA; return new WebEndpointResponse<>(content, format); diff --git a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/export/prometheus/PrometheusScrapeEndpointIntegrationTests.java b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/export/prometheus/PrometheusScrapeEndpointIntegrationTests.java index 6ea796ef3b5a..5e71b27f2c54 100644 --- a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/export/prometheus/PrometheusScrapeEndpointIntegrationTests.java +++ b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/export/prometheus/PrometheusScrapeEndpointIntegrationTests.java @@ -19,6 +19,7 @@ import io.micrometer.core.instrument.Clock; import io.micrometer.core.instrument.Counter; import io.micrometer.core.instrument.MeterRegistry; +import io.micrometer.prometheusmetrics.PrometheusConfig; import io.micrometer.prometheusmetrics.PrometheusMeterRegistry; import io.prometheus.metrics.expositionformats.OpenMetricsTextFormatWriter; import io.prometheus.metrics.expositionformats.PrometheusProtobufWriter; @@ -141,7 +142,7 @@ static class TestConfiguration { @Bean PrometheusScrapeEndpoint prometheusScrapeEndpoint(PrometheusRegistry prometheusRegistry) { - return new PrometheusScrapeEndpoint(prometheusRegistry); + return new PrometheusScrapeEndpoint(prometheusRegistry, PrometheusConfig.DEFAULT); } @Bean diff --git a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/export/prometheus/SecondCustomPrometheusScrapeEndpointIntegrationTests.java b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/export/prometheus/SecondCustomPrometheusScrapeEndpointIntegrationTests.java index b77c73f6e4df..5fc78b21315f 100644 --- a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/export/prometheus/SecondCustomPrometheusScrapeEndpointIntegrationTests.java +++ b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/export/prometheus/SecondCustomPrometheusScrapeEndpointIntegrationTests.java @@ -19,6 +19,7 @@ import io.micrometer.core.instrument.Clock; import io.micrometer.core.instrument.Counter; import io.micrometer.core.instrument.composite.CompositeMeterRegistry; +import io.micrometer.prometheusmetrics.PrometheusConfig; import io.micrometer.prometheusmetrics.PrometheusMeterRegistry; import io.prometheus.client.CollectorRegistry; import io.prometheus.metrics.expositionformats.OpenMetricsTextFormatWriter; @@ -183,7 +184,7 @@ static class TestConfiguration { @Bean PrometheusScrapeEndpoint prometheusScrapeEndpoint(PrometheusRegistry prometheusRegistry) { - return new PrometheusScrapeEndpoint(prometheusRegistry); + return new PrometheusScrapeEndpoint(prometheusRegistry, PrometheusConfig.DEFAULT); } @Bean