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