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