All Implemented Interfaces:
DataPoint, StateSetDataPoint, io.prometheus.metrics.model.registry.Collector

public class StateSet extends MetricWithFixedMetadata implements StateSetDataPoint
StateSet metric. Example:

 public enum Feature {

     FEATURE_1("feature1"),
     FEATURE_2("feature2");

     private final String name;

     Feature(String name) {
         this.name = name;
     }

     // Override
     public String toString() {
         return name;
     }
 }

 public static void main(String[] args) {

     StateSet stateSet = StateSet.builder()
             .name("feature_flags")
             .help("Feature flags")
             .labelNames("env")
             .states(Feature.class)
             .register();

     stateSet.labelValues("dev").setFalse(FEATURE_1);
     stateSet.labelValues("dev").setTrue(FEATURE_2);
 }
 
The example above shows how to use a StateSet with an enum. You don't have to use enum, you can use regular strings as well.
  • Method Details

    • collect

      public io.prometheus.metrics.model.snapshots.StateSetSnapshot collect()
      Specified by:
      collect in interface io.prometheus.metrics.model.registry.Collector
    • collect

      protected io.prometheus.metrics.model.snapshots.StateSetSnapshot collect(List<io.prometheus.metrics.model.snapshots.Labels> labels, List<io.prometheus.metrics.core.metrics.StateSet.DataPoint> metricDataList)
      labels and metricData have the same size. labels.get(i) are the labels for metricData.get(i).
    • setTrue

      public void setTrue(String state)
      Description copied from interface: StateSetDataPoint
      state must be one of the states from when the StateSet was created with StateSet.Builder.states(String...).
      Specified by:
      setTrue in interface StateSetDataPoint
    • setFalse

      public void setFalse(String state)
      Description copied from interface: StateSetDataPoint
      state must be one of the states from when the StateSet was created with StateSet.Builder.states(String...).
      Specified by:
      setFalse in interface StateSetDataPoint
    • newDataPoint

      protected io.prometheus.metrics.core.metrics.StateSet.DataPoint newDataPoint()
    • isExemplarsEnabled

      protected boolean isExemplarsEnabled()
    • builder

      public static StateSet.Builder builder()
    • builder

      public static StateSet.Builder builder(io.prometheus.metrics.config.PrometheusProperties config)
    • initLabelValues

      public void initLabelValues(String... labelValues)
      Initialize label values.

      Example: Imagine you have a counter for payments as follows

       payment_transactions_total{payment_type="credit card"} 7.0
       payment_transactions_total{payment_type="paypal"} 3.0
       
      Now, the data points for the payment_type label values get initialized when they are first used, i.e. the first time you call
      
       counter.labelValues("paypal").inc();
       
      the data point with label payment_type="paypal" will go from non-existent to having value 1.0.

      In some cases this is confusing, and you want to have data points initialized on application start with an initial value of 0.0:

       payment_transactions_total{payment_type="credit card"} 0.0
       payment_transactions_total{payment_type="paypal"} 0.0
       
      initLabelValues(...) can be used to initialize label value, so that the data points show up in the exposition format with an initial value of zero.
    • labelValues

      public StateSetDataPoint labelValues(String... labelValues)
    • remove

      public void remove(String... labelValues)
      Remove the data point with the given label values. See https://prometheus.io/docs/instrumenting/writing_clientlibs/#labels.
    • removeIf

      public void removeIf(Function<List<String>,Boolean> f)
      Remove the data points when the given function.
    • clear

      public void clear()
      Reset the metric (remove all data points).
    • getNoLabels

      protected io.prometheus.metrics.core.metrics.StateSet.DataPoint getNoLabels()
    • getMetricProperties

      protected io.prometheus.metrics.config.MetricsProperties[] getMetricProperties(io.prometheus.metrics.core.metrics.StatefulMetric.Builder<?,?> builder, io.prometheus.metrics.config.PrometheusProperties prometheusProperties)
    • getConfigProperty

      protected <P> P getConfigProperty(io.prometheus.metrics.config.MetricsProperties[] properties, Function<io.prometheus.metrics.config.MetricsProperties,P> getter)