public class Histogram extends SimpleCollector<Histogram.Child> implements Collector.Describable
Example of uses for Histograms include:
Note: Each bucket is one timeseries. Many buckets and/or many dimensions with labels can produce large amount of time series, that may cause performance problems.
The default buckets are intended to cover a typical web/rpc request from milliseconds to seconds.
Example Histograms:
class YourClass {
static final Histogram requestLatency = Histogram.build()
.name("requests_latency_seconds").help("Request latency in seconds.").register();
void processRequest(Request req) {
Histogram.Timer requestTimer = requestLatency.startTimer();
try {
// Your code here.
} finally {
requestTimer.observeDuration();
}
}
// Or if using Java 8 lambdas.
void processRequestLambda(Request req) {
requestLatency.time(() -> {
// Your code here.
});
}
}
You can choose your own buckets:
static final Histogram requestLatency = Histogram.build()
.buckets(.01, .02, .03, .04)
.name("requests_latency_seconds").help("Request latency in seconds.").register();
linearBuckets
and
exponentialBuckets
offer easy ways to set common bucket patterns.Modifier and Type | Class and Description |
---|---|
static class |
Histogram.Builder |
static class |
Histogram.Child
The value of a single Histogram.
|
static class |
Histogram.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 Histogram.Builder |
build()
Return a Builder to allow configuration of a new Histogram.
|
static Histogram.Builder |
build(String name,
String help)
Return a Builder to allow configuration of a new Histogram.
|
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.
|
protected Histogram.Child |
newChild()
Return a new child, workaround for Java generics limitations.
|
void |
observe(double amt)
Observe the given amount on the histogram with no labels.
|
Histogram.Timer |
startTimer()
Start a timer to track a duration on the histogram with no labels.
|
double |
time(Runnable timeable)
Executes runnable code (i.e.
|
clear, familySamplesList, initializeNoLabelsChild, labels, remove, setChild
checkMetricLabelName, checkMetricName, doubleToGoString, register, register, sanitizeMetricName
public static Histogram.Builder build(String name, String help)
name
- The name of the metrichelp
- The help string of the metricpublic static Histogram.Builder build()
protected Histogram.Child newChild()
SimpleCollector
newChild
in class SimpleCollector<Histogram.Child>
public void observe(double amt)
public Histogram.Timer startTimer()
Call Histogram.Timer.observeDuration()
at the end of what you want to measure the duration of.
public double time(Runnable timeable)
timeable
- Code that is being timedpublic 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.