public class Counter extends SimpleCollector<Counter.Child>
Example of Counters include:
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 Promtheus
server than individual metrics for each labelset.Modifier and Type | Class and Description |
---|---|
static class |
Counter.Builder |
static class |
Counter.Child
The value of a single Counter.
|
Collector.MetricFamilySamples, Collector.Type
children, fullname, help, labelNames, noLabelsChild
METRIC_LABEL_NAME_RE, METRIC_NAME_RE, MILLISECONDS_PER_SECOND, NANOSECONDS_PER_SECOND, RESERVED_METRIC_LABEL_NAME_RE
Modifier and Type | Method and Description |
---|---|
static Counter.Builder |
build()
Return a Builder to allow configuration of a new Counter.
|
List<Collector.MetricFamilySamples> |
collect()
Return all of the metrics of this Collector.
|
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.
|
clear, initializeNoLabelsChild, labels, remove, setChild
checkMetricLabelName, checkMetricName, doubleToGoString, register, register
public static Counter.Builder build()
protected Counter.Child newChild()
SimpleCollector
newChild
in class SimpleCollector<Counter.Child>
public void inc()
public void inc(double amt)
IllegalArgumentException
- If amt is negative.Copyright © 2015. All rights reserved.