-
Notifications
You must be signed in to change notification settings - Fork 825
allow Histograms and Summaries to time Callables #368
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
99c9798
7b9c888
9a49945
9795fc7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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> E time(Callable<E> timeable) { | ||
| Timer timer = startTimer(); | ||
|
|
||
| try { | ||
| return timeable.call(); | ||
| } catch (Exception e) { | ||
| throw new RuntimeException(e); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are you wrapping this exception rather than just letting it propagate naturally?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cause in most cases there will be no checked exceptions within that Callable. And I would not make
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay |
||
| } 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> E time(Callable<E> timeable){ | ||
| return noLabelsChild.time(timeable); | ||
| } | ||
|
|
||
| @Override | ||
| public List<MetricFamilySamples> collect() { | ||
| List<MetricFamilySamples.Sample> samples = new ArrayList<MetricFamilySamples.Sample>(); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Gauge also has a time() method
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can add that too.