Skip to content

Commit cfb1bda

Browse files
authored
Adding ability to disable compression in PrometheusScrapeHandler (#1691)
Resolves #1684 --------- Signed-off-by: Igor Melnichenko <[email protected]>
1 parent d8bc338 commit cfb1bda

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

prometheus-metrics-config/src/main/java/io/prometheus/metrics/config/ExporterHttpServerProperties.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,25 @@
77
public class ExporterHttpServerProperties {
88

99
private static final String PORT = "port";
10+
private static final String PREFER_UNCOMPRESSED_RESPONSE = "preferUncompressedResponse";
1011
private static final String PREFIX = "io.prometheus.exporter.httpServer";
1112
@Nullable private final Integer port;
13+
private final boolean preferUncompressedResponse;
1214

13-
private ExporterHttpServerProperties(@Nullable Integer port) {
15+
private ExporterHttpServerProperties(@Nullable Integer port, boolean preferUncompressedResponse) {
1416
this.port = port;
17+
this.preferUncompressedResponse = preferUncompressedResponse;
1518
}
1619

1720
@Nullable
1821
public Integer getPort() {
1922
return port;
2023
}
2124

25+
public boolean isPreferUncompressedResponse() {
26+
return preferUncompressedResponse;
27+
}
28+
2229
/**
2330
* Note that this will remove entries from {@code properties}. This is because we want to know if
2431
* there are unused properties remaining after all properties have been loaded.
@@ -27,7 +34,12 @@ static ExporterHttpServerProperties load(Map<Object, Object> properties)
2734
throws PrometheusPropertiesException {
2835
Integer port = Util.loadInteger(PREFIX + "." + PORT, properties);
2936
Util.assertValue(port, t -> t > 0, "Expecting value > 0.", PREFIX, PORT);
30-
return new ExporterHttpServerProperties(port);
37+
38+
Boolean preferUncompressedResponse =
39+
Util.loadBoolean(PREFIX + "." + PREFER_UNCOMPRESSED_RESPONSE, properties);
40+
41+
return new ExporterHttpServerProperties(
42+
port, preferUncompressedResponse != null && preferUncompressedResponse);
3143
}
3244

3345
public static Builder builder() {
@@ -37,6 +49,7 @@ public static Builder builder() {
3749
public static class Builder {
3850

3951
@Nullable private Integer port;
52+
private boolean preferUncompressedResponse = false;
4053

4154
private Builder() {}
4255

@@ -45,8 +58,13 @@ public Builder port(int port) {
4558
return this;
4659
}
4760

61+
public Builder preferUncompressedResponse(boolean preferUncompressedResponse) {
62+
this.preferUncompressedResponse = preferUncompressedResponse;
63+
return this;
64+
}
65+
4866
public ExporterHttpServerProperties build() {
49-
return new ExporterHttpServerProperties(port);
67+
return new ExporterHttpServerProperties(port, preferUncompressedResponse);
5068
}
5169
}
5270
}

prometheus-metrics-config/src/test/java/io/prometheus/metrics/config/ExporterHttpServerPropertiesTest.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static org.assertj.core.api.Assertions.assertThat;
44
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
5+
import static org.assertj.core.api.SoftAssertions.assertSoftly;
56

67
import java.util.HashMap;
78
import java.util.Map;
@@ -13,6 +14,7 @@ void load() {
1314
ExporterHttpServerProperties properties =
1415
load(Map.of("io.prometheus.exporter.httpServer.port", "1"));
1516
assertThat(properties.getPort()).isOne();
17+
assertThat(properties.isPreferUncompressedResponse()).isFalse();
1618

1719
assertThatExceptionOfType(PrometheusPropertiesException.class)
1820
.isThrownBy(() -> load(Map.of("io.prometheus.exporter.httpServer.port", "0")))
@@ -21,7 +23,14 @@ void load() {
2123

2224
@Test
2325
void builder() {
24-
assertThat(ExporterHttpServerProperties.builder().port(1).build().getPort()).isOne();
26+
ExporterHttpServerProperties properties =
27+
ExporterHttpServerProperties.builder().port(1).build();
28+
29+
assertSoftly(
30+
softly -> {
31+
softly.assertThat(properties.getPort()).isOne();
32+
softly.assertThat(properties.isPreferUncompressedResponse()).isFalse();
33+
});
2534
}
2635

2736
private static ExporterHttpServerProperties load(Map<String, String> map) {

prometheus-metrics-exporter-common/src/main/java/io/prometheus/metrics/exporter/common/PrometheusScrapeHandler.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public class PrometheusScrapeHandler {
2929
@Nullable private final Predicate<String> nameFilter;
3030
private final AtomicInteger lastResponseSize = new AtomicInteger(2 << 9); // 0.5 MB
3131
private final List<String> supportedFormats;
32+
private final boolean preferUncompressedResponse;
3233

3334
public PrometheusScrapeHandler() {
3435
this(PrometheusProperties.get(), PrometheusRegistry.defaultRegistry);
@@ -44,6 +45,8 @@ public PrometheusScrapeHandler(PrometheusProperties config) {
4445

4546
public PrometheusScrapeHandler(PrometheusProperties config, PrometheusRegistry registry) {
4647
this.expositionFormats = ExpositionFormats.init(config.getExporterProperties());
48+
this.preferUncompressedResponse =
49+
config.getExporterHttpServerProperties().isPreferUncompressedResponse();
4750
this.registry = registry;
4851
this.nameFilter = makeNameFilter(config.getExporterFilterProperties());
4952
supportedFormats = new ArrayList<>(Arrays.asList("openmetrics", "text"));
@@ -180,6 +183,10 @@ private boolean writeDebugResponse(
180183
}
181184

182185
private boolean shouldUseCompression(PrometheusHttpRequest request) {
186+
if (preferUncompressedResponse) {
187+
return false;
188+
}
189+
183190
Enumeration<String> encodingHeaders = request.getHeaders("Accept-Encoding");
184191
if (encodingHeaders == null) {
185192
return false;

0 commit comments

Comments
 (0)