Class Histogram

All Implemented Interfaces:
DataPoint, DistributionDataPoint, TimerApi, io.prometheus.metrics.model.registry.Collector

Histogram metric. Example usage:

 Histogram histogram = Histogram.newBuilder()
         .withName("http_request_duration_seconds")
         .withHelp("HTTP request service time in seconds")
         .withUnit(SECONDS)
         .withLabelNames("method", "path", "status_code")
         .register();

 long start = System.nanoTime();
 // do something
 histogram.withLabelValues("GET", "/", "200").observe(Unit.nanosToSeconds(System.nanoTime() - start));
 
Prometheus supports two internal representations of histograms:
  1. Classic Histograms have a fixed number of buckets with fixed bucket boundaries.
  2. Native Histograms have an infinite number of buckets with a dynamic resolution. Prometheus native histograms are the same as OpenTelemetry's exponential histograms.
By default, a histogram maintains both representations, i.e. the example above will maintain a classic histogram representation with Prometheus' default bucket boundaries as well as native histogram representation. Which representation is used depends on the exposition format, i.e. which content type the Prometheus server accepts when scraping. Exposition format "Text" exposes the classic histogram, exposition format "Protobuf" exposes both representations. This is great for migrating from classic histograms to native histograms.

If you want the classic representation only, use Histogram.Builder.classicOnly. If you want the native representation only, use Histogram.Builder.nativeOnly.

  • Method Details

    • observe

      public void observe(double amount)
      Observe value.
      Specified by:
      observe in interface DistributionDataPoint
    • observeWithExemplar

      public void observeWithExemplar(double amount, io.prometheus.metrics.model.snapshots.Labels labels)
      Observe value, and create a custom exemplar with the given labels.
      Specified by:
      observeWithExemplar in interface DistributionDataPoint
    • isExemplarsEnabled

      protected boolean isExemplarsEnabled()
    • collect

      public io.prometheus.metrics.model.snapshots.HistogramSnapshot collect()
      Specified by:
      collect in interface io.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

    • newBuilder

      public static Histogram.Builder newBuilder()
    • newBuilder

      public static Histogram.Builder newBuilder(io.prometheus.metrics.config.PrometheusProperties config)
    • withLabelValues

      public DistributionDataPoint withLabelValues(String... labelValues)
    • remove

      public void remove(String... labelValues)
    • getNoLabels

      protected Histogram.DataPoint 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 <T> T getConfigProperty(io.prometheus.metrics.config.MetricsProperties[] properties, Function<io.prometheus.metrics.config.MetricsProperties,T> getter)