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..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 @@ -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,12 @@ 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 +49,7 @@ public static Builder builder() { public static class Builder { @Nullable private Integer port; + private boolean preferUncompressedResponse = false; private Builder() {} @@ -45,8 +58,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..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 @@ -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,14 @@ 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..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 @@ -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;