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

public class Counter extends MetricWithFixedMetadata implements CounterDataPoint
Counter metric.

Example usage:


 Counter requestCount = Counter.builder()
     .name("requests_total")
     .help("Total number of requests")
     .labelNames("path", "status")
     .register();
 requestCount.labelValues("/hello-world", "200").inc();
 requestCount.labelValues("/hello-world", "500").inc();
 
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static class 
     
  • Field Summary

    Fields inherited from class io.prometheus.metrics.core.metrics.MetricWithFixedMetadata

    labelNames

    Fields inherited from class io.prometheus.metrics.core.metrics.Metric

    constLabels
  • Method Summary

    Modifier and Type
    Method
    Description
     
    builder(io.prometheus.metrics.config.PrometheusProperties config)
     
    void
    Reset the metric (remove all data points).
    io.prometheus.metrics.model.snapshots.CounterSnapshot
     
    protected io.prometheus.metrics.model.snapshots.CounterSnapshot
    collect(List<io.prometheus.metrics.model.snapshots.Labels> labels, List<io.prometheus.metrics.core.metrics.Counter.DataPoint> metricData)
    labels and metricData have the same size. labels.get(i) are the labels for metricData.get(i).
    double
    get()
    Get the current value.
    protected <P> P
    getConfigProperty(io.prometheus.metrics.config.MetricsProperties[] properties, Function<io.prometheus.metrics.config.MetricsProperties,P> getter)
     
    long
    Get the current value as a long.
    protected io.prometheus.metrics.config.MetricsProperties[]
    getMetricProperties(io.prometheus.metrics.core.metrics.StatefulMetric.Builder<?,?> builder, io.prometheus.metrics.config.PrometheusProperties prometheusProperties)
     
    protected io.prometheus.metrics.core.metrics.Counter.DataPoint
     
    void
    inc(double amount)
    Add amount.
    void
    inc(long amount)
    Add amount.
    void
    incWithExemplar(double amount, io.prometheus.metrics.model.snapshots.Labels labels)
    Add amount, and create a custom exemplar with the given labels.
    void
    incWithExemplar(long amount, io.prometheus.metrics.model.snapshots.Labels labels)
    Add amount, and create a custom exemplar with the given labels.
    void
    initLabelValues(String... labelValues)
    Initialize label values.
    protected boolean
     
    labelValues(String... labelValues)
     
    protected io.prometheus.metrics.core.metrics.Counter.DataPoint
     
    void
    remove(String... labelValues)
    Remove the data point with the given label values.
    void
    Remove the data points when the given function.

    Methods inherited from class io.prometheus.metrics.core.metrics.MetricWithFixedMetadata

    getMetadata, getPrometheusName

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface io.prometheus.metrics.model.registry.Collector

    collect, collect, collect

    Methods inherited from interface io.prometheus.metrics.core.datapoints.CounterDataPoint

    inc, incWithExemplar
  • Method Details

    • inc

      public void inc(long amount)
      Description copied from interface: CounterDataPoint
      Add amount. Throws an IllegalArgumentException if amount is negative.
      Specified by:
      inc in interface CounterDataPoint
    • inc

      public void inc(double amount)
      Description copied from interface: CounterDataPoint
      Add amount. Throws an IllegalArgumentException if amount is negative.
      Specified by:
      inc in interface CounterDataPoint
    • incWithExemplar

      public void incWithExemplar(long amount, io.prometheus.metrics.model.snapshots.Labels labels)
      Description copied from interface: CounterDataPoint
      Add amount, and create a custom exemplar with the given labels. Throws an IllegalArgumentException if amount is negative.
      Specified by:
      incWithExemplar in interface CounterDataPoint
    • incWithExemplar

      public void incWithExemplar(double amount, io.prometheus.metrics.model.snapshots.Labels labels)
      Description copied from interface: CounterDataPoint
      Add amount, and create a custom exemplar with the given labels. Throws an IllegalArgumentException if amount is negative.
      Specified by:
      incWithExemplar in interface CounterDataPoint
    • get

      public double get()
      Description copied from interface: CounterDataPoint
      Get the current value.
      Specified by:
      get in interface CounterDataPoint
    • getLongValue

      public long getLongValue()
      Description copied from interface: CounterDataPoint
      Get the current value as a long. Decimal places will be discarded.
      Specified by:
      getLongValue in interface CounterDataPoint
    • collect

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

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

      protected boolean isExemplarsEnabled()
    • newDataPoint

      protected io.prometheus.metrics.core.metrics.Counter.DataPoint newDataPoint()
    • builder

      public static Counter.Builder builder()
    • builder

      public static Counter.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 CounterDataPoint 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.Counter.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)