Package io.prometheus.client
Class Counter
- All Implemented Interfaces:
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 the rate()
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.
}
}
These can be aggregated and processed together much more easily in the Prometheus
server than individual metrics for each labelset.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
static class
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
Fields inherited from class io.prometheus.client.Collector
MILLISECONDS_PER_SECOND, NANOSECONDS_PER_SECOND
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic Counter.Builder
build()
Return a Builder to allow configuration of a new Counter.static Counter.Builder
Return a Builder to allow configuration of a new Counter.collect()
Return all of the metrics of this Collector.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.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, doubleToGoString, register, register, sanitizeMetricName
-
Constructor Details
-
Counter
Counter(Counter.Builder b)
-
-
Method Details
-
build
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
Return a Builder to allow configuration of a new Counter. -
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. -
inc
public void inc(double amt) Increment the counter with no labels by the given amount.- Throws:
IllegalArgumentException
- If amt is negative.
-
get
public double get()Get the value of the counter. -
collect
Description copied from class:Collector
Return all of the metrics of this Collector. -
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
-