Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,25 @@
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
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.
Expand All @@ -27,7 +34,12 @@ static ExporterHttpServerProperties load(Map<Object, Object> 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() {
Expand All @@ -37,6 +49,7 @@ public static Builder builder() {
public static class Builder {

@Nullable private Integer port;
private boolean preferUncompressedResponse = false;

private Builder() {}

Expand All @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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")))
Expand All @@ -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<String, String> map) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class PrometheusScrapeHandler {
@Nullable private final Predicate<String> nameFilter;
private final AtomicInteger lastResponseSize = new AtomicInteger(2 << 9); // 0.5 MB
private final List<String> supportedFormats;
private final boolean preferUncompressedResponse;

public PrometheusScrapeHandler() {
this(PrometheusProperties.get(), PrometheusRegistry.defaultRegistry);
Expand All @@ -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"));
Expand Down Expand Up @@ -180,6 +183,10 @@ private boolean writeDebugResponse(
}

private boolean shouldUseCompression(PrometheusHttpRequest request) {
if (preferUncompressedResponse) {
return false;
}

Enumeration<String> encodingHeaders = request.getHeaders("Accept-Encoding");
if (encodingHeaders == null) {
return false;
Expand Down