Package io.prometheus.client
Class Counter
- java.lang.Object
-
- io.prometheus.client.Collector
-
- io.prometheus.client.SimpleCollector<Counter.Child>
-
- io.prometheus.client.Counter
-
- All Implemented Interfaces:
Collector.Describable
public class Counter extends SimpleCollector<Counter.Child> implements Collector.Describable
Counter metric, to track counts of events or running totals.Example of Counters include:
- Number of requests processed
- Number of items that were inserted into a queue
- Total amount of data a system has processed
Gauge
instead. Use therate()
function in Prometheus to calculate the rate of increase of a Counter. By convention, the names of Counters are suffixed by_total
.An example Counter:
class YourClass { static final Counter requests = Counter.build() .name("requests_total").help("Total requests.").register(); static final Counter failedRequests = Counter.build() .name("requests_failed_total").help("Total failed requests.").register(); void processRequest() { requests.inc(); try { // Your code here. } catch (Exception e) { failedRequests.inc(); throw e; } } }
You can also use labels to track different types of metric:
class YourClass { static final Counter requests = Counter.build() .name("requests_total").help("Total requests.") .labelNames("method").register(); void processGetRequest() { requests.labels("get").inc(); // Your code here. } void processPostRequest() { requests.labels("post").inc(); // Your code here. } }
_total
on the metric name, it will be removed. When exposing the time series for counter value, a_total
suffix will be added. This is for compatibility between OpenMetrics and the Prometheus text format, as OpenMetrics requires the_total
suffix.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
Counter.Builder
static class
Counter.Child
The value of a single Counter.-
Nested classes/interfaces inherited from class io.prometheus.client.Collector
Collector.Describable, Collector.MetricFamilySamples, Collector.Type
-
-
Field Summary
-
Fields inherited from class io.prometheus.client.SimpleCollector
children, fullname, help, labelNames, noLabelsChild, unit
-
Fields inherited from class io.prometheus.client.Collector
MILLISECONDS_PER_SECOND, NANOSECONDS_PER_SECOND
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static Counter.Builder
build()
Return a Builder to allow configuration of a new Counter.static Counter.Builder
build(String name, String help)
Return a Builder to allow configuration of a new Counter.List<Collector.MetricFamilySamples>
collect()
Return all metrics of this Collector.List<Collector.MetricFamilySamples>
describe()
Provide a list of metric families this Collector is expected to return.double
get()
Get the value of the counter.void
inc()
Increment the counter with no labels by 1.void
inc(double amt)
Increment the counter with no labels by the given amount.void
incWithExemplar(double amt, String... exemplarLabels)
LikeCounter.Child.incWithExemplar(double, String...)
, but for the counter without labels.void
incWithExemplar(double amt, Map<String,String> exemplarLabels)
LikeCounter.Child.incWithExemplar(double, Map)
, but for the counter without labels.void
incWithExemplar(String... exemplarLabels)
LikeCounter.Child.incWithExemplar(String...)
, but for the counter without labels.void
incWithExemplar(Map<String,String> exemplarLabels)
LikeCounter.Child.incWithExemplar(Map)
, but for the counter without labels.protected Counter.Child
newChild()
Return a new child, workaround for Java generics limitations.-
Methods inherited from class io.prometheus.client.SimpleCollector
clear, familySamplesList, initializeNoLabelsChild, labels, remove, setChild
-
Methods inherited from class io.prometheus.client.Collector
checkMetricLabelName, checkMetricName, collect, doubleToGoString, register, register, sanitizeMetricName
-
-
-
-
Method Detail
-
build
public static Counter.Builder build(String name, String help)
Return a Builder to allow configuration of a new Counter. Ensures required fields are provided.- Parameters:
name
- The name of the metrichelp
- The help string of the metric
-
build
public static Counter.Builder build()
Return a Builder to allow configuration of a new Counter.
-
newChild
protected Counter.Child newChild()
Description copied from class:SimpleCollector
Return a new child, workaround for Java generics limitations.- Specified by:
newChild
in classSimpleCollector<Counter.Child>
-
inc
public void inc()
Increment the counter with no labels by 1.
-
incWithExemplar
public void incWithExemplar(String... exemplarLabels)
LikeCounter.Child.incWithExemplar(String...)
, but for the counter without labels.
-
incWithExemplar
public void incWithExemplar(Map<String,String> exemplarLabels)
LikeCounter.Child.incWithExemplar(Map)
, but for the counter without labels.
-
inc
public void inc(double amt)
Increment the counter with no labels by the given amount.- Throws:
IllegalArgumentException
- If amt is negative.
-
incWithExemplar
public void incWithExemplar(double amt, String... exemplarLabels)
LikeCounter.Child.incWithExemplar(double, String...)
, but for the counter without labels.
-
incWithExemplar
public void incWithExemplar(double amt, Map<String,String> exemplarLabels)
LikeCounter.Child.incWithExemplar(double, Map)
, but for the counter without labels.
-
get
public double get()
Get the value of the counter.
-
collect
public List<Collector.MetricFamilySamples> collect()
Description copied from class:Collector
Return all metrics of this Collector.
-
describe
public List<Collector.MetricFamilySamples> describe()
Description copied from interface:Collector.Describable
Provide a list of metric families this Collector is expected to return. These should exclude the samples. This is used by the registry to detect collisions and duplicate registrations. Usually custom collectors do not have to implement Describable. If Describable is not implemented and the CollectorRegistry was created with auto describe enabled (which is the case for the default registry) thenCollector.collect()
will be called at registration time instead of describe. If this could cause problems, either implement a proper describe, or if that's not practical have describe return an empty list.- Specified by:
describe
in interfaceCollector.Describable
-
-