Skip to content

Commit 944ecf9

Browse files
checkettsbrian-brazil
authored andcommitted
Add time() methods that remove needs for finally blocks (#173)
1 parent 6b42425 commit 944ecf9

File tree

6 files changed

+138
-8
lines changed

6 files changed

+138
-8
lines changed

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,26 @@ public void setToCurrentTime() {
181181
public Timer startTimer() {
182182
return new Timer(this);
183183
}
184+
185+
/**
186+
* Executes runnable code (i.e. a Java 8 Lambda) and observes a duration of how long it took to run.
187+
*
188+
* @param timeable Code that is being timed
189+
* @return Measured duration in seconds for timeable to complete.
190+
*/
191+
public double setToTime(Runnable timeable){
192+
Timer timer = startTimer();
193+
194+
double elapsed;
195+
try {
196+
timeable.run();
197+
} finally {
198+
elapsed = timer.setDuration();
199+
}
200+
201+
return elapsed;
202+
}
203+
184204
/**
185205
* Get the value of the gauge.
186206
*/
@@ -241,6 +261,16 @@ public Timer startTimer() {
241261
return noLabelsChild.startTimer();
242262
}
243263

264+
/**
265+
* Executes runnable code (i.e. a Java 8 Lambda) and observes a duration of how long it took to run.
266+
*
267+
* @param timeable Code that is being timed
268+
* @return Measured duration in seconds for timeable to complete.
269+
*/
270+
public double setToTime(Runnable timeable){
271+
return noLabelsChild.setToTime(timeable);
272+
}
273+
244274
@Override
245275
public List<MetricFamilySamples> collect() {
246276
List<MetricFamilySamples.Sample> samples = new ArrayList<MetricFamilySamples.Sample>();

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@
3636
* requestTimer.observeDuration();
3737
* }
3838
* }
39+
*
40+
* // Or if using Java 8 lambdas.
41+
* void processRequestLambda(Request req) {
42+
* requestLatency.time(() -> {
43+
* // Your code here.
44+
* });
45+
* }
3946
* }
4047
* }
4148
* </pre>
@@ -172,6 +179,25 @@ public void close() throws IOException {
172179
* {@link SimpleCollector#remove} or {@link SimpleCollector#clear}.
173180
*/
174181
public static class Child {
182+
183+
/**
184+
* Executes runnable code (i.e. a Java 8 Lambda) and observes a duration of how long it took to run.
185+
*
186+
* @param timeable Code that is being timed
187+
* @return Measured duration in seconds for timeable to complete.
188+
*/
189+
public double time(Runnable timeable) {
190+
Timer timer = startTimer();
191+
192+
double elapsed;
193+
try {
194+
timeable.run();
195+
} finally {
196+
elapsed = timer.observeDuration();
197+
}
198+
return elapsed;
199+
}
200+
175201
public static class Value {
176202
public final double sum;
177203
public final double[] buckets;
@@ -247,6 +273,16 @@ public Timer startTimer() {
247273
return noLabelsChild.startTimer();
248274
}
249275

276+
/**
277+
* Executes runnable code (i.e. a Java 8 Lambda) and observes a duration of how long it took to run.
278+
*
279+
* @param timeable Code that is being timed
280+
* @return Measured duration in seconds for timeable to complete.
281+
*/
282+
public double time(Runnable timeable){
283+
return noLabelsChild.time(timeable);
284+
}
285+
250286
@Override
251287
public List<MetricFamilySamples> collect() {
252288
List<MetricFamilySamples.Sample> samples = new ArrayList<MetricFamilySamples.Sample>();

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

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,15 @@
4040
* requestTimer.observeDuration();
4141
* }
4242
* }
43-
* }
43+
*
44+
* // Or if using Java 8 and lambdas.
45+
* void processRequestLambda(Request req) {
46+
* receivedBytes.observe(req.size());
47+
* requestLatency.time(() -> {
48+
* // Your code here.
49+
* });
50+
* }
51+
* }
4452
* }
4553
* </pre>
4654
* This would allow you to track request rate, average latency and average request size.
@@ -179,6 +187,25 @@ public void close() throws IOException {
179187
* {@link SimpleCollector#remove} or {@link SimpleCollector#clear}.
180188
*/
181189
public static class Child {
190+
191+
/**
192+
* Executes runnable code (i.e. a Java 8 Lambda) and observes a duration of how long it took to run.
193+
*
194+
* @param timeable Code that is being timed
195+
* @return Measured duration in seconds for timeable to complete.
196+
*/
197+
public double time(Runnable timeable) {
198+
Timer timer = startTimer();
199+
200+
double elapsed;
201+
try {
202+
timeable.run();
203+
} finally {
204+
elapsed = timer.observeDuration();
205+
}
206+
return elapsed;
207+
}
208+
182209
public static class Value {
183210
public final double count;
184211
public final double sum;
@@ -261,6 +288,16 @@ public Timer startTimer() {
261288
return noLabelsChild.startTimer();
262289
}
263290

291+
/**
292+
* Executes runnable code (i.e. a Java 8 Lambda) and observes a duration of how long it took to run.
293+
*
294+
* @param timeable Code that is being timed
295+
* @return Measured duration in seconds for timeable to complete.
296+
*/
297+
public double time(Runnable timeable){
298+
return noLabelsChild.time(timeable);
299+
}
300+
264301
@Override
265302
public List<MetricFamilySamples> collect() {
266303
List<MetricFamilySamples.Sample> samples = new ArrayList<MetricFamilySamples.Sample>();

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,17 @@ long nanoTime() {
8282
return value;
8383
}
8484
};
85+
86+
double elapsed = noLabels.setToTime(new Runnable() {
87+
@Override
88+
public void run() {
89+
//no op
90+
}
91+
});
92+
assertEquals(10, elapsed, .001);
93+
8594
Gauge.Timer timer = noLabels.startTimer();
86-
double elapsed = timer.setDuration();
95+
elapsed = timer.setDuration();
8796
assertEquals(10, getValue(), .001);
8897
assertEquals(10, elapsed, .001);
8998
}

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,19 @@ long nanoTime() {
110110
return value;
111111
}
112112
};
113+
114+
double elapsed = noLabels.time(new Runnable() {
115+
@Override
116+
public void run() {
117+
//no op
118+
}
119+
});
120+
assertEquals(10, elapsed, .001);
121+
113122
Histogram.Timer timer = noLabels.startTimer();
114-
double elapsed = timer.observeDuration();
115-
assertEquals(1, getCount(), .001);
116-
assertEquals(10, getSum(), .001);
123+
elapsed = timer.observeDuration();
124+
assertEquals(2, getCount(), .001);
125+
assertEquals(20, getSum(), .001);
117126
assertEquals(10, elapsed, .001);
118127
}
119128

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,19 @@ long nanoTime() {
111111
return value;
112112
}
113113
};
114+
115+
double elapsed = noLabels.time(new Runnable() {
116+
@Override
117+
public void run() {
118+
//no op
119+
}
120+
});
121+
assertEquals(10, elapsed, .001);
122+
114123
Summary.Timer timer = noLabels.startTimer();
115-
double elapsed = timer.observeDuration();
116-
assertEquals(1, getCount(), .001);
117-
assertEquals(10, getSum(), .001);
124+
elapsed = timer.observeDuration();
125+
assertEquals(2, getCount(), .001);
126+
assertEquals(20, getSum(), .001);
118127
assertEquals(10, elapsed, .001);
119128
}
120129

0 commit comments

Comments
 (0)