forked from prometheus/client_java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHistogram.java
More file actions
358 lines (325 loc) · 10.6 KB
/
Histogram.java
File metadata and controls
358 lines (325 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
package io.prometheus.client;
import java.io.Closeable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
/**
* Histogram metric, to track distributions of events.
* <p>
* Example of uses for Histograms include:
* <ul>
* <li>Response latency</li>
* <li>Request size</li>
* </ul>
* <p>
* <em>Note:</em> 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.
*
* <p>
* The default buckets are intended to cover a typical web/rpc request from milliseconds to seconds.
* <p>
* Example Histograms:
* <pre>
* {@code
* 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.
* });
* }
* }
* }
* </pre>
* <p>
* You can choose your own buckets:
* <pre>
* {@code
* static final Histogram requestLatency = Histogram.build()
* .buckets(.01, .02, .03, .04)
* .name("requests_latency_seconds").help("Request latency in seconds.").register();
* }
* </pre>
* {@link Histogram.Builder#linearBuckets(double, double, int) linearBuckets} and
* {@link Histogram.Builder#exponentialBuckets(double, double, int) exponentialBuckets}
* offer easy ways to set common bucket patterns.
*/
public class Histogram extends SimpleCollector<Histogram.Child> implements Collector.Describable {
private final double[] buckets;
Histogram(Builder b) {
super(b);
buckets = b.buckets;
initializeNoLabelsChild();
}
public static class Builder extends SimpleCollector.Builder<Builder, Histogram> {
private double[] buckets = new double[]{.005, .01, .025, .05, .075, .1, .25, .5, .75, 1, 2.5, 5, 7.5, 10};
@Override
public Histogram create() {
for (int i = 0; i < buckets.length - 1; i++) {
if (buckets[i] >= buckets[i + 1]) {
throw new IllegalStateException("Histogram buckets must be in increasing order: "
+ buckets[i] + " >= " + buckets[i + 1]);
}
}
if (buckets.length == 0) {
throw new IllegalStateException("Histogram must have at least one bucket.");
}
for (String label: labelNames) {
if (label.equals("le")) {
throw new IllegalStateException("Histogram cannot have a label named 'le'.");
}
}
// Append infinity bucket if it's not already there.
if (buckets[buckets.length - 1] != Double.POSITIVE_INFINITY) {
double[] tmp = new double[buckets.length + 1];
System.arraycopy(buckets, 0, tmp, 0, buckets.length);
tmp[buckets.length] = Double.POSITIVE_INFINITY;
buckets = tmp;
}
dontInitializeNoLabelsChild = true;
return new Histogram(this);
}
/**
* Set the upper bounds of buckets for the histogram.
*/
public Builder buckets(double... buckets) {
this.buckets = buckets;
return this;
}
/**
* Set the upper bounds of buckets for the histogram with a linear sequence.
*/
public Builder linearBuckets(double start, double width, int count) {
buckets = new double[count];
for (int i = 0; i < count; i++){
buckets[i] = start + i*width;
}
return this;
}
/**
* Set the upper bounds of buckets for the histogram with an exponential sequence.
*/
public Builder exponentialBuckets(double start, double factor, int count) {
buckets = new double[count];
for (int i = 0; i < count; i++) {
buckets[i] = start * Math.pow(factor, i);
}
return this;
}
}
/**
* Return a Builder to allow configuration of a new Histogram. Ensures required fields are provided.
*
* @param name The name of the metric
* @param help The help string of the metric
*/
public static Builder build(String name, String help) {
return new Builder().name(name).help(help);
}
/**
* Return a Builder to allow configuration of a new Histogram.
*/
public static Builder build() {
return new Builder();
}
@Override
protected Child newChild() {
return new Child(buckets);
}
/**
* Represents an event being timed.
*/
public static class Timer implements Closeable {
private final Child child;
private final long start;
private Timer(Child child, long start) {
this.child = child;
this.start = start;
}
/**
* Observe the amount of time in seconds since {@link Child#startTimer} was called.
* @return Measured duration in seconds since {@link Child#startTimer} was called.
*/
public double observeDuration() {
double elapsed = SimpleTimer.elapsedSecondsFromNanos(start, SimpleTimer.defaultTimeProvider.nanoTime());
child.observe(elapsed);
return elapsed;
}
/**
* Equivalent to calling {@link #observeDuration()}.
*/
@Override
public void close() {
observeDuration();
}
}
/**
* The value of a single Histogram.
* <p>
* <em>Warning:</em> References to a Child become invalid after using
* {@link SimpleCollector#remove} or {@link SimpleCollector#clear}.
*/
public static class Child {
/**
* Executes runnable code (e.g. a Java 8 Lambda) and observes a duration of how long it took to run.
*
* @param timeable Code that is being timed
* @return Measured duration in seconds for timeable to complete.
*/
public double time(Runnable timeable) {
Timer timer = startTimer();
double elapsed;
try {
timeable.run();
} finally {
elapsed = timer.observeDuration();
}
return elapsed;
}
/**
* Executes callable code (e.g. a Java 8 Lambda) and observes a duration of how long it took to run.
*
* @param timeable Code that is being timed
* @return Result returned by callable.
*/
public <E> E time(Callable<E> timeable) {
Timer timer = startTimer();
try {
return timeable.call();
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
timer.observeDuration();
}
}
public static class Value {
public final double sum;
public final double[] buckets;
public Value(double sum, double[] buckets) {
this.sum = sum;
this.buckets = buckets;
}
}
private Child(double[] buckets) {
upperBounds = buckets;
cumulativeCounts = new DoubleAdder[buckets.length];
for (int i = 0; i < buckets.length; ++i) {
cumulativeCounts[i] = new DoubleAdder();
}
}
private final double[] upperBounds;
private final DoubleAdder[] cumulativeCounts;
private final DoubleAdder sum = new DoubleAdder();
/**
* Observe the given amount.
*/
public void observe(double amt) {
for (int i = 0; i < upperBounds.length; ++i) {
// The last bucket is +Inf, so we always increment.
if (amt <= upperBounds[i]) {
cumulativeCounts[i].add(1.0);
break;
}
}
sum.add(amt);
}
/**
* Start a timer to track a duration.
* <p>
* Call {@link Timer#observeDuration} at the end of what you want to measure the duration of.
*/
public Timer startTimer() {
return new Timer(this, SimpleTimer.defaultTimeProvider.nanoTime());
}
/**
* Get the value of the Histogram.
* <p>
* <em>Warning:</em> The definition of {@link Value} is subject to change.
*/
public Value get() {
double[] buckets = new double[cumulativeCounts.length];
double acc = 0;
for (int i = 0; i < cumulativeCounts.length; ++i) {
acc += cumulativeCounts[i].sum();
buckets[i] = acc;
}
return new Value(sum.sum(), buckets);
}
}
// Convenience methods.
/**
* Observe the given amount on the histogram with no labels.
*/
public void observe(double amt) {
noLabelsChild.observe(amt);
}
/**
* Start a timer to track a duration on the histogram with no labels.
* <p>
* Call {@link Timer#observeDuration} at the end of what you want to measure the duration of.
*/
public Timer startTimer() {
return noLabelsChild.startTimer();
}
/**
* Executes runnable code (e.g. a Java 8 Lambda) and observes a duration of how long it took to run.
*
* @param timeable Code that is being timed
* @return Measured duration in seconds for timeable to complete.
*/
public double time(Runnable timeable){
return noLabelsChild.time(timeable);
}
/**
* Executes callable code (e.g. a Java 8 Lambda) and observes a duration of how long it took to run.
*
* @param timeable Code that is being timed
* @return Result returned by callable.
*/
public <E> E time(Callable<E> timeable){
return noLabelsChild.time(timeable);
}
@Override
public List<MetricFamilySamples> collect() {
final Map.Entry<List<String>, Child>[] children = children();
List<MetricFamilySamples.Sample> samples = new ArrayList<MetricFamilySamples.Sample>(children.length);
for(Map.Entry<List<String>,Child> c : children) {
if (c == null) {
continue;
}
Child.Value v = c.getValue().get();
List<String> labelNamesWithLe = new ArrayList<String>(labelNames);
labelNamesWithLe.add("le");
for (int i = 0; i < v.buckets.length; ++i) {
List<String> labelValuesWithLe = new ArrayList<String>(c.getKey());
labelValuesWithLe.add(doubleToGoString(buckets[i]));
samples.add(new MetricFamilySamples.Sample(fullname + "_bucket", labelNamesWithLe, labelValuesWithLe, v.buckets[i]));
}
samples.add(new MetricFamilySamples.Sample(fullname + "_count", labelNames, c.getKey(), v.buckets[buckets.length-1]));
samples.add(new MetricFamilySamples.Sample(fullname + "_sum", labelNames, c.getKey(), v.sum));
}
return familySamplesList(Type.HISTOGRAM, samples);
}
@Override
public List<MetricFamilySamples> describe() {
return Collections.singletonList(
new MetricFamilySamples(fullname, Type.HISTOGRAM, help, Collections.<MetricFamilySamples.Sample>emptyList()));
}
double[] getBuckets() {
return buckets;
}
}