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