Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 32 additions & 3 deletions simpleclient/src/main/java/io/prometheus/client/Gauge.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -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> 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.
*/
Expand Down Expand Up @@ -272,15 +291,25 @@ 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.
*/
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.
*/
Expand Down
33 changes: 31 additions & 2 deletions simpleclient/src/main/java/io/prometheus/client/Histogram.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -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);
} finally {
timer.observeDuration();
}
}

public static class Value {
public final double sum;
public final double[] buckets;
Expand Down Expand Up @@ -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.
Expand All @@ -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>();
Expand Down
35 changes: 32 additions & 3 deletions simpleclient/src/main/java/io/prometheus/client/Summary.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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.
Expand All @@ -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> E time(Callable<E> 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;
Expand Down Expand Up @@ -297,15 +316,25 @@ 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.
*/
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);
}

/**
* Get the value of the Summary.
* <p>
Expand Down
21 changes: 16 additions & 5 deletions simpleclient/src/test/java/io/prometheus/client/GaugeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -29,7 +30,7 @@ public void tearDown() {
private double getValue() {
return registry.getSampleValue("nolabels").doubleValue();
}

@Test
public void testIncrement() {
noLabels.inc();
Expand All @@ -45,7 +46,7 @@ public void testIncrement() {
assertEquals(8.0, getValue(), .001);
assertEquals(8.0, noLabels.get(), .001);
}

@Test
public void testDecrement() {
noLabels.dec();
Expand All @@ -57,7 +58,7 @@ public void testDecrement() {
noLabels.labels().dec();
assertEquals(-8.0, getValue(), .001);
}

@Test
public void testSet() {
noLabels.set(42);
Expand Down Expand Up @@ -93,8 +94,18 @@ public void run() {
//no op
}
});
assertEquals(10, getValue(), .001);
assertEquals(10, elapsed, .001);

int result = noLabels.setToTime(new Callable<Integer>() {
@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);
Expand All @@ -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});
}
Expand All @@ -126,7 +137,7 @@ public void testLabels() {
public void testCollect() {
labels.labels("a").inc();
List<Collector.MetricFamilySamples> mfs = labels.collect();

ArrayList<Collector.MetricFamilySamples.Sample> samples = new ArrayList<Collector.MetricFamilySamples.Sample>();
ArrayList<String> labelNames = new ArrayList<String>();
labelNames.add("l");
Expand Down
23 changes: 16 additions & 7 deletions simpleclient/src/test/java/io/prometheus/client/HistogramTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -119,19 +120,27 @@ public void run() {
});
assertEquals(10, elapsed, .001);

int result = noLabels.time(new Callable<Integer>() {
@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});
}
Expand Down Expand Up @@ -166,7 +175,7 @@ public void testLeLabelThrows() {
public void testCollect() {
labels.labels("a").observe(2);
List<Collector.MetricFamilySamples> mfs = labels.collect();

ArrayList<Collector.MetricFamilySamples.Sample> samples = new ArrayList<Collector.MetricFamilySamples.Sample>();
ArrayList<String> labelNames = new ArrayList<String>();
labelNames.add("l");
Expand Down
17 changes: 13 additions & 4 deletions simpleclient/src/test/java/io/prometheus/client/SummaryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -124,19 +125,27 @@ public void run() {
});
assertEquals(10, elapsed, .001);

int result = noLabels.time(new Callable<Integer>() {
@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});
}
Expand Down