001package io.prometheus.metrics.config;
002
003import io.prometheus.metrics.annotations.StableApi;
004import javax.annotation.Nullable;
005
006/** Properties starting with io.prometheus.exporter */
007@StableApi
008public class ExporterProperties {
009
010  private static final String INCLUDE_CREATED_TIMESTAMPS = "include_created_timestamps";
011  // milliseconds is the default - we only provide a boolean flag to avoid a breaking change
012  private static final String PROMETHEUS_TIMESTAMPS_IN_MS = "prometheus_timestamps_in_ms";
013  private static final String EXEMPLARS_ON_ALL_METRIC_TYPES = "exemplars_on_all_metric_types";
014  private static final String PREFIX = "io.prometheus.exporter";
015
016  @Nullable private final Boolean includeCreatedTimestamps;
017  @Nullable private final Boolean prometheusTimestampsInMs;
018  @Nullable private final Boolean exemplarsOnAllMetricTypes;
019
020  private ExporterProperties(
021      @Nullable Boolean includeCreatedTimestamps,
022      @Nullable Boolean prometheusTimestampsInMs,
023      @Nullable Boolean exemplarsOnAllMetricTypes) {
024    this.includeCreatedTimestamps = includeCreatedTimestamps;
025    this.prometheusTimestampsInMs = prometheusTimestampsInMs;
026    this.exemplarsOnAllMetricTypes = exemplarsOnAllMetricTypes;
027  }
028
029  /** Include the {@code _created} timestamps in text format? Default is {@code false}. */
030  public boolean getIncludeCreatedTimestamps() {
031    return includeCreatedTimestamps != null && includeCreatedTimestamps;
032  }
033
034  /** Use milliseconds for timestamps in prometheus text format? Default is {@code false}. */
035  public boolean getPrometheusTimestampsInMs() {
036    return prometheusTimestampsInMs != null && prometheusTimestampsInMs;
037  }
038
039  /**
040   * Allow Exemplars on all metric types in OpenMetrics format? Default is {@code false}, which
041   * means Exemplars will only be added for Counters and Histogram buckets.
042   */
043  public boolean getExemplarsOnAllMetricTypes() {
044    return exemplarsOnAllMetricTypes != null && exemplarsOnAllMetricTypes;
045  }
046
047  /**
048   * Note that this will remove entries from {@code propertySource}. This is because we want to know
049   * if there are unused properties remaining after all properties have been loaded.
050   */
051  static ExporterProperties load(PropertySource propertySource)
052      throws PrometheusPropertiesException {
053    Boolean includeCreatedTimestamps =
054        Util.loadBoolean(PREFIX, INCLUDE_CREATED_TIMESTAMPS, propertySource);
055    Boolean timestampsInMs = Util.loadBoolean(PREFIX, PROMETHEUS_TIMESTAMPS_IN_MS, propertySource);
056    Boolean exemplarsOnAllMetricTypes =
057        Util.loadBoolean(PREFIX, EXEMPLARS_ON_ALL_METRIC_TYPES, propertySource);
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}