Class Histogram
java.lang.Object
io.prometheus.metrics.core.metrics.Metric
io.prometheus.metrics.core.metrics.MetricWithFixedMetadata
io.prometheus.metrics.core.metrics.StatefulMetric<DistributionDataPoint,Histogram.DataPoint>
io.prometheus.metrics.core.metrics.Histogram
- All Implemented Interfaces:
DataPoint,DistributionDataPoint,TimerApi,Collector
public class Histogram
extends StatefulMetric<DistributionDataPoint,Histogram.DataPoint>
implements DistributionDataPoint
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
FieldsModifier and TypeFieldDescriptionprivate static final intprivate final double[]private final ExemplarSamplerConfigprivate static final double[][]private final intprivate final intprivate final doubleprivate final doubleprivate final longFields inherited from class io.prometheus.metrics.core.metrics.MetricWithFixedMetadata
labelNamesFields inherited from class io.prometheus.metrics.core.metrics.Metric
constLabels -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivateHistogram(Histogram.Builder builder, PrometheusProperties prometheusProperties) -
Method Summary
Modifier and TypeMethodDescriptionstatic Histogram.Builderbuilder()static Histogram.Builderbuilder(PrometheusProperties config) collect()Called when the Prometheus server scrapes metrics.protected HistogramSnapshotcollect(List<Labels> labels, List<Histogram.DataPoint> metricData) labels and metricData have the same size.longgetCount()Get the count of observations.Returns the metric type for registration-time validation.doublegetSum()Get the sum of all observed values.protected Histogram.DataPointvoidobserve(double amount) Observevalue.voidobserveWithExemplar(double amount, Labels labels) Observevalue, and create a custom exemplar with the given labels.Methods inherited from class io.prometheus.metrics.core.metrics.StatefulMetric
clear, getConfigProperty, getMetricProperties, getNoLabels, initLabelValues, labelValues, remove, removeIfMethods inherited from class io.prometheus.metrics.core.metrics.MetricWithFixedMetadata
getLabelNames, 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
-
Field Details
-
CLASSIC_HISTOGRAM
private static final int CLASSIC_HISTOGRAM- See Also:
-
NATIVE_BOUNDS
private static final double[][] NATIVE_BOUNDS -
exemplarSamplerConfig
-
classicUpperBounds
private final double[] classicUpperBounds -
nativeInitialSchema
private final int nativeInitialSchema -
nativeMinZeroThreshold
private final double nativeMinZeroThreshold -
nativeMaxZeroThreshold
private final double nativeMaxZeroThreshold -
nativeMaxBuckets
private final int nativeMaxBuckets -
nativeResetDurationSeconds
private final long nativeResetDurationSeconds
-
-
Constructor Details
-
Histogram
-
-
Method Details
-
getSum
public double getSum()Description copied from interface:DistributionDataPointGet the sum of all observed values.- Specified by:
getSumin interfaceDistributionDataPoint
-
getCount
public long getCount()Description copied from interface:DistributionDataPointGet the count of observations.- Specified by:
getCountin interfaceDistributionDataPoint
-
observe
public void observe(double amount) Description copied from interface:DistributionDataPointObservevalue.- Specified by:
observein interfaceDistributionDataPoint
-
observeWithExemplar
Description copied from interface:DistributionDataPointObservevalue, and create a custom exemplar with the given labels.- Specified by:
observeWithExemplarin interfaceDistributionDataPoint
-
collect
Description copied from interface:CollectorCalled when the Prometheus server scrapes metrics.- Specified by:
collectin interfaceCollector- Overrides:
collectin classStatefulMetric<DistributionDataPoint,Histogram.DataPoint>
-
collect
Description copied from class:StatefulMetriclabels and metricData have the same size. labels.get(i) are the labels for metricData.get(i).- Specified by:
collectin classStatefulMetric<DistributionDataPoint,Histogram.DataPoint>
-
getMetricType
Description copied from interface:CollectorReturns the metric type for registration-time validation.This is used to prevent different metric types (e.g., Counter and Gauge) from sharing the same name. Returning
nullmeans type validation is skipped for this collector.Validation is performed only at registration time. If this method returns
null, no type validation is performed for this collector, and duplicate or conflicting metrics may result in invalid exposition output.- Specified by:
getMetricTypein interfaceCollector- Returns:
- the metric type, or
nullto skip validation
-
newDataPoint
- Specified by:
newDataPointin classStatefulMetric<DistributionDataPoint,Histogram.DataPoint>
-
builder
-
builder
-