001package io.prometheus.metrics.config;
002
003import javax.annotation.Nullable;
004
005/** Properties starting with io.prometheus.exporter */
006public class ExporterProperties {
007
008  private static final String INCLUDE_CREATED_TIMESTAMPS = "include_created_timestamps";
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 = "prometheus_timestamps_in_ms";
011  private static final String EXEMPLARS_ON_ALL_METRIC_TYPES = "exemplars_on_all_metric_types";
012  private static final String PREFIX = "io.prometheus.exporter";
013
014  @Nullable private final Boolean includeCreatedTimestamps;
015  @Nullable private final Boolean prometheusTimestampsInMs;
016  @Nullable private final Boolean exemplarsOnAllMetricTypes;
017
018  private ExporterProperties(
019      @Nullable Boolean includeCreatedTimestamps,
020      @Nullable Boolean prometheusTimestampsInMs,
021      @Nullable 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 propertySource}. This is because we want to know
047   * if there are unused properties remaining after all properties have been loaded.
048   */
049  static ExporterProperties load(PropertySource propertySource)
050      throws PrometheusPropertiesException {
051    Boolean includeCreatedTimestamps =
052        Util.loadBoolean(PREFIX, INCLUDE_CREATED_TIMESTAMPS, propertySource);
053    Boolean timestampsInMs = Util.loadBoolean(PREFIX, PROMETHEUS_TIMESTAMPS_IN_MS, propertySource);
054    Boolean exemplarsOnAllMetricTypes =
055        Util.loadBoolean(PREFIX, EXEMPLARS_ON_ALL_METRIC_TYPES, propertySource);
056    return new ExporterProperties(
057        includeCreatedTimestamps, timestampsInMs, exemplarsOnAllMetricTypes);
058  }
059
060  public static Builder builder() {
061    return new Builder();
062  }
063
064  public static class Builder {
065
066    @Nullable private Boolean includeCreatedTimestamps;
067    @Nullable private Boolean exemplarsOnAllMetricTypes;
068    boolean prometheusTimestampsInMs;
069
070    private Builder() {}
071
072    /** See {@link #getIncludeCreatedTimestamps()} */
073    public Builder includeCreatedTimestamps(boolean includeCreatedTimestamps) {
074      this.includeCreatedTimestamps = includeCreatedTimestamps;
075      return this;
076    }
077
078    /** See {@link #getExemplarsOnAllMetricTypes()}. */
079    public Builder exemplarsOnAllMetricTypes(boolean exemplarsOnAllMetricTypes) {
080      this.exemplarsOnAllMetricTypes = exemplarsOnAllMetricTypes;
081      return this;
082    }
083
084    /** See {@link #getPrometheusTimestampsInMs()}. */
085    public Builder prometheusTimestampsInMs(boolean prometheusTimestampsInMs) {
086      this.prometheusTimestampsInMs = prometheusTimestampsInMs;
087      return this;
088    }
089
090    public ExporterProperties build() {
091      return new ExporterProperties(
092          includeCreatedTimestamps, prometheusTimestampsInMs, exemplarsOnAllMetricTypes);
093    }
094  }
095}