From 69f46ca2c56afd43f82f1ad72952272fda712584 Mon Sep 17 00:00:00 2001 From: Yaroslav Stavnichiy Date: Sat, 7 Apr 2018 17:42:32 +0300 Subject: [PATCH] allow Histograms and Summaries to time Callables allow Gauges to time Callables + small fix in javadocs Signed-off-by: Yaroslav Stavnichiy --- .../main/java/io/prometheus/client/Gauge.java | 35 +++++++++++++++++-- .../java/io/prometheus/client/Histogram.java | 33 +++++++++++++++-- .../java/io/prometheus/client/Summary.java | 35 +++++++++++++++++-- .../java/io/prometheus/client/GaugeTest.java | 21 ++++++++--- .../io/prometheus/client/HistogramTest.java | 23 ++++++++---- .../io/prometheus/client/SummaryTest.java | 17 ++++++--- 6 files changed, 140 insertions(+), 24 deletions(-) diff --git a/simpleclient/src/main/java/io/prometheus/client/Gauge.java b/simpleclient/src/main/java/io/prometheus/client/Gauge.java index 2da01d08c..d868f8bef 100644 --- a/simpleclient/src/main/java/io/prometheus/client/Gauge.java +++ b/simpleclient/src/main/java/io/prometheus/client/Gauge.java @@ -5,6 +5,7 @@ import java.util.Collections; import java.util.List; import java.util.Map; +import java.util.concurrent.Callable; /** * Gauge metric, to report instantaneous values. @@ -193,7 +194,7 @@ public Timer startTimer() { } /** - * Executes runnable code (i.e. a Java 8 Lambda) and observes a duration of how long it took to run. + * 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. @@ -211,6 +212,24 @@ public double setToTime(Runnable timeable){ 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 setToTime(Callable timeable){ + Timer timer = startTimer(); + + try { + return timeable.call(); + } catch (Exception e) { + throw new RuntimeException(e); + } finally { + timer.setDuration(); + } + } + /** * Get the value of the gauge. */ @@ -272,7 +291,7 @@ public Timer startTimer() { } /** - * Executes runnable code (i.e. a Java 8 Lambda) and observes a duration of how long it took to run. + * 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. @@ -280,7 +299,17 @@ public Timer startTimer() { 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 setToTime(Callable timeable){ + return noLabelsChild.setToTime(timeable); + } + /** * Get the value of the gauge. */ diff --git a/simpleclient/src/main/java/io/prometheus/client/Histogram.java b/simpleclient/src/main/java/io/prometheus/client/Histogram.java index c5ed9060a..2b289806a 100644 --- a/simpleclient/src/main/java/io/prometheus/client/Histogram.java +++ b/simpleclient/src/main/java/io/prometheus/client/Histogram.java @@ -5,6 +5,7 @@ import java.util.Collections; import java.util.List; import java.util.Map; +import java.util.concurrent.Callable; /** * Histogram metric, to track distributions of events. @@ -190,7 +191,7 @@ public void close() { public static class Child { /** - * Executes runnable code (i.e. a Java 8 Lambda) and observes a duration of how long it took to run. + * 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. @@ -207,6 +208,24 @@ public double time(Runnable timeable) { 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 time(Callable 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; @@ -283,7 +302,7 @@ public Timer startTimer() { } /** - * Executes runnable code (i.e. a Java 8 Lambda) and observes a duration of how long it took to run. + * 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. @@ -292,6 +311,16 @@ 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 time(Callable timeable){ + return noLabelsChild.time(timeable); + } + @Override public List collect() { List samples = new ArrayList(); diff --git a/simpleclient/src/main/java/io/prometheus/client/Summary.java b/simpleclient/src/main/java/io/prometheus/client/Summary.java index 6bda32dc0..a341f2515 100644 --- a/simpleclient/src/main/java/io/prometheus/client/Summary.java +++ b/simpleclient/src/main/java/io/prometheus/client/Summary.java @@ -9,6 +9,7 @@ import java.util.Map; import java.util.SortedMap; import java.util.TreeMap; +import java.util.concurrent.Callable; import java.util.concurrent.TimeUnit; /** @@ -197,7 +198,7 @@ public void close() { public static class Child { /** - * Executes runnable code (i.e. a Java 8 Lambda) and observes a duration of how long it took to run. + * 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. @@ -214,6 +215,24 @@ public double time(Runnable timeable) { 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 time(Callable 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 count; public final double sum; @@ -297,7 +316,7 @@ public Timer startTimer() { } /** - * Executes runnable code (i.e. a Java 8 Lambda) and observes a duration of how long it took to run. + * 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. @@ -305,7 +324,17 @@ public Timer startTimer() { 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 time(Callable timeable){ + return noLabelsChild.time(timeable); + } + /** * Get the value of the Summary. *

diff --git a/simpleclient/src/test/java/io/prometheus/client/GaugeTest.java b/simpleclient/src/test/java/io/prometheus/client/GaugeTest.java index 7a9156684..8058f5c90 100644 --- a/simpleclient/src/test/java/io/prometheus/client/GaugeTest.java +++ b/simpleclient/src/test/java/io/prometheus/client/GaugeTest.java @@ -4,6 +4,7 @@ import java.util.ArrayList; import java.util.List; +import java.util.concurrent.Callable; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -29,7 +30,7 @@ public void tearDown() { private double getValue() { return registry.getSampleValue("nolabels").doubleValue(); } - + @Test public void testIncrement() { noLabels.inc(); @@ -45,7 +46,7 @@ public void testIncrement() { assertEquals(8.0, getValue(), .001); assertEquals(8.0, noLabels.get(), .001); } - + @Test public void testDecrement() { noLabels.dec(); @@ -57,7 +58,7 @@ public void testDecrement() { noLabels.labels().dec(); assertEquals(-8.0, getValue(), .001); } - + @Test public void testSet() { noLabels.set(42); @@ -93,8 +94,18 @@ public void run() { //no op } }); + assertEquals(10, getValue(), .001); assertEquals(10, elapsed, .001); + int result = noLabels.setToTime(new Callable() { + @Override + public Integer call() { + return 123; + } + }); + assertEquals(123, result); + assertEquals(10, getValue(), .001); + Gauge.Timer timer = noLabels.startTimer(); elapsed = timer.setDuration(); assertEquals(10, getValue(), .001); @@ -105,7 +116,7 @@ public void run() { public void noLabelsDefaultZeroValue() { assertEquals(0.0, getValue(), .001); } - + private Double getLabelsValue(String labelValue) { return registry.getSampleValue("labels", new String[]{"l"}, new String[]{labelValue}); } @@ -126,7 +137,7 @@ public void testLabels() { public void testCollect() { labels.labels("a").inc(); List mfs = labels.collect(); - + ArrayList samples = new ArrayList(); ArrayList labelNames = new ArrayList(); labelNames.add("l"); diff --git a/simpleclient/src/test/java/io/prometheus/client/HistogramTest.java b/simpleclient/src/test/java/io/prometheus/client/HistogramTest.java index d59e8eacb..d5d44e3d4 100644 --- a/simpleclient/src/test/java/io/prometheus/client/HistogramTest.java +++ b/simpleclient/src/test/java/io/prometheus/client/HistogramTest.java @@ -8,6 +8,7 @@ import java.lang.reflect.Modifier; import java.util.ArrayList; import java.util.List; +import java.util.concurrent.Callable; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -37,11 +38,11 @@ private double getSum() { return registry.getSampleValue("nolabels_sum").doubleValue(); } private double getBucket(double b) { - return registry.getSampleValue("nolabels_bucket", + return registry.getSampleValue("nolabels_bucket", new String[]{"le"}, new String[]{Collector.doubleToGoString(b)}).doubleValue(); } - + @Test public void testObserve() { noLabels.observe(2); @@ -119,19 +120,27 @@ public void run() { }); assertEquals(10, elapsed, .001); + int result = noLabels.time(new Callable() { + @Override + public Integer call() { + return 123; + } + }); + assertEquals(123, result); + Histogram.Timer timer = noLabels.startTimer(); elapsed = timer.observeDuration(); - assertEquals(2, getCount(), .001); - assertEquals(20, getSum(), .001); + assertEquals(3, getCount(), .001); + assertEquals(30, getSum(), .001); assertEquals(10, elapsed, .001); } - + @Test public void noLabelsDefaultZeroValue() { assertEquals(0.0, getCount(), .001); assertEquals(0.0, getSum(), .001); } - + private Double getLabelsCount(String labelValue) { return registry.getSampleValue("labels_count", new String[]{"l"}, new String[]{labelValue}); } @@ -166,7 +175,7 @@ public void testLeLabelThrows() { public void testCollect() { labels.labels("a").observe(2); List mfs = labels.collect(); - + ArrayList samples = new ArrayList(); ArrayList labelNames = new ArrayList(); labelNames.add("l"); diff --git a/simpleclient/src/test/java/io/prometheus/client/SummaryTest.java b/simpleclient/src/test/java/io/prometheus/client/SummaryTest.java index fca395c7e..06e1f9d9b 100644 --- a/simpleclient/src/test/java/io/prometheus/client/SummaryTest.java +++ b/simpleclient/src/test/java/io/prometheus/client/SummaryTest.java @@ -8,6 +8,7 @@ import java.lang.reflect.Modifier; import java.util.ArrayList; import java.util.List; +import java.util.concurrent.Callable; import static java.util.Arrays.asList; import static org.junit.Assert.assertEquals; @@ -124,19 +125,27 @@ public void run() { }); assertEquals(10, elapsed, .001); + int result = noLabels.time(new Callable() { + @Override + public Integer call() { + return 123; + } + }); + assertEquals(123, result); + Summary.Timer timer = noLabels.startTimer(); elapsed = timer.observeDuration(); - assertEquals(2, getCount(), .001); - assertEquals(20, getSum(), .001); + assertEquals(3, getCount(), .001); + assertEquals(30, getSum(), .001); assertEquals(10, elapsed, .001); } - + @Test public void noLabelsDefaultZeroValue() { assertEquals(0.0, getCount(), .001); assertEquals(0.0, getSum(), .001); } - + private Double getLabelsCount(String labelValue) { return registry.getSampleValue("labels_count", new String[]{"l"}, new String[]{labelValue}); }