public class Gauge extends SimpleCollector<Gauge.Child> implements Collector.Describable
Examples of Gauges include:
An example Gauge:
class YourClass {
static final Gauge inprogressRequests = Gauge.build()
.name("inprogress_requests").help("Inprogress requests.").register();
void processRequest() {
inprogressRequest.inc();
// Your code here.
inprogressRequest.dec();
}
}
You can also use labels to track different types of metric:
class YourClass {
static final Gauge inprogressRequests = Gauge.build()
.name("inprogress_requests").help("Inprogress requests.")
.labelNames("method").register();
void processGetRequest() {
inprogressRequests.labels("get").inc();
// Your code here.
inprogressRequests.labels("get").dec();
}
void processPostRequest() {
inprogressRequests.labels("post").inc();
// Your code here.
inprogressRequests.labels("post").dec();
}
}
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 |
Gauge.Builder |
static class |
Gauge.Child
The value of a single Gauge.
|
static class |
Gauge.Timer
Represents an event being timed.
|
Collector.Describable, Collector.MetricFamilySamples, Collector.Type
children, fullname, help, labelNames, noLabelsChild
MILLISECONDS_PER_SECOND, NANOSECONDS_PER_SECOND
Modifier and Type | Method and Description |
---|---|
static Gauge.Builder |
build()
Return a Builder to allow configuration of a new Gauge.
|
static Gauge.Builder |
build(String name,
String help)
Return a Builder to allow configuration of a new Gauge.
|
List<Collector.MetricFamilySamples> |
collect()
Return all of the metrics of this Collector.
|
void |
dec()
Increment the gauge with no labels by 1.
|
void |
dec(double amt)
Decrement the gauge with no labels by the given amount.
|
List<Collector.MetricFamilySamples> |
describe()
Provide a list of metric families this Collector is expected to return.
|
double |
get()
Get the value of the gauge.
|
void |
inc()
Increment the gauge with no labels by 1.
|
void |
inc(double amt)
Increment the gauge with no labels by the given amount.
|
protected Gauge.Child |
newChild()
Return a new child, workaround for Java generics limitations.
|
void |
set(double val)
Set the gauge with no labels to the given value.
|
void |
setToCurrentTime()
Set the gauge with no labels to the current unixtime.
|
double |
setToTime(Runnable timeable)
Executes runnable code (i.e.
|
Gauge.Timer |
startTimer()
Start a timer to track a duration, for the gauge with no labels.
|
clear, familySamplesList, initializeNoLabelsChild, labels, remove, setChild
checkMetricLabelName, checkMetricName, doubleToGoString, register, register, sanitizeMetricName
public static Gauge.Builder build(String name, String help)
name
- The name of the metrichelp
- The help string of the metricpublic static Gauge.Builder build()
protected Gauge.Child newChild()
SimpleCollector
newChild
in class SimpleCollector<Gauge.Child>
public void inc()
public void inc(double amt)
public void dec()
public void dec(double amt)
public void set(double val)
public void setToCurrentTime()
public Gauge.Timer startTimer()
This is primarily useful for tracking the durations of major steps of batch jobs,
which are then pushed to a PushGateway.
For tracking other durations/latencies you should usually use a Summary
.
Call Gauge.Timer.setDuration()
at the end of what you want to measure the duration of.
public double setToTime(Runnable timeable)
timeable
- Code that is being timedpublic 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 © 2018. All Rights Reserved.