forked from prometheus/client_java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGauge.java
More file actions
338 lines (312 loc) · 8.57 KB
/
Gauge.java
File metadata and controls
338 lines (312 loc) · 8.57 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
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;
/**
* Gauge metric, to report instantaneous values.
* <p>
* Examples of Gauges include:
* <ul>
* <li>Inprogress requests</li>
* <li>Number of items in a queue</li>
* <li>Free memory</li>
* <li>Total memory</li>
* <li>Temperature</li>
* </ul>
*
* Gauges can go both up and down.
* <p>
* An example Gauge:
* <pre>
* {@code
* class YourClass {
* static final Gauge inprogressRequests = Gauge.build()
* .name("inprogress_requests").help("Inprogress requests.").register();
*
* void processRequest() {
* inprogressRequests.inc();
* // Your code here.
* inprogressRequests.dec();
* }
* }
* }
* </pre>
*
* <p>
* You can also use labels to track different types of metric:
* <pre>
* {@code
* class YourClass {
* static final Gauge inprogressRequests = Gauge.build()
* .name("inprogress_requests").help("Inprogress requests.")
* .labelNames("method").register();
*
* void processGetRequest() {
* inprogressRequests.labels("get").inc();
* // Your code here.
* inprogressRequests.labels("get").dec();
* }
* void processPostRequest() {
* inprogressRequests.labels("post").inc();
* // Your code here.
* inprogressRequests.labels("post").dec();
* }
* }
* }
* </pre>
* <p>
* These can be aggregated and processed together much more easily in the Prometheus
* server than individual metrics for each labelset.
*/
public class Gauge extends SimpleCollector<Gauge.Child> implements Collector.Describable {
Gauge(Builder b) {
super(b);
}
public static class Builder extends SimpleCollector.Builder<Builder, Gauge> {
@Override
public Gauge create() {
return new Gauge(this);
}
}
/**
* Return a Builder to allow configuration of a new Gauge. 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 Gauge.
*/
public static Builder build() {
return new Builder();
}
@Override
protected Child newChild() {
return new Child();
}
/**
* Represents an event being timed.
*/
public static class Timer implements Closeable {
private final Child child;
private final long start;
private Timer(Child child) {
this.child = child;
start = Child.timeProvider.nanoTime();
}
/**
* Set 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 setDuration() {
double elapsed = (Child.timeProvider.nanoTime() - start) / NANOSECONDS_PER_SECOND;
child.set(elapsed);
return elapsed;
}
/**
* Equivalent to calling {@link #setDuration()}.
*/
@Override
public void close() {
setDuration();
}
}
/**
* The value of a single Gauge.
* <p>
* <em>Warning:</em> References to a Child become invalid after using
* {@link SimpleCollector#remove} or {@link SimpleCollector#clear},
*/
public static class Child {
private final DoubleAdder value = new DoubleAdder();
static TimeProvider timeProvider = new TimeProvider();
/**
* Increment the gauge by 1.
*/
public void inc() {
inc(1.0);
}
/**
* Increment the gauge by the given amount.
*/
public void inc(double amt) {
value.add(amt);
}
/**
* Decrement the gauge by 1.
*/
public void dec() {
dec(1.0);
}
/**
* Decrement the gauge by the given amount.
*/
public void dec(double amt) {
value.add(-amt);
}
/**
* Set the gauge to the given value.
*/
public void set(double val) {
value.set(val);
}
/**
* Set the gauge to the current unixtime.
*/
public void setToCurrentTime() {
set(timeProvider.currentTimeMillis() / MILLISECONDS_PER_SECOND);
}
/**
* Start a timer to track a duration.
* <p>
* Call {@link Timer#setDuration} at the end of what you want to measure the duration of.
* <p>
* This is primarily useful for tracking the durations of major steps of batch jobs,
* which are then pushed to a PushGateway.
* For tracking other durations/latencies you should usually use a {@link Summary}.
*/
public Timer startTimer() {
return new Timer(this);
}
/**
* 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 setToTime(Runnable timeable){
Timer timer = startTimer();
double elapsed;
try {
timeable.run();
} finally {
elapsed = timer.setDuration();
}
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 setToTime(Callable<E> timeable){
Timer timer = startTimer();
try {
return timeable.call();
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
timer.setDuration();
}
}
/**
* Get the value of the gauge.
*/
public double get() {
return value.sum();
}
}
// Convenience methods.
/**
* Increment the gauge with no labels by 1.
*/
public void inc() {
inc(1.0);
}
/**
* Increment the gauge with no labels by the given amount.
*/
public void inc(double amt) {
noLabelsChild.inc(amt);
}
/**
* Decrement the gauge with no labels by 1.
*/
public void dec() {
dec(1.0);
}
/**
* Decrement the gauge with no labels by the given amount.
*/
public void dec(double amt) {
noLabelsChild.dec(amt);
}
/**
* Set the gauge with no labels to the given value.
*/
public void set(double val) {
noLabelsChild.set(val);
}
/**
* Set the gauge with no labels to the current unixtime.
*/
public void setToCurrentTime() {
noLabelsChild.setToCurrentTime();
}
/**
* Start a timer to track a duration, for the gauge with no labels.
* <p>
* This is primarily useful for tracking the durations of major steps of batch jobs,
* which are then pushed to a PushGateway.
* For tracking other durations/latencies you should usually use a {@link Summary}.
* <p>
* Call {@link Timer#setDuration} 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 setToTime(Runnable timeable){
return noLabelsChild.setToTime(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 setToTime(Callable<E> timeable){
return noLabelsChild.setToTime(timeable);
}
/**
* Get the value of the gauge.
*/
public double get() {
return noLabelsChild.get();
}
@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) {
samples.add(new MetricFamilySamples.Sample(fullname, labelNames, c.getKey(), c.getValue().get()));
}
}
return familySamplesList(Type.GAUGE, samples);
}
@Override
public List<MetricFamilySamples> describe() {
return Collections.<MetricFamilySamples>singletonList(new GaugeMetricFamily(fullname, help, labelNames));
}
static class TimeProvider {
long currentTimeMillis() {
return System.currentTimeMillis();
}
long nanoTime() {
return System.nanoTime();
}
}
}