Skip to content

Commit 358d144

Browse files
MyllyenkoIgor Melnichenko
authored andcommitted
preferUncompressedResponse option was added
Signed-off-by: Igor Melnichenko <[email protected]>
1 parent 4c0f5cf commit 358d144

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

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

Lines changed: 23 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,14 @@ 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,
43+
preferUncompressedResponse != null && preferUncompressedResponse
44+
);
3145
}
3246

3347
public static Builder builder() {
@@ -37,6 +51,7 @@ public static Builder builder() {
3751
public static class Builder {
3852

3953
@Nullable private Integer port;
54+
private boolean preferUncompressedResponse = false;
4055

4156
private Builder() {}
4257

@@ -45,8 +60,13 @@ public Builder port(int port) {
4560
return this;
4661
}
4762

63+
public Builder preferUncompressedResponse(boolean preferUncompressedResponse) {
64+
this.preferUncompressedResponse = preferUncompressedResponse;
65+
return this;
66+
}
67+
4868
public ExporterHttpServerProperties build() {
49-
return new ExporterHttpServerProperties(port);
69+
return new ExporterHttpServerProperties(port, preferUncompressedResponse);
5070
}
5171
}
5272
}

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

Lines changed: 9 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,13 @@ 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(softly -> {
30+
softly.assertThat(properties.getPort()).isOne();
31+
softly.assertThat(properties.isPreferUncompressedResponse()).isFalse();
32+
});
2533
}
2634

2735
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)