Class Gauge

All Implemented Interfaces:
DataPoint, GaugeDataPoint, TimerApi, Collector

public class Gauge extends MetricWithFixedMetadata implements GaugeDataPoint
Gauge metric.

Example usage:


 Gauge currentActiveUsers = Gauge.builder()
     .name("current_active_users")
     .help("Number of users that are currently active")
     .labelNames("region")
     .register();

 public void login(String region) {
     currentActiveUsers.labelValues(region).inc();
     // perform login
 }

 public void logout(String region) {
     currentActiveUsers.labelValues(region).dec();
     // perform logout
 }
 
  • Method Details

    • inc

      public void inc(double amount)
      Add amount.
      Specified by:
      inc in interface GaugeDataPoint
    • get

      public double get()
      Get the current value.
      Specified by:
      get in interface GaugeDataPoint
    • incWithExemplar

      public void incWithExemplar(double amount, Labels labels)
      Add amount, and create a custom exemplar with the given labels.
      Specified by:
      incWithExemplar in interface GaugeDataPoint
    • set

      public void set(double value)
      Set the gauge to value.
      Specified by:
      set in interface GaugeDataPoint
    • setWithExemplar

      public void setWithExemplar(double value, Labels labels)
      Set the gauge to value, and create a custom exemplar with the given labels.
      Specified by:
      setWithExemplar in interface GaugeDataPoint
    • collect

      Called when the Prometheus server scrapes metrics.
      Specified by:
      collect in interface Collector
    • builder

      public static Gauge.Builder builder()
    • builder

      public static Gauge.Builder builder(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 GaugeDataPoint 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.
    • clear

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