Skip to content

Commit 40d179d

Browse files
yaroslabrian-brazil
authored andcommitted
allow Histograms and Summaries to time Callables (#378)
allow Gauges to time Callables + small fix in javadocs Signed-off-by: Yaroslav Stavnichiy <[email protected]>
1 parent ad17a8f commit 40d179d

File tree

6 files changed

+140
-24
lines changed

6 files changed

+140
-24
lines changed

simpleclient/src/main/java/io/prometheus/client/Gauge.java

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.util.Collections;
66
import java.util.List;
77
import java.util.Map;
8+
import java.util.concurrent.Callable;
89

910
/**
1011
* Gauge metric, to report instantaneous values.
@@ -193,7 +194,7 @@ public Timer startTimer() {
193194
}
194195

195196
/**
196-
* Executes runnable code (i.e. a Java 8 Lambda) and observes a duration of how long it took to run.
197+
* Executes runnable code (e.g. a Java 8 Lambda) and observes a duration of how long it took to run.
197198
*
198199
* @param timeable Code that is being timed
199200
* @return Measured duration in seconds for timeable to complete.
@@ -211,6 +212,24 @@ public double setToTime(Runnable timeable){
211212
return elapsed;
212213
}
213214

215+
/**
216+
* Executes callable code (e.g. a Java 8 Lambda) and observes a duration of how long it took to run.
217+
*
218+
* @param timeable Code that is being timed
219+
* @return Result returned by callable.
220+
*/
221+
public <E> E setToTime(Callable<E> timeable){
222+
Timer timer = startTimer();
223+
224+
try {
225+
return timeable.call();
226+
} catch (Exception e) {
227+
throw new RuntimeException(e);
228+
} finally {
229+
timer.setDuration();
230+
}
231+
}
232+
214233
/**
215234
* Get the value of the gauge.
216235
*/
@@ -272,15 +291,25 @@ public Timer startTimer() {
272291
}
273292

274293
/**
275-
* Executes runnable code (i.e. a Java 8 Lambda) and observes a duration of how long it took to run.
294+
* Executes runnable code (e.g. a Java 8 Lambda) and observes a duration of how long it took to run.
276295
*
277296
* @param timeable Code that is being timed
278297
* @return Measured duration in seconds for timeable to complete.
279298
*/
280299
public double setToTime(Runnable timeable){
281300
return noLabelsChild.setToTime(timeable);
282301
}
283-
302+
303+
/**
304+
* Executes callable code (e.g. a Java 8 Lambda) and observes a duration of how long it took to run.
305+
*
306+
* @param timeable Code that is being timed
307+
* @return Result returned by callable.
308+
*/
309+
public <E> E setToTime(Callable<E> timeable){
310+
return noLabelsChild.setToTime(timeable);
311+
}
312+
284313
/**
285314
* Get the value of the gauge.
286315
*/

simpleclient/src/main/java/io/prometheus/client/Histogram.java

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.util.Collections;
66
import java.util.List;
77
import java.util.Map;
8+
import java.util.concurrent.Callable;
89

910
/**
1011
* Histogram metric, to track distributions of events.
@@ -190,7 +191,7 @@ public void close() {
190191
public static class Child {
191192

192193
/**
193-
* Executes runnable code (i.e. a Java 8 Lambda) and observes a duration of how long it took to run.
194+
* Executes runnable code (e.g. a Java 8 Lambda) and observes a duration of how long it took to run.
194195
*
195196
* @param timeable Code that is being timed
196197
* @return Measured duration in seconds for timeable to complete.
@@ -207,6 +208,24 @@ public double time(Runnable timeable) {
207208
return elapsed;
208209
}
209210

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+
210229
public static class Value {
211230
public final double sum;
212231
public final double[] buckets;
@@ -283,7 +302,7 @@ public Timer startTimer() {
283302
}
284303

285304
/**
286-
* Executes runnable code (i.e. a Java 8 Lambda) and observes a duration of how long it took to run.
305+
* Executes runnable code (e.g. a Java 8 Lambda) and observes a duration of how long it took to run.
287306
*
288307
* @param timeable Code that is being timed
289308
* @return Measured duration in seconds for timeable to complete.
@@ -292,6 +311,16 @@ public double time(Runnable timeable){
292311
return noLabelsChild.time(timeable);
293312
}
294313

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+
295324
@Override
296325
public List<MetricFamilySamples> collect() {
297326
List<MetricFamilySamples.Sample> samples = new ArrayList<MetricFamilySamples.Sample>();

simpleclient/src/main/java/io/prometheus/client/Summary.java

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.util.Map;
1010
import java.util.SortedMap;
1111
import java.util.TreeMap;
12+
import java.util.concurrent.Callable;
1213
import java.util.concurrent.TimeUnit;
1314

1415
/**
@@ -197,7 +198,7 @@ public void close() {
197198
public static class Child {
198199

199200
/**
200-
* Executes runnable code (i.e. a Java 8 Lambda) and observes a duration of how long it took to run.
201+
* Executes runnable code (e.g. a Java 8 Lambda) and observes a duration of how long it took to run.
201202
*
202203
* @param timeable Code that is being timed
203204
* @return Measured duration in seconds for timeable to complete.
@@ -214,6 +215,24 @@ public double time(Runnable timeable) {
214215
return elapsed;
215216
}
216217

218+
/**
219+
* Executes callable code (e.g. a Java 8 Lambda) and observes a duration of how long it took to run.
220+
*
221+
* @param timeable Code that is being timed
222+
* @return Result returned by callable.
223+
*/
224+
public <E> E time(Callable<E> timeable) {
225+
Timer timer = startTimer();
226+
227+
try {
228+
return timeable.call();
229+
} catch (Exception e) {
230+
throw new RuntimeException(e);
231+
} finally {
232+
timer.observeDuration();
233+
}
234+
}
235+
217236
public static class Value {
218237
public final double count;
219238
public final double sum;
@@ -297,15 +316,25 @@ public Timer startTimer() {
297316
}
298317

299318
/**
300-
* Executes runnable code (i.e. a Java 8 Lambda) and observes a duration of how long it took to run.
319+
* Executes runnable code (e.g. a Java 8 Lambda) and observes a duration of how long it took to run.
301320
*
302321
* @param timeable Code that is being timed
303322
* @return Measured duration in seconds for timeable to complete.
304323
*/
305324
public double time(Runnable timeable){
306325
return noLabelsChild.time(timeable);
307326
}
308-
327+
328+
/**
329+
* Executes callable code (e.g. a Java 8 Lambda) and observes a duration of how long it took to run.
330+
*
331+
* @param timeable Code that is being timed
332+
* @return Result returned by callable.
333+
*/
334+
public <E> E time(Callable<E> timeable){
335+
return noLabelsChild.time(timeable);
336+
}
337+
309338
/**
310339
* Get the value of the Summary.
311340
* <p>

simpleclient/src/test/java/io/prometheus/client/GaugeTest.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import java.util.ArrayList;
66
import java.util.List;
7+
import java.util.concurrent.Callable;
78
import org.junit.After;
89
import org.junit.Before;
910
import org.junit.Test;
@@ -29,7 +30,7 @@ public void tearDown() {
2930
private double getValue() {
3031
return registry.getSampleValue("nolabels").doubleValue();
3132
}
32-
33+
3334
@Test
3435
public void testIncrement() {
3536
noLabels.inc();
@@ -45,7 +46,7 @@ public void testIncrement() {
4546
assertEquals(8.0, getValue(), .001);
4647
assertEquals(8.0, noLabels.get(), .001);
4748
}
48-
49+
4950
@Test
5051
public void testDecrement() {
5152
noLabels.dec();
@@ -57,7 +58,7 @@ public void testDecrement() {
5758
noLabels.labels().dec();
5859
assertEquals(-8.0, getValue(), .001);
5960
}
60-
61+
6162
@Test
6263
public void testSet() {
6364
noLabels.set(42);
@@ -93,8 +94,18 @@ public void run() {
9394
//no op
9495
}
9596
});
97+
assertEquals(10, getValue(), .001);
9698
assertEquals(10, elapsed, .001);
9799

100+
int result = noLabels.setToTime(new Callable<Integer>() {
101+
@Override
102+
public Integer call() {
103+
return 123;
104+
}
105+
});
106+
assertEquals(123, result);
107+
assertEquals(10, getValue(), .001);
108+
98109
Gauge.Timer timer = noLabels.startTimer();
99110
elapsed = timer.setDuration();
100111
assertEquals(10, getValue(), .001);
@@ -105,7 +116,7 @@ public void run() {
105116
public void noLabelsDefaultZeroValue() {
106117
assertEquals(0.0, getValue(), .001);
107118
}
108-
119+
109120
private Double getLabelsValue(String labelValue) {
110121
return registry.getSampleValue("labels", new String[]{"l"}, new String[]{labelValue});
111122
}
@@ -126,7 +137,7 @@ public void testLabels() {
126137
public void testCollect() {
127138
labels.labels("a").inc();
128139
List<Collector.MetricFamilySamples> mfs = labels.collect();
129-
140+
130141
ArrayList<Collector.MetricFamilySamples.Sample> samples = new ArrayList<Collector.MetricFamilySamples.Sample>();
131142
ArrayList<String> labelNames = new ArrayList<String>();
132143
labelNames.add("l");

simpleclient/src/test/java/io/prometheus/client/HistogramTest.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.lang.reflect.Modifier;
99
import java.util.ArrayList;
1010
import java.util.List;
11+
import java.util.concurrent.Callable;
1112
import org.junit.After;
1213
import org.junit.Before;
1314
import org.junit.Test;
@@ -37,11 +38,11 @@ private double getSum() {
3738
return registry.getSampleValue("nolabels_sum").doubleValue();
3839
}
3940
private double getBucket(double b) {
40-
return registry.getSampleValue("nolabels_bucket",
41+
return registry.getSampleValue("nolabels_bucket",
4142
new String[]{"le"},
4243
new String[]{Collector.doubleToGoString(b)}).doubleValue();
4344
}
44-
45+
4546
@Test
4647
public void testObserve() {
4748
noLabels.observe(2);
@@ -119,19 +120,27 @@ public void run() {
119120
});
120121
assertEquals(10, elapsed, .001);
121122

123+
int result = noLabels.time(new Callable<Integer>() {
124+
@Override
125+
public Integer call() {
126+
return 123;
127+
}
128+
});
129+
assertEquals(123, result);
130+
122131
Histogram.Timer timer = noLabels.startTimer();
123132
elapsed = timer.observeDuration();
124-
assertEquals(2, getCount(), .001);
125-
assertEquals(20, getSum(), .001);
133+
assertEquals(3, getCount(), .001);
134+
assertEquals(30, getSum(), .001);
126135
assertEquals(10, elapsed, .001);
127136
}
128-
137+
129138
@Test
130139
public void noLabelsDefaultZeroValue() {
131140
assertEquals(0.0, getCount(), .001);
132141
assertEquals(0.0, getSum(), .001);
133142
}
134-
143+
135144
private Double getLabelsCount(String labelValue) {
136145
return registry.getSampleValue("labels_count", new String[]{"l"}, new String[]{labelValue});
137146
}
@@ -166,7 +175,7 @@ public void testLeLabelThrows() {
166175
public void testCollect() {
167176
labels.labels("a").observe(2);
168177
List<Collector.MetricFamilySamples> mfs = labels.collect();
169-
178+
170179
ArrayList<Collector.MetricFamilySamples.Sample> samples = new ArrayList<Collector.MetricFamilySamples.Sample>();
171180
ArrayList<String> labelNames = new ArrayList<String>();
172181
labelNames.add("l");

simpleclient/src/test/java/io/prometheus/client/SummaryTest.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.lang.reflect.Modifier;
99
import java.util.ArrayList;
1010
import java.util.List;
11+
import java.util.concurrent.Callable;
1112

1213
import static java.util.Arrays.asList;
1314
import static org.junit.Assert.assertEquals;
@@ -124,19 +125,27 @@ public void run() {
124125
});
125126
assertEquals(10, elapsed, .001);
126127

128+
int result = noLabels.time(new Callable<Integer>() {
129+
@Override
130+
public Integer call() {
131+
return 123;
132+
}
133+
});
134+
assertEquals(123, result);
135+
127136
Summary.Timer timer = noLabels.startTimer();
128137
elapsed = timer.observeDuration();
129-
assertEquals(2, getCount(), .001);
130-
assertEquals(20, getSum(), .001);
138+
assertEquals(3, getCount(), .001);
139+
assertEquals(30, getSum(), .001);
131140
assertEquals(10, elapsed, .001);
132141
}
133-
142+
134143
@Test
135144
public void noLabelsDefaultZeroValue() {
136145
assertEquals(0.0, getCount(), .001);
137146
assertEquals(0.0, getSum(), .001);
138147
}
139-
148+
140149
private Double getLabelsCount(String labelValue) {
141150
return registry.getSampleValue("labels_count", new String[]{"l"}, new String[]{labelValue});
142151
}

0 commit comments

Comments
 (0)