Skip to content

Commit

Permalink
Added support to define an externally available timestamp to the metrics
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Wiechmann <cwiechmann@axway.com>
  • Loading branch information
Chris Wiechmann committed Aug 3, 2019
1 parent fbc3fc4 commit 5bce97e
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 8 deletions.
17 changes: 16 additions & 1 deletion simpleclient/src/main/java/io/prometheus/client/Counter.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ protected Child newChild() {
*/
public static class Child {
private final DoubleAdder value = new DoubleAdder();

private Long timestampMs;
/**
* Increment the counter by 1.
*/
Expand All @@ -130,6 +132,19 @@ public void inc(double amt) {
public double get() {
return value.sum();
}
/**
* Get the optionally defined timestamp for the counter.
*/
public Long getTimestampMs() {
return timestampMs;
}
/**
* Optionally sets an external timestamp for the counter.
*/
public Child setTimestampMs(Long timestampMs) {
this.timestampMs = timestampMs;
return this;
}
}

// Convenience methods.
Expand Down Expand Up @@ -158,7 +173,7 @@ public double get() {
public List<MetricFamilySamples> collect() {
List<MetricFamilySamples.Sample> samples = new ArrayList<MetricFamilySamples.Sample>(children.size());
for(Map.Entry<List<String>, Child> c: children.entrySet()) {
samples.add(new MetricFamilySamples.Sample(fullname, labelNames, c.getKey(), c.getValue().get()));
samples.add(new MetricFamilySamples.Sample(fullname, labelNames, c.getKey(), c.getValue().get(), c.getValue().getTimestampMs()));
}
return familySamplesList(Type.COUNTER, samples);
}
Expand Down
16 changes: 15 additions & 1 deletion simpleclient/src/main/java/io/prometheus/client/Gauge.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ public void close() {
*/
public static class Child {
private final DoubleAdder value = new DoubleAdder();
private Long timestampMs = null;

static TimeProvider timeProvider = new TimeProvider();

Expand Down Expand Up @@ -238,6 +239,19 @@ public double get() {
return value.sum();
}
}
/**
* Get the optionally defined timestamp for the gauge.
*/
public Long getTimestampMs() {
return timestampMs;
}
/**
* Optionally sets an external timestamp for the gauge.
*/
public Child setTimestampMs(Long timestampMs) {
this.timestampMs = timestampMs;
return this;
}
}

// Convenience methods.
Expand Down Expand Up @@ -321,7 +335,7 @@ public double get() {
public List<MetricFamilySamples> collect() {
List<MetricFamilySamples.Sample> samples = new ArrayList<MetricFamilySamples.Sample>(children.size());
for(Map.Entry<List<String>, Child> c: children.entrySet()) {
samples.add(new MetricFamilySamples.Sample(fullname, labelNames, c.getKey(), c.getValue().get()));
samples.add(new MetricFamilySamples.Sample(fullname, labelNames, c.getKey(), c.getValue().get(), c.getValue().getTimestampMs()));
}
return familySamplesList(Type.GAUGE, samples);
}
Expand Down
20 changes: 17 additions & 3 deletions simpleclient/src/main/java/io/prometheus/client/Histogram.java
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ private Child(double[] buckets) {
private final double[] upperBounds;
private final DoubleAdder[] cumulativeCounts;
private final DoubleAdder sum = new DoubleAdder();
private Long timestampMs;


/**
Expand Down Expand Up @@ -283,6 +284,19 @@ public Value get() {
}
return new Value(sum.sum(), buckets);
}
/**
* Get the optionally defined timestamp for the histogram.
*/
public Long getTimestampMs() {
return timestampMs;
}
/**
* Optionally sets an external timestamp for the histogram.
*/
public Child setTimestampMs(Long timestampMs) {
this.timestampMs = timestampMs;
return this;
}
}

// Convenience methods.
Expand Down Expand Up @@ -331,10 +345,10 @@ public List<MetricFamilySamples> collect() {
for (int i = 0; i < v.buckets.length; ++i) {
List<String> labelValuesWithLe = new ArrayList<String>(c.getKey());
labelValuesWithLe.add(doubleToGoString(buckets[i]));
samples.add(new MetricFamilySamples.Sample(fullname + "_bucket", labelNamesWithLe, labelValuesWithLe, v.buckets[i]));
samples.add(new MetricFamilySamples.Sample(fullname + "_bucket", labelNamesWithLe, labelValuesWithLe, v.buckets[i], c.getValue().getTimestampMs()));
}
samples.add(new MetricFamilySamples.Sample(fullname + "_count", labelNames, c.getKey(), v.buckets[buckets.length-1]));
samples.add(new MetricFamilySamples.Sample(fullname + "_sum", labelNames, c.getKey(), v.sum));
samples.add(new MetricFamilySamples.Sample(fullname + "_count", labelNames, c.getKey(), v.buckets[buckets.length-1], c.getValue().getTimestampMs()));
samples.add(new MetricFamilySamples.Sample(fullname + "_sum", labelNames, c.getKey(), v.sum, c.getValue().getTimestampMs()));
}

return familySamplesList(Type.HISTOGRAM, samples);
Expand Down
20 changes: 17 additions & 3 deletions simpleclient/src/main/java/io/prometheus/client/Summary.java
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ private SortedMap<Double, Double> snapshot(List<Quantile> quantiles, TimeWindowQ
private final DoubleAdder sum = new DoubleAdder();
private final List<Quantile> quantiles;
private final TimeWindowQuantiles quantileValues;
private Long timestampMs;

private Child(List<Quantile> quantiles, long maxAgeSeconds, int ageBuckets) {
this.quantiles = quantiles;
Expand Down Expand Up @@ -297,6 +298,19 @@ public Timer startTimer() {
public Value get() {
return new Value(count.sum(), sum.sum(), quantiles, quantileValues);
}
/**
* If set, get the timestamp of the Summary.
*/
public Long getTimestampMs() {
return timestampMs;
}
/**
* Optionally sets an external timestamp for the summary.
*/
public Child setTimestampMs(Long timestampMs) {
this.timestampMs = timestampMs;
return this;
}
}

// Convenience methods.
Expand Down Expand Up @@ -354,10 +368,10 @@ public List<MetricFamilySamples> collect() {
for(Map.Entry<Double, Double> q : v.quantiles.entrySet()) {
List<String> labelValuesWithQuantile = new ArrayList<String>(c.getKey());
labelValuesWithQuantile.add(doubleToGoString(q.getKey()));
samples.add(new MetricFamilySamples.Sample(fullname, labelNamesWithQuantile, labelValuesWithQuantile, q.getValue()));
samples.add(new MetricFamilySamples.Sample(fullname, labelNamesWithQuantile, labelValuesWithQuantile, q.getValue(), c.getValue().getTimestampMs()));
}
samples.add(new MetricFamilySamples.Sample(fullname + "_count", labelNames, c.getKey(), v.count));
samples.add(new MetricFamilySamples.Sample(fullname + "_sum", labelNames, c.getKey(), v.sum));
samples.add(new MetricFamilySamples.Sample(fullname + "_count", labelNames, c.getKey(), v.count, c.getValue().getTimestampMs()));
samples.add(new MetricFamilySamples.Sample(fullname + "_sum", labelNames, c.getKey(), v.sum, c.getValue().getTimestampMs()));
}

return familySamplesList(Type.SUMMARY, samples);
Expand Down

0 comments on commit 5bce97e

Please sign in to comment.