001package io.prometheus.metrics.config;
002
003import io.prometheus.metrics.annotations.StableApi;
004import javax.annotation.Nullable;
005
006/** Properties starting with io.prometheus.exporter.http_server */
007@StableApi
008public class ExporterHttpServerProperties {
009
010  private static final String PORT = "port";
011  private static final String PREFER_UNCOMPRESSED_RESPONSE = "prefer_uncompressed_response";
012  private static final String PREFIX = "io.prometheus.exporter.http_server";
013  @Nullable private final Integer port;
014  private final boolean preferUncompressedResponse;
015
016  private ExporterHttpServerProperties(@Nullable Integer port, boolean preferUncompressedResponse) {
017    this.port = port;
018    this.preferUncompressedResponse = preferUncompressedResponse;
019  }
020
021  @Nullable
022  public Integer getPort() {
023    return port;
024  }
025
026  public boolean isPreferUncompressedResponse() {
027    return preferUncompressedResponse;
028  }
029
030  /**
031   * Note that this will remove entries from {@code propertySource}. This is because we want to know
032   * if there are unused properties remaining after all properties have been loaded.
033   */
034  static ExporterHttpServerProperties load(PropertySource propertySource)
035      throws PrometheusPropertiesException {
036    Integer port = Util.loadInteger(PREFIX, PORT, propertySource);
037    Util.assertValue(port, t -> t > 0, "Expecting value > 0.", PREFIX, PORT);
038
039    Boolean preferUncompressedResponse =
040        Util.loadBoolean(PREFIX, PREFER_UNCOMPRESSED_RESPONSE, propertySource);
041
042    return new ExporterHttpServerProperties(
043        port, preferUncompressedResponse != null && preferUncompressedResponse);
044  }
045
046  public static Builder builder() {
047    return new Builder();
048  }
049
050  public static class Builder {
051
052    @Nullable private Integer port;
053    private boolean preferUncompressedResponse = false;
054
055    private Builder() {}
056
057    public Builder port(int port) {
058      this.port = port;
059      return this;
060    }
061
062    public Builder preferUncompressedResponse(boolean preferUncompressedResponse) {
063      this.preferUncompressedResponse = preferUncompressedResponse;
064      return this;
065    }
066
067    public ExporterHttpServerProperties build() {
068      return new ExporterHttpServerProperties(port, preferUncompressedResponse);
069    }
070  }
071}