001package io.prometheus.metrics.config;
002
003import java.util.Map;
004import javax.annotation.Nullable;
005
006/** Properties starting with io.prometheus.exporter */
007public class ExporterProperties {
008
009  private static final String INCLUDE_CREATED_TIMESTAMPS = "includeCreatedTimestamps";
010  // milliseconds is the default - we only provide a boolean flag to avoid a breaking change
011  private static final String PROMETHEUS_TIMESTAMPS_IN_MS = "prometheusTimestampsInMs";
012  private static final String EXEMPLARS_ON_ALL_METRIC_TYPES = "exemplarsOnAllMetricTypes";
013  private static final String PREFIX = "io.prometheus.exporter";
014
015  @Nullable private final Boolean includeCreatedTimestamps;
016  @Nullable private final Boolean prometheusTimestampsInMs;
017  @Nullable private final Boolean exemplarsOnAllMetricTypes;
018
019  private ExporterProperties(
020      @Nullable Boolean includeCreatedTimestamps,
021      @Nullable Boolean prometheusTimestampsInMs,
022      @Nullable Boolean exemplarsOnAllMetricTypes) {
023    this.includeCreatedTimestamps = includeCreatedTimestamps;
024    this.prometheusTimestampsInMs = prometheusTimestampsInMs;
025    this.exemplarsOnAllMetricTypes = exemplarsOnAllMetricTypes;
026  }
027
028  /** Include the {@code _created} timestamps in text format? Default is {@code false}. */
029  public boolean getIncludeCreatedTimestamps() {
030    return includeCreatedTimestamps != null && includeCreatedTimestamps;
031  }
032
033  /** Use milliseconds for timestamps in prometheus text format? Default is {@code false}. */
034  public boolean getPrometheusTimestampsInMs() {
035    return prometheusTimestampsInMs != null && prometheusTimestampsInMs;
036  }
037
038  /**
039   * Allow Exemplars on all metric types in OpenMetrics format? Default is {@code false}, which
040   * means Exemplars will only be added for Counters and Histogram buckets.
041   */
042  public boolean getExemplarsOnAllMetricTypes() {
043    return exemplarsOnAllMetricTypes != null && exemplarsOnAllMetricTypes;
044  }
045
046  /**
047   * Note that this will remove entries from {@code properties}. This is because we want to know if
048   * there are unused properties remaining after all properties have been loaded.
049   */
050  static ExporterProperties load(Map<Object, Object> properties)
051      throws PrometheusPropertiesException {
052    Boolean includeCreatedTimestamps =
053        Util.loadBoolean(PREFIX + "." + INCLUDE_CREATED_TIMESTAMPS, properties);
054    Boolean timestampsInMs =
055        Util.loadBoolean(PREFIX + "." + PROMETHEUS_TIMESTAMPS_IN_MS, properties);
056    Boolean exemplarsOnAllMetricTypes =
057        Util.loadBoolean(PREFIX + "." + EXEMPLARS_ON_ALL_METRIC_TYPES, properties);
058    return new ExporterProperties(
059        includeCreatedTimestamps, timestampsInMs, exemplarsOnAllMetricTypes);
060  }
061
062  public static Builder builder() {
063    return new Builder();
064  }
065
066  public static class Builder {
067
068    @Nullable private Boolean includeCreatedTimestamps;
069    @Nullable private Boolean exemplarsOnAllMetricTypes;
070    boolean prometheusTimestampsInMs;
071
072    private Builder() {}
073
074    /** See {@link #getIncludeCreatedTimestamps()} */
075    public Builder includeCreatedTimestamps(boolean includeCreatedTimestamps) {
076      this.includeCreatedTimestamps = includeCreatedTimestamps;
077      return this;
078    }
079
080    /** See {@link #getExemplarsOnAllMetricTypes()}. */
081    public Builder exemplarsOnAllMetricTypes(boolean exemplarsOnAllMetricTypes) {
082      this.exemplarsOnAllMetricTypes = exemplarsOnAllMetricTypes;
083      return this;
084    }
085
086    /** See {@link #getPrometheusTimestampsInMs()}. */
087    public Builder prometheusTimestampsInMs(boolean prometheusTimestampsInMs) {
088      this.prometheusTimestampsInMs = prometheusTimestampsInMs;
089      return this;
090    }
091
092    public ExporterProperties build() {
093      return new ExporterProperties(
094          includeCreatedTimestamps, prometheusTimestampsInMs, exemplarsOnAllMetricTypes);
095    }
096  }
097}