From 358d144dd0926860627db903c52d3f5befeca295 Mon Sep 17 00:00:00 2001 From: Igor Melnichenko Date: Wed, 19 Nov 2025 10:50:22 +0300 Subject: [PATCH 1/2] preferUncompressedResponse option was added Signed-off-by: Igor Melnichenko --- .../config/ExporterHttpServerProperties.java | 26 ++++++++++++++++--- .../ExporterHttpServerPropertiesTest.java | 10 ++++++- .../common/PrometheusScrapeHandler.java | 7 +++++ 3 files changed, 39 insertions(+), 4 deletions(-) diff --git a/prometheus-metrics-config/src/main/java/io/prometheus/metrics/config/ExporterHttpServerProperties.java b/prometheus-metrics-config/src/main/java/io/prometheus/metrics/config/ExporterHttpServerProperties.java index 55c15a60c..500dc6509 100644 --- a/prometheus-metrics-config/src/main/java/io/prometheus/metrics/config/ExporterHttpServerProperties.java +++ b/prometheus-metrics-config/src/main/java/io/prometheus/metrics/config/ExporterHttpServerProperties.java @@ -7,11 +7,14 @@ public class ExporterHttpServerProperties { private static final String PORT = "port"; + private static final String PREFER_UNCOMPRESSED_RESPONSE = "preferUncompressedResponse"; private static final String PREFIX = "io.prometheus.exporter.httpServer"; @Nullable private final Integer port; + private final boolean preferUncompressedResponse; - private ExporterHttpServerProperties(@Nullable Integer port) { + private ExporterHttpServerProperties(@Nullable Integer port, boolean preferUncompressedResponse) { this.port = port; + this.preferUncompressedResponse = preferUncompressedResponse; } @Nullable @@ -19,6 +22,10 @@ public Integer getPort() { return port; } + public boolean isPreferUncompressedResponse() { + return preferUncompressedResponse; + } + /** * Note that this will remove entries from {@code properties}. This is because we want to know if * there are unused properties remaining after all properties have been loaded. @@ -27,7 +34,14 @@ static ExporterHttpServerProperties load(Map properties) throws PrometheusPropertiesException { Integer port = Util.loadInteger(PREFIX + "." + PORT, properties); Util.assertValue(port, t -> t > 0, "Expecting value > 0.", PREFIX, PORT); - return new ExporterHttpServerProperties(port); + + Boolean preferUncompressedResponse = + Util.loadBoolean(PREFIX + "." + PREFER_UNCOMPRESSED_RESPONSE, properties); + + return new ExporterHttpServerProperties( + port, + preferUncompressedResponse != null && preferUncompressedResponse + ); } public static Builder builder() { @@ -37,6 +51,7 @@ public static Builder builder() { public static class Builder { @Nullable private Integer port; + private boolean preferUncompressedResponse = false; private Builder() {} @@ -45,8 +60,13 @@ public Builder port(int port) { return this; } + public Builder preferUncompressedResponse(boolean preferUncompressedResponse) { + this.preferUncompressedResponse = preferUncompressedResponse; + return this; + } + public ExporterHttpServerProperties build() { - return new ExporterHttpServerProperties(port); + return new ExporterHttpServerProperties(port, preferUncompressedResponse); } } } diff --git a/prometheus-metrics-config/src/test/java/io/prometheus/metrics/config/ExporterHttpServerPropertiesTest.java b/prometheus-metrics-config/src/test/java/io/prometheus/metrics/config/ExporterHttpServerPropertiesTest.java index 07b00a2a4..c1fc7d273 100644 --- a/prometheus-metrics-config/src/test/java/io/prometheus/metrics/config/ExporterHttpServerPropertiesTest.java +++ b/prometheus-metrics-config/src/test/java/io/prometheus/metrics/config/ExporterHttpServerPropertiesTest.java @@ -2,6 +2,7 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; +import static org.assertj.core.api.SoftAssertions.assertSoftly; import java.util.HashMap; import java.util.Map; @@ -13,6 +14,7 @@ void load() { ExporterHttpServerProperties properties = load(Map.of("io.prometheus.exporter.httpServer.port", "1")); assertThat(properties.getPort()).isOne(); + assertThat(properties.isPreferUncompressedResponse()).isFalse(); assertThatExceptionOfType(PrometheusPropertiesException.class) .isThrownBy(() -> load(Map.of("io.prometheus.exporter.httpServer.port", "0"))) @@ -21,7 +23,13 @@ void load() { @Test void builder() { - assertThat(ExporterHttpServerProperties.builder().port(1).build().getPort()).isOne(); + ExporterHttpServerProperties properties = + ExporterHttpServerProperties.builder().port(1).build(); + + assertSoftly(softly -> { + softly.assertThat(properties.getPort()).isOne(); + softly.assertThat(properties.isPreferUncompressedResponse()).isFalse(); + }); } private static ExporterHttpServerProperties load(Map map) { diff --git a/prometheus-metrics-exporter-common/src/main/java/io/prometheus/metrics/exporter/common/PrometheusScrapeHandler.java b/prometheus-metrics-exporter-common/src/main/java/io/prometheus/metrics/exporter/common/PrometheusScrapeHandler.java index 3ce4bdb98..5c65e44b9 100644 --- a/prometheus-metrics-exporter-common/src/main/java/io/prometheus/metrics/exporter/common/PrometheusScrapeHandler.java +++ b/prometheus-metrics-exporter-common/src/main/java/io/prometheus/metrics/exporter/common/PrometheusScrapeHandler.java @@ -29,6 +29,7 @@ public class PrometheusScrapeHandler { @Nullable private final Predicate nameFilter; private final AtomicInteger lastResponseSize = new AtomicInteger(2 << 9); // 0.5 MB private final List supportedFormats; + private final boolean preferUncompressedResponse; public PrometheusScrapeHandler() { this(PrometheusProperties.get(), PrometheusRegistry.defaultRegistry); @@ -44,6 +45,8 @@ public PrometheusScrapeHandler(PrometheusProperties config) { public PrometheusScrapeHandler(PrometheusProperties config, PrometheusRegistry registry) { this.expositionFormats = ExpositionFormats.init(config.getExporterProperties()); + this.preferUncompressedResponse = + config.getExporterHttpServerProperties().isPreferUncompressedResponse(); this.registry = registry; this.nameFilter = makeNameFilter(config.getExporterFilterProperties()); supportedFormats = new ArrayList<>(Arrays.asList("openmetrics", "text")); @@ -180,6 +183,10 @@ private boolean writeDebugResponse( } private boolean shouldUseCompression(PrometheusHttpRequest request) { + if (preferUncompressedResponse) { + return false; + } + Enumeration encodingHeaders = request.getHeaders("Accept-Encoding"); if (encodingHeaders == null) { return false; From d7edce99bd639815d793c3ecd5dcc5cf892affa5 Mon Sep 17 00:00:00 2001 From: Igor Melnichenko Date: Thu, 20 Nov 2025 16:02:08 +0300 Subject: [PATCH 2/2] Formatting was fixed Signed-off-by: Igor Melnichenko --- .../metrics/config/ExporterHttpServerProperties.java | 6 ++---- .../config/ExporterHttpServerPropertiesTest.java | 11 ++++++----- .../exporter/common/PrometheusScrapeHandler.java | 2 +- 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/prometheus-metrics-config/src/main/java/io/prometheus/metrics/config/ExporterHttpServerProperties.java b/prometheus-metrics-config/src/main/java/io/prometheus/metrics/config/ExporterHttpServerProperties.java index 500dc6509..01849f55a 100644 --- a/prometheus-metrics-config/src/main/java/io/prometheus/metrics/config/ExporterHttpServerProperties.java +++ b/prometheus-metrics-config/src/main/java/io/prometheus/metrics/config/ExporterHttpServerProperties.java @@ -36,12 +36,10 @@ static ExporterHttpServerProperties load(Map properties) Util.assertValue(port, t -> t > 0, "Expecting value > 0.", PREFIX, PORT); Boolean preferUncompressedResponse = - Util.loadBoolean(PREFIX + "." + PREFER_UNCOMPRESSED_RESPONSE, properties); + Util.loadBoolean(PREFIX + "." + PREFER_UNCOMPRESSED_RESPONSE, properties); return new ExporterHttpServerProperties( - port, - preferUncompressedResponse != null && preferUncompressedResponse - ); + port, preferUncompressedResponse != null && preferUncompressedResponse); } public static Builder builder() { diff --git a/prometheus-metrics-config/src/test/java/io/prometheus/metrics/config/ExporterHttpServerPropertiesTest.java b/prometheus-metrics-config/src/test/java/io/prometheus/metrics/config/ExporterHttpServerPropertiesTest.java index c1fc7d273..ae83c305e 100644 --- a/prometheus-metrics-config/src/test/java/io/prometheus/metrics/config/ExporterHttpServerPropertiesTest.java +++ b/prometheus-metrics-config/src/test/java/io/prometheus/metrics/config/ExporterHttpServerPropertiesTest.java @@ -24,12 +24,13 @@ void load() { @Test void builder() { ExporterHttpServerProperties properties = - ExporterHttpServerProperties.builder().port(1).build(); + ExporterHttpServerProperties.builder().port(1).build(); - assertSoftly(softly -> { - softly.assertThat(properties.getPort()).isOne(); - softly.assertThat(properties.isPreferUncompressedResponse()).isFalse(); - }); + assertSoftly( + softly -> { + softly.assertThat(properties.getPort()).isOne(); + softly.assertThat(properties.isPreferUncompressedResponse()).isFalse(); + }); } private static ExporterHttpServerProperties load(Map map) { diff --git a/prometheus-metrics-exporter-common/src/main/java/io/prometheus/metrics/exporter/common/PrometheusScrapeHandler.java b/prometheus-metrics-exporter-common/src/main/java/io/prometheus/metrics/exporter/common/PrometheusScrapeHandler.java index 5c65e44b9..440110b33 100644 --- a/prometheus-metrics-exporter-common/src/main/java/io/prometheus/metrics/exporter/common/PrometheusScrapeHandler.java +++ b/prometheus-metrics-exporter-common/src/main/java/io/prometheus/metrics/exporter/common/PrometheusScrapeHandler.java @@ -46,7 +46,7 @@ public PrometheusScrapeHandler(PrometheusProperties config) { public PrometheusScrapeHandler(PrometheusProperties config, PrometheusRegistry registry) { this.expositionFormats = ExpositionFormats.init(config.getExporterProperties()); this.preferUncompressedResponse = - config.getExporterHttpServerProperties().isPreferUncompressedResponse(); + config.getExporterHttpServerProperties().isPreferUncompressedResponse(); this.registry = registry; this.nameFilter = makeNameFilter(config.getExporterFilterProperties()); supportedFormats = new ArrayList<>(Arrays.asList("openmetrics", "text"));