java.lang.Object
io.prometheus.metrics.core.metrics.Metric
io.prometheus.metrics.core.metrics.MetricWithFixedMetadata
io.prometheus.metrics.core.metrics.Histogram
- All Implemented Interfaces:
DataPoint,DistributionDataPoint,TimerApi,io.prometheus.metrics.model.registry.Collector
Histogram metric. Example usage:
Histogram histogram = Histogram.builder()
.name("http_request_duration_seconds")
.help("HTTP request service time in seconds")
.unit(SECONDS)
.labelNames("method", "path", "status_code")
.register();
long start = System.nanoTime();
// do something
histogram.labelValues("GET", "/", "200").observe(Unit.nanosToSeconds(System.nanoTime() - start));
Prometheus supports two internal representations of histograms:
- Classic Histograms have a fixed number of buckets with fixed bucket boundaries.
- Native Histograms have an infinite number of buckets with a dynamic resolution. Prometheus native histograms are the same as OpenTelemetry's exponential histograms.
If you want the classic representation only, use Histogram.Builder.classicOnly. If you
want the native representation only, use Histogram.Builder.nativeOnly.
-
Nested Class Summary
Nested Classes -
Field Summary
Fields inherited from class io.prometheus.metrics.core.metrics.MetricWithFixedMetadata
labelNamesFields inherited from class io.prometheus.metrics.core.metrics.Metric
constLabels -
Method Summary
Modifier and TypeMethodDescriptionstatic Histogram.Builderbuilder()static Histogram.Builderbuilder(io.prometheus.metrics.config.PrometheusProperties config) voidclear()Reset the metric (remove all data points).io.prometheus.metrics.model.snapshots.HistogramSnapshotcollect()protected io.prometheus.metrics.model.snapshots.HistogramSnapshotcollect(List<io.prometheus.metrics.model.snapshots.Labels> labels, List<Histogram.DataPoint> metricData) labels and metricData have the same size. labels.get(i) are the labels for metricData.get(i).protected <P> PgetConfigProperty(io.prometheus.metrics.config.MetricsProperties[] properties, Function<io.prometheus.metrics.config.MetricsProperties, P> getter) protected io.prometheus.metrics.config.MetricsProperties[]getMetricProperties(io.prometheus.metrics.core.metrics.StatefulMetric.Builder<?, ?> builder, io.prometheus.metrics.config.PrometheusProperties prometheusProperties) protected Histogram.DataPointvoidinitLabelValues(String... labelValues) Initialize label values.protected booleanlabelValues(String... labelValues) protected Histogram.DataPointvoidobserve(double amount) Observevalue.voidobserveWithExemplar(double amount, io.prometheus.metrics.model.snapshots.Labels labels) Observevalue, and create a custom exemplar with the given labels.voidRemove the data point with the given label values.voidRemove the data points when the given function.Methods inherited from class io.prometheus.metrics.core.metrics.MetricWithFixedMetadata
getMetadata, getPrometheusNameMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface io.prometheus.metrics.model.registry.Collector
collect, collect, collectMethods inherited from interface io.prometheus.metrics.core.datapoints.DistributionDataPoint
startTimerMethods inherited from interface io.prometheus.metrics.core.datapoints.TimerApi
time, time, timeChecked
-
Method Details
-
observe
public void observe(double amount) Description copied from interface:DistributionDataPointObservevalue.- Specified by:
observein interfaceDistributionDataPoint
-
observeWithExemplar
public void observeWithExemplar(double amount, io.prometheus.metrics.model.snapshots.Labels labels) Description copied from interface:DistributionDataPointObservevalue, and create a custom exemplar with the given labels.- Specified by:
observeWithExemplarin interfaceDistributionDataPoint
-
isExemplarsEnabled
protected boolean isExemplarsEnabled() -
collect
public io.prometheus.metrics.model.snapshots.HistogramSnapshot collect()- Specified by:
collectin interfaceio.prometheus.metrics.model.registry.Collector
-
collect
protected io.prometheus.metrics.model.snapshots.HistogramSnapshot collect(List<io.prometheus.metrics.model.snapshots.Labels> labels, List<Histogram.DataPoint> metricData) labels and metricData have the same size. labels.get(i) are the labels for metricData.get(i). -
newDataPoint
-
builder
-
builder
-
initLabelValues
Initialize label values.Example: Imagine you have a counter for payments as follows
payment_transactions_total{payment_type="credit card"} 7.0 payment_transactions_total{payment_type="paypal"} 3.0Now, the data points for thepayment_typelabel values get initialized when they are first used, i.e. the first time you call
the data point with labelcounter.labelValues("paypal").inc();payment_type="paypal"will go from non-existent to having value1.0.In some cases this is confusing, and you want to have data points initialized on application start with an initial value of
0.0:payment_transactions_total{payment_type="credit card"} 0.0 payment_transactions_total{payment_type="paypal"} 0.0initLabelValues(...)can be used to initialize label value, so that the data points show up in the exposition format with an initial value of zero. -
labelValues
-
remove
Remove the data point with the given label values. See https://prometheus.io/docs/instrumenting/writing_clientlibs/#labels. -
removeIf
Remove the data points when the given function. -
clear
public void clear()Reset the metric (remove all data points). -
getNoLabels
-
getMetricProperties
protected io.prometheus.metrics.config.MetricsProperties[] getMetricProperties(io.prometheus.metrics.core.metrics.StatefulMetric.Builder<?, ?> builder, io.prometheus.metrics.config.PrometheusProperties prometheusProperties) -
getConfigProperty
protected <P> P getConfigProperty(io.prometheus.metrics.config.MetricsProperties[] properties, Function<io.prometheus.metrics.config.MetricsProperties, P> getter)
-