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