From 5c0d1e7ee22897b87f73358caee43d95dcd07408 Mon Sep 17 00:00:00 2001 From: Brian Brazil Date: Fri, 15 Jan 2021 14:47:38 +0000 Subject: [PATCH] Add _created for direct instrumentation. Signed-off-by: Brian Brazil --- .../prometheus/client/CollectorRegistry.java | 8 ++++++ .../java/io/prometheus/client/Counter.java | 8 ++++++ .../java/io/prometheus/client/Histogram.java | 8 ++++-- .../java/io/prometheus/client/Summary.java | 8 ++++-- .../io/prometheus/client/CounterTest.java | 1 + .../io/prometheus/client/HistogramTest.java | 2 ++ .../io/prometheus/client/SummaryTest.java | 2 ++ .../common/TextFormatOpenMetricsTest.java | 9 ++++--- .../exporter/common/TextFormatTest.java | 26 +++++++++++++++---- 9 files changed, 60 insertions(+), 12 deletions(-) diff --git a/simpleclient/src/main/java/io/prometheus/client/CollectorRegistry.java b/simpleclient/src/main/java/io/prometheus/client/CollectorRegistry.java index b7bc0284b..590beeba5 100644 --- a/simpleclient/src/main/java/io/prometheus/client/CollectorRegistry.java +++ b/simpleclient/src/main/java/io/prometheus/client/CollectorRegistry.java @@ -113,12 +113,20 @@ private List 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: diff --git a/simpleclient/src/main/java/io/prometheus/client/Counter.java b/simpleclient/src/main/java/io/prometheus/client/Counter.java index 27a7d5627..900812b16 100644 --- a/simpleclient/src/main/java/io/prometheus/client/Counter.java +++ b/simpleclient/src/main/java/io/prometheus/client/Counter.java @@ -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. */ @@ -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. @@ -163,6 +170,7 @@ public List collect() { List samples = new ArrayList(children.size()); for(Map.Entry, 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); } diff --git a/simpleclient/src/main/java/io/prometheus/client/Histogram.java b/simpleclient/src/main/java/io/prometheus/client/Histogram.java index 1d8b1a2fa..30595e187 100644 --- a/simpleclient/src/main/java/io/prometheus/client/Histogram.java +++ b/simpleclient/src/main/java/io/prometheus/client/Histogram.java @@ -231,10 +231,12 @@ public E time(Callable 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; } } @@ -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(); /** @@ -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); } } @@ -337,6 +340,7 @@ public List 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); diff --git a/simpleclient/src/main/java/io/prometheus/client/Summary.java b/simpleclient/src/main/java/io/prometheus/client/Summary.java index 4d79e558a..9d2aca3f8 100644 --- a/simpleclient/src/main/java/io/prometheus/client/Summary.java +++ b/simpleclient/src/main/java/io/prometheus/client/Summary.java @@ -239,11 +239,13 @@ public static class Value { public final double count; public final double sum; public final SortedMap quantiles; + public final long created; - private Value(double count, double sum, List quantiles, TimeWindowQuantiles quantileValues) { + private Value(double count, double sum, List quantiles, TimeWindowQuantiles quantileValues, long created) { this.count = count; this.sum = sum; this.quantiles = Collections.unmodifiableSortedMap(snapshot(quantiles, quantileValues)); + this.created = created; } private SortedMap snapshot(List quantiles, TimeWindowQuantiles quantileValues) { @@ -263,6 +265,7 @@ private SortedMap snapshot(List quantiles, TimeWindowQ private final DoubleAdder sum = new DoubleAdder(); private final List quantiles; private final TimeWindowQuantiles quantileValues; + private final long created = System.currentTimeMillis(); private Child(List quantiles, long maxAgeSeconds, int ageBuckets) { this.quantiles = quantiles; @@ -297,7 +300,7 @@ public Timer startTimer() { * Warning: 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); } } @@ -360,6 +363,7 @@ public List 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); diff --git a/simpleclient/src/test/java/io/prometheus/client/CounterTest.java b/simpleclient/src/test/java/io/prometheus/client/CounterTest.java index 6dc089d6a..2d7ba0735 100644 --- a/simpleclient/src/test/java/io/prometheus/client/CounterTest.java +++ b/simpleclient/src/test/java/io/prometheus/client/CounterTest.java @@ -97,6 +97,7 @@ public void testCollect() { ArrayList labelValues = new ArrayList(); 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()); diff --git a/simpleclient/src/test/java/io/prometheus/client/HistogramTest.java b/simpleclient/src/test/java/io/prometheus/client/HistogramTest.java index 1a09a8939..15ac00a76 100644 --- a/simpleclient/src/test/java/io/prometheus/client/HistogramTest.java +++ b/simpleclient/src/test/java/io/prometheus/client/HistogramTest.java @@ -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()); diff --git a/simpleclient/src/test/java/io/prometheus/client/SummaryTest.java b/simpleclient/src/test/java/io/prometheus/client/SummaryTest.java index 06e1f9d9b..c6f70506d 100644 --- a/simpleclient/src/test/java/io/prometheus/client/SummaryTest.java +++ b/simpleclient/src/test/java/io/prometheus/client/SummaryTest.java @@ -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()); @@ -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()); diff --git a/simpleclient_common/src/test/java/io/prometheus/client/exporter/common/TextFormatOpenMetricsTest.java b/simpleclient_common/src/test/java/io/prometheus/client/exporter/common/TextFormatOpenMetricsTest.java index c8e6f4848..3d013ee5c 100644 --- a/simpleclient_common/src/test/java/io/prometheus/client/exporter/common/TextFormatOpenMetricsTest.java +++ b/simpleclient_common/src/test/java/io/prometheus/client/exporter/common/TextFormatOpenMetricsTest.java @@ -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 @@ -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 @@ -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 diff --git a/simpleclient_common/src/test/java/io/prometheus/client/exporter/common/TextFormatTest.java b/simpleclient_common/src/test/java/io/prometheus/client/exporter/common/TextFormatTest.java index a1b367f06..98fbabfb2 100644 --- a/simpleclient_common/src/test/java/io/prometheus/client/exporter/common/TextFormatTest.java +++ b/simpleclient_common/src/test/java/io/prometheus/client/exporter/common/TextFormatTest.java @@ -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 @@ -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")); } @@ -107,7 +115,7 @@ public List collect() { return mfs; } } - + new CustomCollector().register(registry); TextFormat.write004(writer, registry.metricFamilySamples()); assertEquals("# HELP nolabels help\n" @@ -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 @@ -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