Skip to content

Commit

Permalink
Add _created for direct instrumentation.
Browse files Browse the repository at this point in the history
Signed-off-by: Brian Brazil <[email protected]>
  • Loading branch information
brian-brazil committed Jan 18, 2021
1 parent 801f91e commit 5c0d1e7
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,20 @@ private List<String> collectorNames(Collector m) {
case SUMMARY:
names.add(family.name + "_count");
names.add(family.name + "_sum");
names.add(family.name + "_created");
names.add(family.name);
break;
case HISTOGRAM:
names.add(family.name + "_count");
names.add(family.name + "_sum");
names.add(family.name + "_bucket");
names.add(family.name + "_created");
names.add(family.name);
break;
case GAUGE_HISTOGRAM:
names.add(family.name + "_gcount");
names.add(family.name + "_gsum");
names.add(family.name + "_bucket");
names.add(family.name);
break;
default:
Expand Down
8 changes: 8 additions & 0 deletions simpleclient/src/main/java/io/prometheus/client/Counter.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ protected Child newChild() {
*/
public static class Child {
private final DoubleAdder value = new DoubleAdder();
private final long created = System.currentTimeMillis();
/**
* Increment the counter by 1.
*/
Expand All @@ -134,6 +135,12 @@ public void inc(double amt) {
public double get() {
return value.sum();
}
/**
* Get the created time of the counter in milliseconds.
*/
public long created() {
return created;
}
}

// Convenience methods.
Expand Down Expand Up @@ -163,6 +170,7 @@ 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 + "_total", labelNames, c.getKey(), c.getValue().get()));
samples.add(new MetricFamilySamples.Sample(fullname + "_created", labelNames, c.getKey(), c.getValue().created() / 1000.0));
}
return familySamplesList(Type.COUNTER, samples);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,10 +231,12 @@ public <E> E time(Callable<E> timeable) {
public static class Value {
public final double sum;
public final double[] buckets;
public final long created;

public Value(double sum, double[] buckets) {
public Value(double sum, double[] buckets, long created) {
this.sum = sum;
this.buckets = buckets;
this.created = created;
}
}

Expand All @@ -248,6 +250,7 @@ private Child(double[] buckets) {
private final double[] upperBounds;
private final DoubleAdder[] cumulativeCounts;
private final DoubleAdder sum = new DoubleAdder();
private final long created = System.currentTimeMillis();


/**
Expand Down Expand Up @@ -283,7 +286,7 @@ public Value get() {
acc += cumulativeCounts[i].sum();
buckets[i] = acc;
}
return new Value(sum.sum(), buckets);
return new Value(sum.sum(), buckets, created);
}
}

Expand Down Expand Up @@ -337,6 +340,7 @@ public List<MetricFamilySamples> collect() {
}
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 + "_created", labelNames, c.getKey(), v.created / 1000.0));
}

return familySamplesList(Type.HISTOGRAM, samples);
Expand Down
8 changes: 6 additions & 2 deletions simpleclient/src/main/java/io/prometheus/client/Summary.java
Original file line number Diff line number Diff line change
Expand Up @@ -239,11 +239,13 @@ public static class Value {
public final double count;
public final double sum;
public final SortedMap<Double, Double> quantiles;
public final long created;

private Value(double count, double sum, List<Quantile> quantiles, TimeWindowQuantiles quantileValues) {
private Value(double count, double sum, List<Quantile> quantiles, TimeWindowQuantiles quantileValues, long created) {
this.count = count;
this.sum = sum;
this.quantiles = Collections.unmodifiableSortedMap(snapshot(quantiles, quantileValues));
this.created = created;
}

private SortedMap<Double, Double> snapshot(List<Quantile> quantiles, TimeWindowQuantiles quantileValues) {
Expand All @@ -263,6 +265,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 final long created = System.currentTimeMillis();

private Child(List<Quantile> quantiles, long maxAgeSeconds, int ageBuckets) {
this.quantiles = quantiles;
Expand Down Expand Up @@ -297,7 +300,7 @@ public Timer startTimer() {
* <em>Warning:</em> The definition of {@link Value} is subject to change.
*/
public Value get() {
return new Value(count.sum(), sum.sum(), quantiles, quantileValues);
return new Value(count.sum(), sum.sum(), quantiles, quantileValues, created);
}
}

Expand Down Expand Up @@ -360,6 +363,7 @@ public List<MetricFamilySamples> collect() {
}
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 + "_created", labelNames, c.getKey(), v.created / 1000.0));
}

return familySamplesList(Type.SUMMARY, samples);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ public void testCollect() {
ArrayList<String> labelValues = new ArrayList<String>();
labelValues.add("a");
samples.add(new Collector.MetricFamilySamples.Sample("labels_seconds_total", labelNames, labelValues, 1.0));
samples.add(new Collector.MetricFamilySamples.Sample("labels_seconds_created", labelNames, labelValues, labels.labels("a").created() / 1000.0));
Collector.MetricFamilySamples mfsFixture = new Collector.MetricFamilySamples("labels_seconds", "seconds", Collector.Type.COUNTER, "help", samples);

assertEquals(1, mfs.size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,8 @@ public void testCollect() {
}
samples.add(new Collector.MetricFamilySamples.Sample("labels_count", labelNames, labelValues, 1.0));
samples.add(new Collector.MetricFamilySamples.Sample("labels_sum", labelNames, labelValues, 2.0));
samples.add(new Collector.MetricFamilySamples.Sample("labels_created", labelNames, labelValues, labels.labels("a").get().created / 1000.0));

Collector.MetricFamilySamples mfsFixture = new Collector.MetricFamilySamples("labels", Collector.Type.HISTOGRAM, "help", samples);

assertEquals(1, mfs.size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ public void testCollect() {
labelValues.add("a");
samples.add(new Collector.MetricFamilySamples.Sample("labels_count", labelNames, labelValues, 1.0));
samples.add(new Collector.MetricFamilySamples.Sample("labels_sum", labelNames, labelValues, 2.0));
samples.add(new Collector.MetricFamilySamples.Sample("labels_created", labelNames, labelValues, labels.labels("a").get().created / 1000.0));
Collector.MetricFamilySamples mfsFixture = new Collector.MetricFamilySamples("labels", Collector.Type.SUMMARY, "help", samples);

assertEquals(1, mfs.size());
Expand All @@ -200,6 +201,7 @@ public void testCollectWithQuantiles() {
samples.add(new Collector.MetricFamilySamples.Sample("labels_and_quantiles", asList("l", "quantile"), asList("a", "0.99"), 2.0));
samples.add(new Collector.MetricFamilySamples.Sample("labels_and_quantiles_count", asList("l"), asList("a"), 1.0));
samples.add(new Collector.MetricFamilySamples.Sample("labels_and_quantiles_sum", asList("l"), asList("a"), 2.0));
samples.add(new Collector.MetricFamilySamples.Sample("labels_and_quantiles_created", asList("l"), asList("a"), labelsAndQuantiles.labels("a").get().created / 1000.0));
Collector.MetricFamilySamples mfsFixture = new Collector.MetricFamilySamples("labels_and_quantiles", Collector.Type.SUMMARY, "help", samples);

assertEquals(1, mfs.size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ public void testCounterOutput() throws IOException {
assertEquals("# TYPE nolabels counter\n"
+ "# HELP nolabels help\n"
+ "nolabels_total 1.0\n"
+ "# EOF\n", writer.toString());
+ "nolabels_created 1234.0\n"
+ "# EOF\n", writer.toString().replaceAll("_created [0-9E.]+", "_created 1234.0"));
}

@Test
Expand Down Expand Up @@ -117,7 +118,8 @@ public void testSummaryOutput() throws IOException {
+ "# HELP nolabels help\n"
+ "nolabels_count 1.0\n"
+ "nolabels_sum 2.0\n"
+ "# EOF\n", writer.toString());
+ "nolabels_created 1234.0\n"
+ "# EOF\n", writer.toString().replaceAll("_created [0-9E.]+", "_created 1234.0"));
}

@Test
Expand All @@ -135,7 +137,8 @@ public void testSummaryOutputWithQuantiles() throws IOException {
+ "labelsAndQuantiles{l=\"a\",quantile=\"0.99\"} 2.0\n"
+ "labelsAndQuantiles_count{l=\"a\"} 1.0\n"
+ "labelsAndQuantiles_sum{l=\"a\"} 2.0\n"
+ "# EOF\n", writer.toString());
+ "labelsAndQuantiles_created{l=\"a\"} 1234.0\n"
+ "# EOF\n", writer.toString().replaceAll("(_created\\{.*\\}) [0-9E.]+", "$1 1234.0"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ public void testCounterOutput() throws IOException {
TextFormat.write004(writer, registry.metricFamilySamples());
assertEquals("# HELP nolabels_total help\n"
+ "# TYPE nolabels_total counter\n"
+ "nolabels_total 1.0\n", writer.toString());
+ "nolabels_total 1.0\n"
+ "# HELP nolabels_created help\n"
+ "# TYPE nolabels_created gauge\n"
+ "nolabels_created 1234.0\n",
writer.toString().replaceAll("_created [0-9E.]+", "_created 1234.0"));
}

@Test
Expand All @@ -65,7 +69,11 @@ public void testCounterWithTotalOutput() throws IOException {
TextFormat.write004(writer, registry.metricFamilySamples());
assertEquals("# HELP nolabels_total help\n"
+ "# TYPE nolabels_total counter\n"
+ "nolabels_total 1.0\n", writer.toString());
+ "nolabels_total 1.0\n"
+ "# HELP nolabels_created help\n"
+ "# TYPE nolabels_created gauge\n"
+ "nolabels_created 1234.0\n",
writer.toString().replaceAll("_created [0-9E.]+", "_created 1234.0"));
}


Expand Down Expand Up @@ -107,7 +115,7 @@ public List<MetricFamilySamples> collect() {
return mfs;
}
}

new CustomCollector().register(registry);
TextFormat.write004(writer, registry.metricFamilySamples());
assertEquals("# HELP nolabels help\n"
Expand All @@ -123,7 +131,11 @@ public void testSummaryOutput() throws IOException {
assertEquals("# HELP nolabels help\n"
+ "# TYPE nolabels summary\n"
+ "nolabels_count 1.0\n"
+ "nolabels_sum 2.0\n", writer.toString());
+ "nolabels_sum 2.0\n"
+ "# HELP nolabels_created help\n"
+ "# TYPE nolabels_created gauge\n"
+ "nolabels_created 1234.0\n",
writer.toString().replaceAll("_created [0-9E.]+", "_created 1234.0"));
}

@Test
Expand All @@ -140,7 +152,11 @@ public void testSummaryOutputWithQuantiles() throws IOException {
+ "labelsAndQuantiles{l=\"a\",quantile=\"0.9\",} 2.0\n"
+ "labelsAndQuantiles{l=\"a\",quantile=\"0.99\",} 2.0\n"
+ "labelsAndQuantiles_count{l=\"a\",} 1.0\n"
+ "labelsAndQuantiles_sum{l=\"a\",} 2.0\n", writer.toString());
+ "labelsAndQuantiles_sum{l=\"a\",} 2.0\n"
+ "# HELP labelsAndQuantiles_created help\n"
+ "# TYPE labelsAndQuantiles_created gauge\n"
+ "labelsAndQuantiles_created{l=\"a\",} 1234.0\n",
writer.toString().replaceAll("(_created\\{.*\\}) [0-9E.]+", "$1 1234.0"));
}

@Test
Expand Down

0 comments on commit 5c0d1e7

Please sign in to comment.