Class Histogram
- All Implemented Interfaces:
Collector.Describable
Example of uses for Histograms include:
- Response latency
- Request size
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.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classstatic classThe value of a single Histogram.static classRepresents an event being timed.Nested classes/interfaces inherited from class io.prometheus.client.Collector
Collector.Describable, Collector.MetricFamilySamples, Collector.Type -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final double[]private final HistogramExemplarSamplerprivate final BooleanFields inherited from class io.prometheus.client.SimpleCollector
children, fullname, help, labelNames, noLabelsChild, unitFields inherited from class io.prometheus.client.Collector
MILLISECONDS_PER_SECOND, NANOSECONDS_PER_SECOND -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic Histogram.Builderbuild()Return a Builder to allow configuration of a new Histogram.static Histogram.BuilderReturn a Builder to allow configuration of a new Histogram.collect()Return all metrics of this Collector.describe()Provide a list of metric families this Collector is expected to return.(package private) double[]protected Histogram.ChildnewChild()Return a new child, workaround for Java generics limitations.voidobserve(double amt) Observe the given amount on the histogram with no labels.voidobserveWithExemplar(double amt, String... exemplarLabels) LikeHistogram.Child.observeWithExemplar(double, String...), but for the histogram without labels.voidobserveWithExemplar(double amt, Map<String, String> exemplarLabels) LikeHistogram.Child.observeWithExemplar(double, Map), but for the histogram without labels.Start a timer to track a duration on the histogram with no labels.doubleExecutes runnable code (e.g.<E> EExecutes callable code (e.g.doubletimeWithExemplar(Runnable timeable, String... exemplarLabels) Liketime(Runnable), but additionally create an exemplar.doubletimeWithExemplar(Runnable timeable, Map<String, String> exemplarLabels) Liketime(Runnable), but additionally create an exemplar.<E> EtimeWithExemplar(Callable<E> timeable, String... exemplarLabels) Liketime(Callable), but additionally create an exemplar.<E> EtimeWithExemplar(Callable<E> timeable, Map<String, String> exemplarLabels) Liketime(Callable), but additionally create an exemplar.Methods inherited from class io.prometheus.client.SimpleCollector
clear, familySamplesList, initializeNoLabelsChild, labels, remove, setChildMethods inherited from class io.prometheus.client.Collector
checkMetricLabelName, checkMetricName, collect, doubleToGoString, register, register, sanitizeMetricName
-
Field Details
-
buckets
private final double[] buckets -
exemplarsEnabled
-
exemplarSampler
-
-
Constructor Details
-
Histogram
Histogram(Histogram.Builder b)
-
-
Method Details
-
build
Return a Builder to allow configuration of a new Histogram. 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 Histogram. -
newChild
Description copied from class:SimpleCollectorReturn a new child, workaround for Java generics limitations.- Specified by:
newChildin classSimpleCollector<Histogram.Child>
-
observe
public void observe(double amt) Observe the given amount on the histogram with no labels.- Parameters:
amt- in most cases amt should be >= 0. Negative values are supported, but you should read https://prometheus.io/docs/practices/histograms/#count-and-sum-of-observations for implications and alternatives.
-
observeWithExemplar
LikeHistogram.Child.observeWithExemplar(double, String...), but for the histogram without labels. -
observeWithExemplar
LikeHistogram.Child.observeWithExemplar(double, Map), but for the histogram without labels. -
startTimer
Start a timer to track a duration on the histogram with no labels.Call
Histogram.Timer.observeDuration()at the end of what you want to measure the duration of. -
time
Executes runnable code (e.g. a Java 8 Lambda) and observes a duration of how long it took to run.- Parameters:
timeable- Code that is being timed- Returns:
- Measured duration in seconds for timeable to complete.
-
timeWithExemplar
Liketime(Runnable), but additionally create an exemplar.See
Histogram.Child.observeWithExemplar(double, String...)for documentation on theexemplarLabelsparameter. -
timeWithExemplar
Liketime(Runnable), but additionally create an exemplar.See
Histogram.Child.observeWithExemplar(double, Map)for documentation on theexemplarLabelsparameter. -
time
Executes callable code (e.g. a Java 8 Lambda) and observes a duration of how long it took to run.- Parameters:
timeable- Code that is being timed- Returns:
- Result returned by callable.
-
timeWithExemplar
Liketime(Callable), but additionally create an exemplar.See
Histogram.Child.observeWithExemplar(double, String...)for documentation on theexemplarLabelsparameter. -
timeWithExemplar
Liketime(Callable), but additionally create an exemplar.See
Histogram.Child.observeWithExemplar(double, Map)for documentation on theexemplarLabelsparameter. -
collect
Description copied from class:CollectorReturn all metrics of this Collector. -
describe
Description copied from interface:Collector.DescribableProvide 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:
describein interfaceCollector.Describable
-
getBuckets
double[] getBuckets()
-