001package io.prometheus.client; 002 003import java.io.Closeable; 004import java.io.IOException; 005import java.util.ArrayList; 006import java.util.Collections; 007import java.util.List; 008import java.util.Map; 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 * @throws IOException 179 */ 180 @Override 181 public void close() throws IOException { 182 observeDuration(); 183 } 184 } 185 186 /** 187 * The value of a single Histogram. 188 * <p> 189 * <em>Warning:</em> References to a Child become invalid after using 190 * {@link SimpleCollector#remove} or {@link SimpleCollector#clear}. 191 */ 192 public static class Child { 193 194 /** 195 * Executes runnable code (i.e. a Java 8 Lambda) and observes a duration of how long it took to run. 196 * 197 * @param timeable Code that is being timed 198 * @return Measured duration in seconds for timeable to complete. 199 */ 200 public double time(Runnable timeable) { 201 Timer timer = startTimer(); 202 203 double elapsed; 204 try { 205 timeable.run(); 206 } finally { 207 elapsed = timer.observeDuration(); 208 } 209 return elapsed; 210 } 211 212 public static class Value { 213 public final double sum; 214 public final double[] buckets; 215 216 public Value(double sum, double[] buckets) { 217 this.sum = sum; 218 this.buckets = buckets; 219 } 220 } 221 222 private Child(double[] buckets) { 223 upperBounds = buckets; 224 cumulativeCounts = new DoubleAdder[buckets.length]; 225 for (int i = 0; i < buckets.length; ++i) { 226 cumulativeCounts[i] = new DoubleAdder(); 227 } 228 } 229 private final double[] upperBounds; 230 private final DoubleAdder[] cumulativeCounts; 231 private final DoubleAdder sum = new DoubleAdder(); 232 233 234 /** 235 * Observe the given amount. 236 */ 237 public void observe(double amt) { 238 for (int i = 0; i < upperBounds.length; ++i) { 239 // The last bucket is +Inf, so we always increment. 240 if (amt <= upperBounds[i]) { 241 cumulativeCounts[i].add(1); 242 break; 243 } 244 } 245 sum.add(amt); 246 } 247 /** 248 * Start a timer to track a duration. 249 * <p> 250 * Call {@link Timer#observeDuration} at the end of what you want to measure the duration of. 251 */ 252 public Timer startTimer() { 253 return new Timer(this, SimpleTimer.defaultTimeProvider.nanoTime()); 254 } 255 /** 256 * Get the value of the Histogram. 257 * <p> 258 * <em>Warning:</em> The definition of {@link Value} is subject to change. 259 */ 260 public Value get() { 261 double[] buckets = new double[cumulativeCounts.length]; 262 double acc = 0; 263 for (int i = 0; i < cumulativeCounts.length; ++i) { 264 acc += cumulativeCounts[i].sum(); 265 buckets[i] = acc; 266 } 267 return new Value(sum.sum(), buckets); 268 } 269 } 270 271 // Convenience methods. 272 /** 273 * Observe the given amount on the histogram with no labels. 274 */ 275 public void observe(double amt) { 276 noLabelsChild.observe(amt); 277 } 278 /** 279 * Start a timer to track a duration on the histogram with no labels. 280 * <p> 281 * Call {@link Timer#observeDuration} at the end of what you want to measure the duration of. 282 */ 283 public Timer startTimer() { 284 return noLabelsChild.startTimer(); 285 } 286 287 /** 288 * Executes runnable code (i.e. a Java 8 Lambda) and observes a duration of how long it took to run. 289 * 290 * @param timeable Code that is being timed 291 * @return Measured duration in seconds for timeable to complete. 292 */ 293 public double time(Runnable timeable){ 294 return noLabelsChild.time(timeable); 295 } 296 297 @Override 298 public List<MetricFamilySamples> collect() { 299 List<MetricFamilySamples.Sample> samples = new ArrayList<MetricFamilySamples.Sample>(); 300 for(Map.Entry<List<String>, Child> c: children.entrySet()) { 301 Child.Value v = c.getValue().get(); 302 List<String> labelNamesWithLe = new ArrayList<String>(labelNames); 303 labelNamesWithLe.add("le"); 304 for (int i = 0; i < v.buckets.length; ++i) { 305 List<String> labelValuesWithLe = new ArrayList<String>(c.getKey()); 306 labelValuesWithLe.add(doubleToGoString(buckets[i])); 307 samples.add(new MetricFamilySamples.Sample(fullname + "_bucket", labelNamesWithLe, labelValuesWithLe, v.buckets[i])); 308 } 309 samples.add(new MetricFamilySamples.Sample(fullname + "_count", labelNames, c.getKey(), v.buckets[buckets.length-1])); 310 samples.add(new MetricFamilySamples.Sample(fullname + "_sum", labelNames, c.getKey(), v.sum)); 311 } 312 313 return familySamplesList(Type.HISTOGRAM, samples); 314 } 315 316 @Override 317 public List<MetricFamilySamples> describe() { 318 return Collections.singletonList( 319 new MetricFamilySamples(fullname, Type.HISTOGRAM, help, Collections.<MetricFamilySamples.Sample>emptyList())); 320 } 321 322 double[] getBuckets() { 323 return buckets; 324 } 325 326 327}