public class Counter extends SimpleCollector<Counter.Child> implements Collector.Describable
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 Prometheus
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.Describable, Collector.MetricFamilySamples, Collector.Type
children, fullname, help, labelNames, noLabelsChild
MILLISECONDS_PER_SECOND, NANOSECONDS_PER_SECOND
Modifier and Type | Method and 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 of the 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.
|
protected Counter.Child |
newChild()
Return a new child, workaround for Java generics limitations.
|
clear, familySamplesList, initializeNoLabelsChild, labels, remove, setChild
checkMetricLabelName, checkMetricName, doubleToGoString, register, register, sanitizeMetricName
public static Counter.Builder build(String name, String help)
name
- The name of the metrichelp
- The help string of the metricpublic 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.public double get()
public List<Collector.MetricFamilySamples> collect()
Collector
public List<Collector.MetricFamilySamples> describe()
Collector.Describable
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.describe
in interface Collector.Describable
Copyright © 2017. All rights reserved.