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