Class Histogram

All Implemented Interfaces:
Collector.Describable

public class Histogram extends SimpleCollector<Histogram.Child> implements Collector.Describable
Histogram metric, to track distributions of events.

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.