001package io.prometheus.metrics.config;
002
003import javax.annotation.Nullable;
004
005/**
006 * Properties starting with io.prometheus.openmetrics2. These properties are experimental and
007 * subject to change.
008 */
009public class OpenMetrics2Properties {
010
011  private static final String PREFIX = "io.prometheus.openmetrics2";
012  private static final String ENABLED = "enabled";
013  private static final String CONTENT_NEGOTIATION = "content_negotiation";
014  private static final String COMPOSITE_VALUES = "composite_values";
015  private static final String EXEMPLAR_COMPLIANCE = "exemplar_compliance";
016  private static final String NATIVE_HISTOGRAMS = "native_histograms";
017
018  @Nullable private final Boolean enabled;
019  @Nullable private final Boolean contentNegotiation;
020  @Nullable private final Boolean compositeValues;
021  @Nullable private final Boolean exemplarCompliance;
022  @Nullable private final Boolean nativeHistograms;
023
024  private OpenMetrics2Properties(
025      @Nullable Boolean enabled,
026      @Nullable Boolean contentNegotiation,
027      @Nullable Boolean compositeValues,
028      @Nullable Boolean exemplarCompliance,
029      @Nullable Boolean nativeHistograms) {
030    this.enabled = enabled;
031    this.contentNegotiation = contentNegotiation;
032    this.compositeValues = compositeValues;
033    this.exemplarCompliance = exemplarCompliance;
034    this.nativeHistograms = nativeHistograms;
035  }
036
037  /**
038   * Enable the OpenMetrics 2.0 text format writer. When {@code true}, the OM2 writer is used
039   * instead of OM1 for OpenMetrics responses. Default is {@code false}.
040   */
041  public boolean getEnabled() {
042    return enabled != null && enabled;
043  }
044
045  /** Gate OM2 features behind content negotiation. Default is {@code false}. */
046  public boolean getContentNegotiation() {
047    return contentNegotiation != null && contentNegotiation;
048  }
049
050  /** Single-line histogram/summary with {@code st@}. Default is {@code false}. */
051  public boolean getCompositeValues() {
052    return compositeValues != null && compositeValues;
053  }
054
055  /** Mandatory timestamps, no 128-char limit for exemplars. Default is {@code false}. */
056  public boolean getExemplarCompliance() {
057    return exemplarCompliance != null && exemplarCompliance;
058  }
059
060  /** Exponential buckets support for native histograms. Default is {@code false}. */
061  public boolean getNativeHistograms() {
062    return nativeHistograms != null && nativeHistograms;
063  }
064
065  /**
066   * Note that this will remove entries from {@code propertySource}. This is because we want to know
067   * if there are unused properties remaining after all properties have been loaded.
068   */
069  static OpenMetrics2Properties load(PropertySource propertySource)
070      throws PrometheusPropertiesException {
071    Boolean enabled = Util.loadBoolean(PREFIX, ENABLED, propertySource);
072    Boolean contentNegotiation = Util.loadBoolean(PREFIX, CONTENT_NEGOTIATION, propertySource);
073    Boolean compositeValues = Util.loadBoolean(PREFIX, COMPOSITE_VALUES, propertySource);
074    Boolean exemplarCompliance = Util.loadBoolean(PREFIX, EXEMPLAR_COMPLIANCE, propertySource);
075    Boolean nativeHistograms = Util.loadBoolean(PREFIX, NATIVE_HISTOGRAMS, propertySource);
076    return new OpenMetrics2Properties(
077        enabled, contentNegotiation, compositeValues, exemplarCompliance, nativeHistograms);
078  }
079
080  public static Builder builder() {
081    return new Builder();
082  }
083
084  public static class Builder {
085
086    @Nullable private Boolean enabled;
087    @Nullable private Boolean contentNegotiation;
088    @Nullable private Boolean compositeValues;
089    @Nullable private Boolean exemplarCompliance;
090    @Nullable private Boolean nativeHistograms;
091
092    private Builder() {}
093
094    /** See {@link #getEnabled()} */
095    public Builder enabled(boolean enabled) {
096      this.enabled = enabled;
097      return this;
098    }
099
100    /** See {@link #getContentNegotiation()} */
101    public Builder contentNegotiation(boolean contentNegotiation) {
102      this.contentNegotiation = contentNegotiation;
103      return this;
104    }
105
106    /** See {@link #getCompositeValues()} */
107    public Builder compositeValues(boolean compositeValues) {
108      this.compositeValues = compositeValues;
109      return this;
110    }
111
112    /** See {@link #getExemplarCompliance()} */
113    public Builder exemplarCompliance(boolean exemplarCompliance) {
114      this.exemplarCompliance = exemplarCompliance;
115      return this;
116    }
117
118    /** See {@link #getNativeHistograms()} */
119    public Builder nativeHistograms(boolean nativeHistograms) {
120      this.nativeHistograms = nativeHistograms;
121      return this;
122    }
123
124    /** Enable all OpenMetrics 2.0 features */
125    public Builder enableAll() {
126      this.enabled = true;
127      this.contentNegotiation = true;
128      this.compositeValues = true;
129      this.exemplarCompliance = true;
130      this.nativeHistograms = true;
131      return this;
132    }
133
134    public OpenMetrics2Properties build() {
135      return new OpenMetrics2Properties(
136          enabled, contentNegotiation, compositeValues, exemplarCompliance, nativeHistograms);
137    }
138  }
139}