001package io.prometheus.metrics.config;
002
003import java.util.Map;
004import javax.annotation.Nullable;
005
006public class ExporterPushgatewayProperties {
007
008  private static final String ADDRESS = "address";
009  private static final String JOB = "job";
010  private static final String SCHEME = "scheme";
011  private static final String ESCAPING_SCHEME = "escapingScheme";
012  private static final String PREFIX = "io.prometheus.exporter.pushgateway";
013  @Nullable private final String scheme;
014  @Nullable private final String address;
015  @Nullable private final String job;
016  @Nullable private final EscapingScheme escapingScheme;
017
018  private ExporterPushgatewayProperties(
019      @Nullable String address,
020      @Nullable String job,
021      @Nullable String scheme,
022      @Nullable EscapingScheme escapingScheme) {
023    this.address = address;
024    this.job = job;
025    this.scheme = scheme;
026    this.escapingScheme = escapingScheme;
027  }
028
029  /** Address of the Pushgateway in the form {@code host:port}. Default is {@code localhost:9091} */
030  @Nullable
031  public String getAddress() {
032    return address;
033  }
034
035  /**
036   * {@code job} label for metrics being pushed. Default is the name of the JAR file that is
037   * running.
038   */
039  @Nullable
040  public String getJob() {
041    return job;
042  }
043
044  /**
045   * Scheme to be used when pushing metrics to the pushgateway. Must be "http" or "https". Default
046   * is "http".
047   */
048  @Nullable
049  public String getScheme() {
050    return scheme;
051  }
052
053  /** Escaping scheme to be used when pushing metric data to the pushgateway. */
054  @Nullable
055  public EscapingScheme getEscapingScheme() {
056    return escapingScheme;
057  }
058
059  /**
060   * Note that this will remove entries from {@code properties}. This is because we want to know if
061   * there are unused properties remaining after all properties have been loaded.
062   */
063  static ExporterPushgatewayProperties load(Map<Object, Object> properties)
064      throws PrometheusPropertiesException {
065    String address = Util.loadString(PREFIX + "." + ADDRESS, properties);
066    String job = Util.loadString(PREFIX + "." + JOB, properties);
067    String scheme = Util.loadString(PREFIX + "." + SCHEME, properties);
068    String escapingScheme = Util.loadString(PREFIX + "." + ESCAPING_SCHEME, properties);
069
070    if (scheme != null) {
071      if (!scheme.equals("http") && !scheme.equals("https")) {
072        throw new PrometheusPropertiesException(
073            String.format(
074                "%s.%s: Illegal value. Expecting 'http' or 'https'. Found: %s",
075                PREFIX, SCHEME, scheme));
076      }
077    }
078
079    return new ExporterPushgatewayProperties(
080        address, job, scheme, parseEscapingScheme(escapingScheme));
081  }
082
083  private static @Nullable EscapingScheme parseEscapingScheme(@Nullable String scheme) {
084    if (scheme == null) {
085      return null;
086    }
087    switch (scheme) {
088      case "allow-utf-8":
089        return EscapingScheme.ALLOW_UTF8;
090      case "values":
091        return EscapingScheme.VALUE_ENCODING_ESCAPING;
092      case "underscores":
093        return EscapingScheme.UNDERSCORE_ESCAPING;
094      case "dots":
095        return EscapingScheme.DOTS_ESCAPING;
096      default:
097        throw new PrometheusPropertiesException(
098            String.format(
099                "%s.%s: Illegal value. Expecting 'allow-utf-8', 'values', 'underscores', "
100                    + "or 'dots'. Found: %s",
101                PREFIX, ESCAPING_SCHEME, scheme));
102    }
103  }
104
105  public static Builder builder() {
106    return new Builder();
107  }
108
109  public static class Builder {
110    @Nullable private String address;
111    @Nullable private String job;
112    @Nullable private String scheme;
113    @Nullable private EscapingScheme escapingScheme;
114
115    private Builder() {}
116
117    public Builder address(String address) {
118      this.address = address;
119      return this;
120    }
121
122    public Builder job(String job) {
123      this.job = job;
124      return this;
125    }
126
127    public Builder scheme(String scheme) {
128      this.scheme = scheme;
129      return this;
130    }
131
132    public Builder escapingScheme(EscapingScheme escapingScheme) {
133      this.escapingScheme = escapingScheme;
134      return this;
135    }
136
137    public ExporterPushgatewayProperties build() {
138      return new ExporterPushgatewayProperties(address, job, scheme, escapingScheme);
139    }
140  }
141}