001package io.prometheus.client;
002
003import java.io.Closeable;
004import java.util.ArrayList;
005import java.util.Collections;
006import java.util.List;
007import java.util.Map;
008import java.util.concurrent.Callable;
009
010/**
011 * Histogram metric, to track distributions of events.
012 * <p>
013 * Example of uses for Histograms include:
014 * <ul>
015 *  <li>Response latency</li>
016 *  <li>Request size</li>
017 * </ul>
018 * <p>
019 * <em>Note:</em> Each bucket is one timeseries. Many buckets and/or many dimensions with labels
020 * can produce large amount of time series, that may cause performance problems.
021 *
022 * <p>
023 * The default buckets are intended to cover a typical web/rpc request from milliseconds to seconds.
024 * <p>
025 * Example Histograms:
026 * <pre>
027 * {@code
028 *   class YourClass {
029 *     static final Histogram requestLatency = Histogram.build()
030 *         .name("requests_latency_seconds").help("Request latency in seconds.").register();
031 *
032 *     void processRequest(Request req) {
033 *        Histogram.Timer requestTimer = requestLatency.startTimer();
034 *        try {
035 *          // Your code here.
036 *        } finally {
037 *          requestTimer.observeDuration();
038 *        }
039 *     }
040 *
041 *     // Or if using Java 8 lambdas.
042 *     void processRequestLambda(Request req) {
043 *        requestLatency.time(() -> {
044 *          // Your code here.
045 *        });
046 *     }
047 *   }
048 * }
049 * </pre>
050 * <p>
051 * You can choose your own buckets:
052 * <pre>
053 * {@code
054 *     static final Histogram requestLatency = Histogram.build()
055 *         .buckets(.01, .02, .03, .04)
056 *         .name("requests_latency_seconds").help("Request latency in seconds.").register();
057 * }
058 * </pre>
059 * {@link Histogram.Builder#linearBuckets(double, double, int) linearBuckets} and
060 * {@link Histogram.Builder#exponentialBuckets(double, double, int) exponentialBuckets}
061 * offer easy ways to set common bucket patterns.
062 */
063public class Histogram extends SimpleCollector<Histogram.Child> implements Collector.Describable {
064  private final double[] buckets;
065
066  Histogram(Builder b) {
067    super(b);
068    buckets = b.buckets;
069    initializeNoLabelsChild();
070  }
071
072  public static class Builder extends SimpleCollector.Builder<Builder, Histogram> {
073    private double[] buckets = new double[]{.005, .01, .025, .05, .075, .1, .25, .5, .75, 1, 2.5, 5, 7.5, 10};
074
075    @Override
076    public Histogram create() {
077      for (int i = 0; i < buckets.length - 1; i++) {
078        if (buckets[i] >= buckets[i + 1]) {
079          throw new IllegalStateException("Histogram buckets must be in increasing order: "
080              + buckets[i] + " >= " + buckets[i + 1]);
081        }
082      }
083      if (buckets.length == 0) {
084          throw new IllegalStateException("Histogram must have at least one bucket.");
085      }
086      for (String label: labelNames) {
087        if (label.equals("le")) {
088            throw new IllegalStateException("Histogram cannot have a label named 'le'.");
089        }
090      }
091
092      // Append infinity bucket if it's not already there.
093      if (buckets[buckets.length - 1] != Double.POSITIVE_INFINITY) {
094        double[] tmp = new double[buckets.length + 1];
095        System.arraycopy(buckets, 0, tmp, 0, buckets.length);
096        tmp[buckets.length] = Double.POSITIVE_INFINITY;
097        buckets = tmp;
098      }
099      dontInitializeNoLabelsChild = true;
100      return new Histogram(this);
101    }
102
103    /**
104      * Set the upper bounds of buckets for the histogram.
105      */
106    public Builder buckets(double... buckets) {
107      this.buckets = buckets;
108      return this;
109    }
110
111    /**
112      * Set the upper bounds of buckets for the histogram with a linear sequence.
113      */
114    public Builder linearBuckets(double start, double width, int count) {
115      buckets = new double[count];
116      for (int i = 0; i < count; i++){
117        buckets[i] = start + i*width;
118      }
119      return this;
120    }
121    /**
122      * Set the upper bounds of buckets for the histogram with an exponential sequence.
123      */
124    public Builder exponentialBuckets(double start, double factor, int count) {
125      buckets = new double[count];
126      for (int i = 0; i < count; i++) {
127        buckets[i] = start * Math.pow(factor, i);
128      }
129      return this;
130    }
131
132  }
133
134  /**
135   *  Return a Builder to allow configuration of a new Histogram. Ensures required fields are provided.
136   *
137   *  @param name The name of the metric
138   *  @param help The help string of the metric
139   */
140  public static Builder build(String name, String help) {
141    return new Builder().name(name).help(help);
142  }
143
144  /**
145   *  Return a Builder to allow configuration of a new Histogram.
146   */
147  public static Builder build() {
148    return new Builder();
149  }
150
151  @Override
152  protected Child newChild() {
153    return new Child(buckets);
154  }
155
156  /**
157   * Represents an event being timed.
158   */
159  public static class Timer implements Closeable {
160    private final Child child;
161    private final long start;
162    private Timer(Child child, long start) {
163      this.child = child;
164      this.start = start;
165    }
166    /**
167     * Observe the amount of time in seconds since {@link Child#startTimer} was called.
168     * @return Measured duration in seconds since {@link Child#startTimer} was called.
169     */
170    public double observeDuration() {
171        double elapsed = SimpleTimer.elapsedSecondsFromNanos(start, SimpleTimer.defaultTimeProvider.nanoTime());
172        child.observe(elapsed);
173        return elapsed;
174    }
175
176    /**
177     * Equivalent to calling {@link #observeDuration()}.
178     */
179    @Override
180    public void close() {
181      observeDuration();
182    }
183  }
184
185  /**
186   * The value of a single Histogram.
187   * <p>
188   * <em>Warning:</em> References to a Child become invalid after using
189   * {@link SimpleCollector#remove} or {@link SimpleCollector#clear}.
190   */
191  public static class Child {
192
193    /**
194     * Executes runnable code (e.g. a Java 8 Lambda) and observes a duration of how long it took to run.
195     *
196     * @param timeable Code that is being timed
197     * @return Measured duration in seconds for timeable to complete.
198     */
199    public double time(Runnable timeable) {
200      Timer timer = startTimer();
201
202      double elapsed;
203      try {
204        timeable.run();
205      } finally {
206        elapsed = timer.observeDuration();
207      }
208      return elapsed;
209    }
210
211    /**
212     * Executes callable code (e.g. a Java 8 Lambda) and observes a duration of how long it took to run.
213     *
214     * @param timeable Code that is being timed
215     * @return Result returned by callable.
216     */
217    public <E> E time(Callable<E> timeable) {
218      Timer timer = startTimer();
219
220      try {
221        return timeable.call();
222      } catch (Exception e) {
223        throw new RuntimeException(e);
224      } finally {
225        timer.observeDuration();
226      }
227    }
228
229    public static class Value {
230      public final double sum;
231      public final double[] buckets;
232
233      public Value(double sum, double[] buckets) {
234        this.sum = sum;
235        this.buckets = buckets;
236      }
237    }
238
239    private Child(double[] buckets) {
240      upperBounds = buckets;
241      cumulativeCounts = new DoubleAdder[buckets.length];
242      for (int i = 0; i < buckets.length; ++i) {
243        cumulativeCounts[i] = new DoubleAdder();
244      }
245    }
246    private final double[] upperBounds;
247    private final DoubleAdder[] cumulativeCounts;
248    private final DoubleAdder sum = new DoubleAdder();
249
250
251    /**
252     * Observe the given amount.
253     */
254    public void observe(double amt) {
255      for (int i = 0; i < upperBounds.length; ++i) {
256        // The last bucket is +Inf, so we always increment.
257        if (amt <= upperBounds[i]) {
258          cumulativeCounts[i].add(1);
259          break;
260        }
261      }
262      sum.add(amt);
263    }
264    /**
265     * Start a timer to track a duration.
266     * <p>
267     * Call {@link Timer#observeDuration} at the end of what you want to measure the duration of.
268     */
269    public Timer startTimer() {
270      return new Timer(this, SimpleTimer.defaultTimeProvider.nanoTime());
271    }
272    /**
273     * Get the value of the Histogram.
274     * <p>
275     * <em>Warning:</em> The definition of {@link Value} is subject to change.
276     */
277    public Value get() {
278      double[] buckets = new double[cumulativeCounts.length];
279      double acc = 0;
280      for (int i = 0; i < cumulativeCounts.length; ++i) {
281        acc += cumulativeCounts[i].sum();
282        buckets[i] = acc;
283      }
284      return new Value(sum.sum(), buckets);
285    }
286  }
287
288  // Convenience methods.
289  /**
290   * Observe the given amount on the histogram with no labels.
291   */
292  public void observe(double amt) {
293    noLabelsChild.observe(amt);
294  }
295  /**
296   * Start a timer to track a duration on the histogram with no labels.
297   * <p>
298   * Call {@link Timer#observeDuration} at the end of what you want to measure the duration of.
299   */
300  public Timer startTimer() {
301    return noLabelsChild.startTimer();
302  }
303
304  /**
305   * Executes runnable code (e.g. a Java 8 Lambda) and observes a duration of how long it took to run.
306   *
307   * @param timeable Code that is being timed
308   * @return Measured duration in seconds for timeable to complete.
309   */
310  public double time(Runnable timeable){
311    return noLabelsChild.time(timeable);
312  }
313
314  /**
315   * Executes callable code (e.g. a Java 8 Lambda) and observes a duration of how long it took to run.
316   *
317   * @param timeable Code that is being timed
318   * @return Result returned by callable.
319   */
320  public <E> E time(Callable<E> timeable){
321    return noLabelsChild.time(timeable);
322  }
323
324  @Override
325  public List<MetricFamilySamples> collect() {
326    List<MetricFamilySamples.Sample> samples = new ArrayList<MetricFamilySamples.Sample>();
327    for(Map.Entry<List<String>, Child> c: children.entrySet()) {
328      Child.Value v = c.getValue().get();
329      List<String> labelNamesWithLe = new ArrayList<String>(labelNames);
330      labelNamesWithLe.add("le");
331      for (int i = 0; i < v.buckets.length; ++i) {
332        List<String> labelValuesWithLe = new ArrayList<String>(c.getKey());
333        labelValuesWithLe.add(doubleToGoString(buckets[i]));
334        samples.add(new MetricFamilySamples.Sample(fullname + "_bucket", labelNamesWithLe, labelValuesWithLe, v.buckets[i]));
335      }
336      samples.add(new MetricFamilySamples.Sample(fullname + "_count", labelNames, c.getKey(), v.buckets[buckets.length-1]));
337      samples.add(new MetricFamilySamples.Sample(fullname + "_sum", labelNames, c.getKey(), v.sum));
338    }
339
340    return familySamplesList(Type.HISTOGRAM, samples);
341  }
342
343  @Override
344  public List<MetricFamilySamples> describe() {
345    return Collections.singletonList(
346            new MetricFamilySamples(fullname, Type.HISTOGRAM, help, Collections.<MetricFamilySamples.Sample>emptyList()));
347  }
348
349  double[] getBuckets() {
350    return buckets;
351  }
352
353
354}