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  private static final String SUFFIXES = "suffixes";
020
021  @Nullable private final Boolean enabled;
022  @Nullable private final Boolean contentNegotiation;
023  @Nullable private final Boolean compositeValues;
024  @Nullable private final Boolean exemplarCompliance;
025  @Nullable private final Boolean nativeHistograms;
026  @Nullable private final Boolean suffixes;
027
028  private OpenMetrics2Properties(
029      @Nullable Boolean enabled,
030      @Nullable Boolean contentNegotiation,
031      @Nullable Boolean compositeValues,
032      @Nullable Boolean exemplarCompliance,
033      @Nullable Boolean nativeHistograms,
034      @Nullable Boolean suffixes) {
035    this.enabled = enabled;
036    this.contentNegotiation = contentNegotiation;
037    this.compositeValues = compositeValues;
038    this.exemplarCompliance = exemplarCompliance;
039    this.nativeHistograms = nativeHistograms;
040    this.suffixes = suffixes;
041  }
042
043  /**
044   * Enable the OpenMetrics 2.0 text format writer. When {@code true}, the OM2 writer is used
045   * instead of OM1 for OpenMetrics responses. Default is {@code false}.
046   */
047  public boolean getEnabled() {
048    return enabled != null && enabled;
049  }
050
051  /** Gate OM2 features behind content negotiation. Default is {@code false}. */
052  public boolean getContentNegotiation() {
053    return contentNegotiation != null && contentNegotiation;
054  }
055
056  /** Single-line histogram/summary with {@code st@}. Default is {@code false}. */
057  public boolean getCompositeValues() {
058    return compositeValues != null && compositeValues;
059  }
060
061  /** Mandatory timestamps, no 128-char limit for exemplars. Default is {@code false}. */
062  public boolean getExemplarCompliance() {
063    return exemplarCompliance != null && exemplarCompliance;
064  }
065
066  /** Exponential buckets support for native histograms. Default is {@code false}. */
067  public boolean getNativeHistograms() {
068    return nativeHistograms != null && nativeHistograms;
069  }
070
071  /** Append unit and type suffixes to metric names. Default is {@code false}. */
072  public boolean getSuffixes() {
073    return suffixes != null && suffixes;
074  }
075
076  /**
077   * Note that this will remove entries from {@code propertySource}. This is because we want to know
078   * if there are unused properties remaining after all properties have been loaded.
079   */
080  static OpenMetrics2Properties load(PropertySource propertySource)
081      throws PrometheusPropertiesException {
082    Boolean enabled = Util.loadBoolean(PREFIX, ENABLED, propertySource);
083    Boolean contentNegotiation = Util.loadBoolean(PREFIX, CONTENT_NEGOTIATION, propertySource);
084    Boolean compositeValues = Util.loadBoolean(PREFIX, COMPOSITE_VALUES, propertySource);
085    Boolean exemplarCompliance = Util.loadBoolean(PREFIX, EXEMPLAR_COMPLIANCE, propertySource);
086    Boolean nativeHistograms = Util.loadBoolean(PREFIX, NATIVE_HISTOGRAMS, propertySource);
087    Boolean suffixes = Util.loadBoolean(PREFIX, SUFFIXES, propertySource);
088    return new OpenMetrics2Properties(
089        enabled,
090        contentNegotiation,
091        compositeValues,
092        exemplarCompliance,
093        nativeHistograms,
094        suffixes);
095  }
096
097  public static Builder builder() {
098    return new Builder();
099  }
100
101  public static class Builder {
102
103    @Nullable private Boolean enabled;
104    @Nullable private Boolean contentNegotiation;
105    @Nullable private Boolean compositeValues;
106    @Nullable private Boolean exemplarCompliance;
107    @Nullable private Boolean nativeHistograms;
108    @Nullable private Boolean suffixes;
109
110    private Builder() {}
111
112    /** See {@link #getEnabled()} */
113    public Builder enabled(boolean enabled) {
114      this.enabled = enabled;
115      return this;
116    }
117
118    /** See {@link #getContentNegotiation()} */
119    public Builder contentNegotiation(boolean contentNegotiation) {
120      this.contentNegotiation = contentNegotiation;
121      return this;
122    }
123
124    /** See {@link #getCompositeValues()} */
125    public Builder compositeValues(boolean compositeValues) {
126      this.compositeValues = compositeValues;
127      return this;
128    }
129
130    /** See {@link #getExemplarCompliance()} */
131    public Builder exemplarCompliance(boolean exemplarCompliance) {
132      this.exemplarCompliance = exemplarCompliance;
133      return this;
134    }
135
136    /** See {@link #getNativeHistograms()} */
137    public Builder nativeHistograms(boolean nativeHistograms) {
138      this.nativeHistograms = nativeHistograms;
139      return this;
140    }
141
142    /** See {@link #getSuffixes()} */
143    public Builder suffixes(boolean suffixes) {
144      this.suffixes = suffixes;
145      return this;
146    }
147
148    /** Enable all OpenMetrics 2.0 features */
149    public Builder enableAll() {
150      this.enabled = true;
151      this.contentNegotiation = true;
152      this.compositeValues = true;
153      this.exemplarCompliance = true;
154      this.nativeHistograms = true;
155      return this;
156    }
157
158    public OpenMetrics2Properties build() {
159      return new OpenMetrics2Properties(
160          enabled,
161          contentNegotiation,
162          compositeValues,
163          exemplarCompliance,
164          nativeHistograms,
165          suffixes);
166    }
167  }
168}