From eb2d8d10a89097c873b6f649c38d9faf62922140 Mon Sep 17 00:00:00 2001 From: Felipe Conde Date: Thu, 1 Feb 2018 18:07:04 +0100 Subject: [PATCH 01/19] Added optional "timestamp" value as part of metric sample --- .../main/java/io/prometheus/client/Collector.java | 14 +++++++++++--- .../client/exporter/common/TextFormat.java | 4 ++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/simpleclient/src/main/java/io/prometheus/client/Collector.java b/simpleclient/src/main/java/io/prometheus/client/Collector.java index 9dd82efe1..02e7528fb 100644 --- a/simpleclient/src/main/java/io/prometheus/client/Collector.java +++ b/simpleclient/src/main/java/io/prometheus/client/Collector.java @@ -77,12 +77,18 @@ public static class Sample { public final List labelNames; public final List labelValues; // Must have same length as labelNames. public final double value; + public final Long timestamp; - public Sample(String name, List labelNames, List labelValues, double value) { + public Sample(String name, List labelNames, List labelValues, double value, Long timestamp) { this.name = name; this.labelNames = labelNames; this.labelValues = labelValues; this.value = value; + this.timestamp = timestamp; + } + + public Sample(String name, List labelNames, List labelValues, double value) { + this(name, labelNames, labelValues, value, null); } @Override @@ -92,7 +98,8 @@ public boolean equals(Object obj) { } Sample other = (Sample) obj; return other.name.equals(name) && other.labelNames.equals(labelNames) - && other.labelValues.equals(labelValues) && other.value == value; + && other.labelValues.equals(labelValues) && other.value == value + && other.timestamp.equals(timestamp); } @Override @@ -103,13 +110,14 @@ public int hashCode() { hash = 37 * hash + labelValues.hashCode(); long d = Double.doubleToLongBits(value); hash = 37 * hash + (int)(d ^ (d >>> 32)); + hash = 37 * hash + timestamp.hashCode(); return hash; } @Override public String toString() { return "Name: " + name + " LabelNames: " + labelNames + " labelValues: " + labelValues + - " Value: " + value; + " Value: " + value + " Timestamp: " + timestamp; } } } diff --git a/simpleclient_common/src/main/java/io/prometheus/client/exporter/common/TextFormat.java b/simpleclient_common/src/main/java/io/prometheus/client/exporter/common/TextFormat.java index 5ebcd6b84..5b61cfd2d 100644 --- a/simpleclient_common/src/main/java/io/prometheus/client/exporter/common/TextFormat.java +++ b/simpleclient_common/src/main/java/io/prometheus/client/exporter/common/TextFormat.java @@ -46,6 +46,10 @@ public static void write004(Writer writer, Enumeration Date: Thu, 1 Feb 2018 18:43:15 +0100 Subject: [PATCH 02/19] Method "equals" was wrong --- .../src/main/java/io/prometheus/client/Collector.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/simpleclient/src/main/java/io/prometheus/client/Collector.java b/simpleclient/src/main/java/io/prometheus/client/Collector.java index 02e7528fb..7b39bc397 100644 --- a/simpleclient/src/main/java/io/prometheus/client/Collector.java +++ b/simpleclient/src/main/java/io/prometheus/client/Collector.java @@ -97,9 +97,16 @@ public boolean equals(Object obj) { return false; } Sample other = (Sample) obj; + boolean timestampEquals = true; + if ((timestamp != null && other.timestamp == null) || (timestamp == null && other.timestamp != null)) { + timestampEquals = false; + } + if (timestamp != null && other.timestamp != null) { + timestampEquals = timestamp.equals(other.timestamp); + } return other.name.equals(name) && other.labelNames.equals(labelNames) && other.labelValues.equals(labelValues) && other.value == value - && other.timestamp.equals(timestamp); + && timestampEquals; } @Override From 35105133797f57ff0f63515e4b2c7e85e8e5812a Mon Sep 17 00:00:00 2001 From: Felipe Conde Date: Fri, 2 Feb 2018 13:26:16 +0100 Subject: [PATCH 03/19] changed "tabs" for "spaces" --- .../java/io/prometheus/client/exporter/common/TextFormat.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/simpleclient_common/src/main/java/io/prometheus/client/exporter/common/TextFormat.java b/simpleclient_common/src/main/java/io/prometheus/client/exporter/common/TextFormat.java index 5b61cfd2d..799eadbe4 100644 --- a/simpleclient_common/src/main/java/io/prometheus/client/exporter/common/TextFormat.java +++ b/simpleclient_common/src/main/java/io/prometheus/client/exporter/common/TextFormat.java @@ -47,8 +47,8 @@ public static void write004(Writer writer, Enumeration Date: Fri, 2 Feb 2018 13:27:07 +0100 Subject: [PATCH 04/19] Added extra check for "null" values --- simpleclient/src/main/java/io/prometheus/client/Collector.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/simpleclient/src/main/java/io/prometheus/client/Collector.java b/simpleclient/src/main/java/io/prometheus/client/Collector.java index 7b39bc397..61c12887e 100644 --- a/simpleclient/src/main/java/io/prometheus/client/Collector.java +++ b/simpleclient/src/main/java/io/prometheus/client/Collector.java @@ -117,7 +117,8 @@ public int hashCode() { hash = 37 * hash + labelValues.hashCode(); long d = Double.doubleToLongBits(value); hash = 37 * hash + (int)(d ^ (d >>> 32)); - hash = 37 * hash + timestamp.hashCode(); + if (timestamp != null) + hash = 37 * hash + timestamp.hashCode(); return hash; } From 2acdc292a2e0c193cfb950d223926d122175fa9d Mon Sep 17 00:00:00 2001 From: Felipe Conde Date: Fri, 2 Feb 2018 13:42:02 +0100 Subject: [PATCH 05/19] changed "tabs" for "spaces" --- .../src/main/java/io/prometheus/client/Collector.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/simpleclient/src/main/java/io/prometheus/client/Collector.java b/simpleclient/src/main/java/io/prometheus/client/Collector.java index 61c12887e..bee1659bb 100644 --- a/simpleclient/src/main/java/io/prometheus/client/Collector.java +++ b/simpleclient/src/main/java/io/prometheus/client/Collector.java @@ -106,7 +106,7 @@ public boolean equals(Object obj) { } return other.name.equals(name) && other.labelNames.equals(labelNames) && other.labelValues.equals(labelValues) && other.value == value - && timestampEquals; + && timestampEquals; } @Override @@ -118,7 +118,7 @@ public int hashCode() { long d = Double.doubleToLongBits(value); hash = 37 * hash + (int)(d ^ (d >>> 32)); if (timestamp != null) - hash = 37 * hash + timestamp.hashCode(); + hash = 37 * hash + timestamp.hashCode(); return hash; } From b7e20df3e9fec88ae03ab6fd3b2f4f402ec8e8e7 Mon Sep 17 00:00:00 2001 From: Felipe Conde Date: Mon, 5 Feb 2018 12:27:16 +0100 Subject: [PATCH 06/19] Added "timestamp" field into required classes --- .../java/io/prometheus/client/Counter.java | 18 ++++++++++++++++- .../main/java/io/prometheus/client/Gauge.java | 20 ++++++++++++++++++- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/simpleclient/src/main/java/io/prometheus/client/Counter.java b/simpleclient/src/main/java/io/prometheus/client/Counter.java index 180a8d5c9..15f54fc8a 100644 --- a/simpleclient/src/main/java/io/prometheus/client/Counter.java +++ b/simpleclient/src/main/java/io/prometheus/client/Counter.java @@ -108,6 +108,16 @@ protected Child newChild() { */ public static class Child { private final DoubleAdder value = new DoubleAdder(); + /** + * Milliseconds since epoch. Optional value. + */ + private Long timestamp; + /** + * Set the optional value for time stamp in epoch format. + */ + public void setTimestamp(Long timestamp) { + this.timestamp = timestamp; + } /** * Increment the counter by 1. */ @@ -130,6 +140,12 @@ public void inc(double amt) { public double get() { return value.sum(); } + /** + * Get the optional value for time stamp in epoch format. It could be null. + */ + public Long getTimestamp() { + return timestamp; + } } // Convenience methods. @@ -158,7 +174,7 @@ public double get() { public List collect() { List samples = new ArrayList(children.size()); for(Map.Entry, 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().getTimestamp())); } return familySamplesList(Type.COUNTER, samples); } diff --git a/simpleclient/src/main/java/io/prometheus/client/Gauge.java b/simpleclient/src/main/java/io/prometheus/client/Gauge.java index 3473cedf0..25281b166 100644 --- a/simpleclient/src/main/java/io/prometheus/client/Gauge.java +++ b/simpleclient/src/main/java/io/prometheus/client/Gauge.java @@ -134,8 +134,19 @@ public void close() { */ public static class Child { private final DoubleAdder value = new DoubleAdder(); + /** + * Milliseconds since epoch. Optional value. + */ + private Long timestamp; static TimeProvider timeProvider = new TimeProvider(); + + /** + * Set the optional value for time stamp in epoch format, + */ + public void setTimestamp(Long timestamp) { + this.timestamp = timestamp; + } /** * Increment the gauge by 1. */ @@ -218,6 +229,13 @@ public double get() { return value.sum(); } } + + /** + * Get the optional value for time stamp in epoch format. It could be null. + */ + public Long getTimestamp() { + return timestamp; + } } // Convenience methods. @@ -292,7 +310,7 @@ public double get() { public List collect() { List samples = new ArrayList(children.size()); for(Map.Entry, 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().getTimestamp())); } return familySamplesList(Type.GAUGE, samples); } From 70fd6db79b73d53f9e8e5d701f279ae488f9b4c8 Mon Sep 17 00:00:00 2001 From: Felipe Conde Date: Mon, 5 Feb 2018 12:27:49 +0100 Subject: [PATCH 07/19] Fixed code format. --- .../src/main/java/io/prometheus/client/Collector.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/simpleclient/src/main/java/io/prometheus/client/Collector.java b/simpleclient/src/main/java/io/prometheus/client/Collector.java index bee1659bb..c5044b030 100644 --- a/simpleclient/src/main/java/io/prometheus/client/Collector.java +++ b/simpleclient/src/main/java/io/prometheus/client/Collector.java @@ -97,13 +97,15 @@ public boolean equals(Object obj) { return false; } Sample other = (Sample) obj; + boolean timestampEquals = true; if ((timestamp != null && other.timestamp == null) || (timestamp == null && other.timestamp != null)) { - timestampEquals = false; + timestampEquals = false; } if (timestamp != null && other.timestamp != null) { - timestampEquals = timestamp.equals(other.timestamp); + timestampEquals = other.timestamp.equals(timestamp); } + return other.name.equals(name) && other.labelNames.equals(labelNames) && other.labelValues.equals(labelValues) && other.value == value && timestampEquals; @@ -117,8 +119,9 @@ public int hashCode() { hash = 37 * hash + labelValues.hashCode(); long d = Double.doubleToLongBits(value); hash = 37 * hash + (int)(d ^ (d >>> 32)); - if (timestamp != null) + if (timestamp != null) { hash = 37 * hash + timestamp.hashCode(); + } return hash; } From 7ff6b0658d19d98000ac9067044561eab7eca49e Mon Sep 17 00:00:00 2001 From: Felipe Conde Date: Mon, 5 Feb 2018 12:28:11 +0100 Subject: [PATCH 08/19] Added unit tests --- .../io/prometheus/client/CounterTest.java | 21 +++++++++++++++++++ .../java/io/prometheus/client/GaugeTest.java | 21 +++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/simpleclient/src/test/java/io/prometheus/client/CounterTest.java b/simpleclient/src/test/java/io/prometheus/client/CounterTest.java index 16d025f73..fa8034e4d 100644 --- a/simpleclient/src/test/java/io/prometheus/client/CounterTest.java +++ b/simpleclient/src/test/java/io/prometheus/client/CounterTest.java @@ -82,4 +82,25 @@ public void testCollect() { assertEquals(mfsFixture, mfs.get(0)); } + @Test + public void testCollectWithTimestamp() { + Long now = System.currentTimeMillis(); + labels.labels("a").inc(); + labels.labels("a").setTimestamp(now); + List mfs = labels.collect(); + + ArrayList samples = new ArrayList(); + ArrayList labelNames = new ArrayList(); + labelNames.add("l"); + ArrayList labelValues = new ArrayList(); + labelValues.add("a"); + samples.add(new Collector.MetricFamilySamples.Sample("labels", labelNames, labelValues, 1.0, now)); + Collector.MetricFamilySamples mfsFixture = new Collector.MetricFamilySamples("labels", Collector.Type.COUNTER, "help", samples); + + assertEquals(1, mfs.size()); + assertEquals(mfsFixture, mfs.get(0)); + Long timestamp = mfs.get(0).samples.get(0).timestamp; + assertEquals(now, timestamp); + } + } diff --git a/simpleclient/src/test/java/io/prometheus/client/GaugeTest.java b/simpleclient/src/test/java/io/prometheus/client/GaugeTest.java index 967c8a87b..f4f385cae 100644 --- a/simpleclient/src/test/java/io/prometheus/client/GaugeTest.java +++ b/simpleclient/src/test/java/io/prometheus/client/GaugeTest.java @@ -139,4 +139,25 @@ public void testCollect() { assertEquals(mfsFixture, mfs.get(0)); } + @Test + public void testCollectWithTimestamp() { + Long now = System.currentTimeMillis(); + labels.labels("a").inc(); + labels.labels("a").setTimestamp(now); + List mfs = labels.collect(); + + ArrayList samples = new ArrayList(); + ArrayList labelNames = new ArrayList(); + labelNames.add("l"); + ArrayList labelValues = new ArrayList(); + labelValues.add("a"); + samples.add(new Collector.MetricFamilySamples.Sample("labels", labelNames, labelValues, 1.0, now)); + Collector.MetricFamilySamples mfsFixture = new Collector.MetricFamilySamples("labels", Collector.Type.GAUGE, "help", samples); + + assertEquals(1, mfs.size()); + assertEquals(mfsFixture, mfs.get(0)); + Long timestamp = mfs.get(0).samples.get(0).timestamp; + assertEquals(now, timestamp); + } + } From 873e3613bdd74fa92b7e82038d9e8bbabe3574a9 Mon Sep 17 00:00:00 2001 From: Felipe Conde Date: Mon, 5 Feb 2018 12:44:46 +0100 Subject: [PATCH 09/19] Changed "tabs" for "spaces" --- .../src/test/java/io/prometheus/client/CounterTest.java | 4 ++-- .../src/test/java/io/prometheus/client/GaugeTest.java | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/simpleclient/src/test/java/io/prometheus/client/CounterTest.java b/simpleclient/src/test/java/io/prometheus/client/CounterTest.java index fa8034e4d..4aceef56b 100644 --- a/simpleclient/src/test/java/io/prometheus/client/CounterTest.java +++ b/simpleclient/src/test/java/io/prometheus/client/CounterTest.java @@ -84,8 +84,8 @@ public void testCollect() { @Test public void testCollectWithTimestamp() { - Long now = System.currentTimeMillis(); - labels.labels("a").inc(); + Long now = System.currentTimeMillis(); + labels.labels("a").inc(); labels.labels("a").setTimestamp(now); List mfs = labels.collect(); diff --git a/simpleclient/src/test/java/io/prometheus/client/GaugeTest.java b/simpleclient/src/test/java/io/prometheus/client/GaugeTest.java index f4f385cae..3aa109ed2 100644 --- a/simpleclient/src/test/java/io/prometheus/client/GaugeTest.java +++ b/simpleclient/src/test/java/io/prometheus/client/GaugeTest.java @@ -141,7 +141,7 @@ public void testCollect() { @Test public void testCollectWithTimestamp() { - Long now = System.currentTimeMillis(); + Long now = System.currentTimeMillis(); labels.labels("a").inc(); labels.labels("a").setTimestamp(now); List mfs = labels.collect(); From 908031e9ed008fbe758c9660eb2e4dde6eb1a909 Mon Sep 17 00:00:00 2001 From: Felipe Conde Date: Wed, 7 Feb 2018 20:32:18 +0100 Subject: [PATCH 10/19] Renamed timestamp property to timestampMs (Added also a note). Refactoring of some code in equals method. --- .../java/io/prometheus/client/Collector.java | 22 ++++++------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/simpleclient/src/main/java/io/prometheus/client/Collector.java b/simpleclient/src/main/java/io/prometheus/client/Collector.java index c5044b030..2d90cc8cc 100644 --- a/simpleclient/src/main/java/io/prometheus/client/Collector.java +++ b/simpleclient/src/main/java/io/prometheus/client/Collector.java @@ -77,14 +77,14 @@ public static class Sample { public final List labelNames; public final List labelValues; // Must have same length as labelNames. public final double value; - public final Long timestamp; + public final Long timestampMs; // It's an epoch format with milliseconds value included (this field is subject to change). - public Sample(String name, List labelNames, List labelValues, double value, Long timestamp) { + public Sample(String name, List labelNames, List labelValues, double value, Long timestampMs) { this.name = name; this.labelNames = labelNames; this.labelValues = labelValues; this.value = value; - this.timestamp = timestamp; + this.timestampMs = timestampMs; } public Sample(String name, List labelNames, List labelValues, double value) { @@ -98,17 +98,9 @@ public boolean equals(Object obj) { } Sample other = (Sample) obj; - boolean timestampEquals = true; - if ((timestamp != null && other.timestamp == null) || (timestamp == null && other.timestamp != null)) { - timestampEquals = false; - } - if (timestamp != null && other.timestamp != null) { - timestampEquals = other.timestamp.equals(timestamp); - } - return other.name.equals(name) && other.labelNames.equals(labelNames) && other.labelValues.equals(labelValues) && other.value == value - && timestampEquals; + && ((timestampMs == null && other.timestampMs == null) || (other.timestampMs != null) && (other.timestampMs.equals(timestampMs))); } @Override @@ -119,8 +111,8 @@ public int hashCode() { hash = 37 * hash + labelValues.hashCode(); long d = Double.doubleToLongBits(value); hash = 37 * hash + (int)(d ^ (d >>> 32)); - if (timestamp != null) { - hash = 37 * hash + timestamp.hashCode(); + if (timestampMs != null) { + hash = 37 * hash + timestampMs.hashCode(); } return hash; } @@ -128,7 +120,7 @@ public int hashCode() { @Override public String toString() { return "Name: " + name + " LabelNames: " + labelNames + " labelValues: " + labelValues + - " Value: " + value + " Timestamp: " + timestamp; + " Value: " + value + " TimestampMs: " + timestampMs; } } } From 3dc7855dd06e0012476498803ac767cc76041f13 Mon Sep 17 00:00:00 2001 From: Felipe Conde Date: Wed, 7 Feb 2018 21:52:27 +0100 Subject: [PATCH 11/19] Minor change: Just moved function setTimeStamp() to be located close to "value" functions, such as get()/set() --- .../src/main/java/io/prometheus/client/Counter.java | 12 ++++++------ .../src/main/java/io/prometheus/client/Gauge.java | 12 ++++++------ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/simpleclient/src/main/java/io/prometheus/client/Counter.java b/simpleclient/src/main/java/io/prometheus/client/Counter.java index 15f54fc8a..c3317d848 100644 --- a/simpleclient/src/main/java/io/prometheus/client/Counter.java +++ b/simpleclient/src/main/java/io/prometheus/client/Counter.java @@ -112,12 +112,6 @@ public static class Child { * Milliseconds since epoch. Optional value. */ private Long timestamp; - /** - * Set the optional value for time stamp in epoch format. - */ - public void setTimestamp(Long timestamp) { - this.timestamp = timestamp; - } /** * Increment the counter by 1. */ @@ -134,6 +128,12 @@ public void inc(double amt) { } value.add(amt); } + /** + * Set the optional value for time stamp in epoch format. + */ + public void setTimestamp(Long timestamp) { + this.timestamp = timestamp; + } /** * Get the value of the counter. */ diff --git a/simpleclient/src/main/java/io/prometheus/client/Gauge.java b/simpleclient/src/main/java/io/prometheus/client/Gauge.java index 25281b166..2b5230301 100644 --- a/simpleclient/src/main/java/io/prometheus/client/Gauge.java +++ b/simpleclient/src/main/java/io/prometheus/client/Gauge.java @@ -141,12 +141,6 @@ public static class Child { static TimeProvider timeProvider = new TimeProvider(); - /** - * Set the optional value for time stamp in epoch format, - */ - public void setTimestamp(Long timestamp) { - this.timestamp = timestamp; - } /** * Increment the gauge by 1. */ @@ -183,6 +177,12 @@ public void set(double val) { value.add(val); } } + /** + * Set the optional value for time stamp in epoch format, + */ + public void setTimestamp(Long timestamp) { + this.timestamp = timestamp; + } /** * Set the gauge to the current unixtime. */ From 88ee313b9a49af811c0b877aed379f6f7945e0a3 Mon Sep 17 00:00:00 2001 From: Felipe Conde Date: Wed, 7 Feb 2018 21:54:24 +0100 Subject: [PATCH 12/19] Added also getTimestamp()/setTimestamp() to be aligned with the structure used by "value". This is needed for Unit Tests code. --- .../src/main/java/io/prometheus/client/Counter.java | 12 ++++++++++++ .../src/main/java/io/prometheus/client/Gauge.java | 13 +++++++++++++ 2 files changed, 25 insertions(+) diff --git a/simpleclient/src/main/java/io/prometheus/client/Counter.java b/simpleclient/src/main/java/io/prometheus/client/Counter.java index c3317d848..56c4193eb 100644 --- a/simpleclient/src/main/java/io/prometheus/client/Counter.java +++ b/simpleclient/src/main/java/io/prometheus/client/Counter.java @@ -163,12 +163,24 @@ public void inc(double amt) { noLabelsChild.inc(amt); } + /** + * Set the optional value for time stamp in epoch format. + */ + public void setTimestamp(Long timestamp) { + noLabelsChild.setTimestamp(timestamp); + } /** * Get the value of the counter. */ public double get() { return noLabelsChild.get(); } + /** + * Get the optional value for time stamp in epoch format. It could be null. + */ + public Long getTimestamp() { + return noLabelsChild.getTimestamp(); + } @Override public List collect() { diff --git a/simpleclient/src/main/java/io/prometheus/client/Gauge.java b/simpleclient/src/main/java/io/prometheus/client/Gauge.java index 2b5230301..0c9223a26 100644 --- a/simpleclient/src/main/java/io/prometheus/client/Gauge.java +++ b/simpleclient/src/main/java/io/prometheus/client/Gauge.java @@ -269,6 +269,13 @@ public void dec(double amt) { public void set(double val) { noLabelsChild.set(val); } + /** + * Set the optional value for time stamp in epoch format, + */ + public void setTimestamp(Long timestamp) { + noLabelsChild.setTimestamp(timestamp); + } + /** * Set the gauge with no labels to the current unixtime. */ @@ -304,6 +311,12 @@ public double setToTime(Runnable timeable){ public double get() { return noLabelsChild.get(); } + /** + * Get the optional value for time stamp in epoch format. It could be null. + */ + public Long getTimestamp() { + return noLabelsChild.getTimestamp(); + } @Override From 9bf115968cf69eed8cf709848e5408a16d74c2d8 Mon Sep 17 00:00:00 2001 From: Felipe Conde Date: Wed, 7 Feb 2018 21:55:24 +0100 Subject: [PATCH 13/19] Fixed, property name was changed on "simpleclient" project but not here. --- .../java/io/prometheus/client/exporter/common/TextFormat.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/simpleclient_common/src/main/java/io/prometheus/client/exporter/common/TextFormat.java b/simpleclient_common/src/main/java/io/prometheus/client/exporter/common/TextFormat.java index 799eadbe4..954edfda8 100644 --- a/simpleclient_common/src/main/java/io/prometheus/client/exporter/common/TextFormat.java +++ b/simpleclient_common/src/main/java/io/prometheus/client/exporter/common/TextFormat.java @@ -46,9 +46,9 @@ public static void write004(Writer writer, Enumeration Date: Wed, 7 Feb 2018 21:57:08 +0100 Subject: [PATCH 14/19] Fixed and improved Unit Tests with new property name and also using the new functions added (covering both cases: "labels" and "nolabels"). --- .../io/prometheus/client/CounterTest.java | 23 +++++++++++++++++-- .../java/io/prometheus/client/GaugeTest.java | 22 ++++++++++++++++-- 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/simpleclient/src/test/java/io/prometheus/client/CounterTest.java b/simpleclient/src/test/java/io/prometheus/client/CounterTest.java index 4aceef56b..738176f68 100644 --- a/simpleclient/src/test/java/io/prometheus/client/CounterTest.java +++ b/simpleclient/src/test/java/io/prometheus/client/CounterTest.java @@ -83,7 +83,7 @@ public void testCollect() { } @Test - public void testCollectWithTimestamp() { + public void testCollectLabelsWithTimestamp() { Long now = System.currentTimeMillis(); labels.labels("a").inc(); labels.labels("a").setTimestamp(now); @@ -99,7 +99,26 @@ public void testCollectWithTimestamp() { assertEquals(1, mfs.size()); assertEquals(mfsFixture, mfs.get(0)); - Long timestamp = mfs.get(0).samples.get(0).timestamp; + Long timestamp = mfs.get(0).samples.get(0).timestampMs; + assertEquals(now, timestamp); + } + + @Test + public void testCollectNoLabelsWithTimestamp() { + Long now = System.currentTimeMillis(); + noLabels.inc(); + noLabels.setTimestamp(now); + List mfs = noLabels.collect(); + + ArrayList samples = new ArrayList(); + ArrayList labelNames = new ArrayList(); + ArrayList labelValues = new ArrayList(); + samples.add(new Collector.MetricFamilySamples.Sample("nolabels", labelNames, labelValues, 1.0, now)); + Collector.MetricFamilySamples mfsFixture = new Collector.MetricFamilySamples("nolabels", Collector.Type.COUNTER, "help", samples); + + assertEquals(1, mfs.size()); + assertEquals(mfsFixture, mfs.get(0)); + Long timestamp = mfs.get(0).samples.get(0).timestampMs; assertEquals(now, timestamp); } diff --git a/simpleclient/src/test/java/io/prometheus/client/GaugeTest.java b/simpleclient/src/test/java/io/prometheus/client/GaugeTest.java index 3aa109ed2..6e0be8acf 100644 --- a/simpleclient/src/test/java/io/prometheus/client/GaugeTest.java +++ b/simpleclient/src/test/java/io/prometheus/client/GaugeTest.java @@ -140,7 +140,7 @@ public void testCollect() { } @Test - public void testCollectWithTimestamp() { + public void testCollectLabelsWithTimestamp() { Long now = System.currentTimeMillis(); labels.labels("a").inc(); labels.labels("a").setTimestamp(now); @@ -156,8 +156,26 @@ public void testCollectWithTimestamp() { assertEquals(1, mfs.size()); assertEquals(mfsFixture, mfs.get(0)); - Long timestamp = mfs.get(0).samples.get(0).timestamp; + Long timestamp = mfs.get(0).samples.get(0).timestampMs; assertEquals(now, timestamp); } + @Test + public void testCollectNoLabelsWithTimestamp() { + Long now = System.currentTimeMillis(); + noLabels.inc(); + noLabels.setTimestamp(now); + List mfs = noLabels.collect(); + + ArrayList samples = new ArrayList(); + ArrayList labelNames = new ArrayList(); + ArrayList labelValues = new ArrayList(); + samples.add(new Collector.MetricFamilySamples.Sample("nolabels", labelNames, labelValues, 1.0, now)); + Collector.MetricFamilySamples mfsFixture = new Collector.MetricFamilySamples("nolabels", Collector.Type.GAUGE, "help", samples); + + assertEquals(1, mfs.size()); + assertEquals(mfsFixture, mfs.get(0)); + Long timestamp = mfs.get(0).samples.get(0).timestampMs; + assertEquals(now, timestamp); + } } From 3af44d7fda8fe9f15fb30c4337184e2e6a2450ca Mon Sep 17 00:00:00 2001 From: Felipe Conde Date: Wed, 7 Feb 2018 21:57:46 +0100 Subject: [PATCH 15/19] Added 2 new Unit Tests using timestamps. --- .../exporter/common/TextFormatTest.java | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) 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 ad2d4f173..9cb9001b2 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 @@ -34,6 +34,18 @@ public void testGaugeOutput() throws IOException { + "nolabels 1.0\n", writer.toString()); } + @Test + public void testGaugeOutputWithTimetamp() throws IOException { + Long now = System.currentTimeMillis(); + Gauge noLabels = Gauge.build().name("nolabels").help("help").register(registry); + noLabels.inc(); + noLabels.setTimestamp(now); + TextFormat.write004(writer, registry.metricFamilySamples()); + assertEquals("# HELP nolabels help\n" + + "# TYPE nolabels gauge\n" + + "nolabels 1.0 "+ now +"\n", writer.toString()); + } + @Test public void testValueInfinity() throws IOException { Gauge noLabels = Gauge.build().name("nolabels").help("help").register(registry); @@ -54,6 +66,18 @@ public void testCounterOutput() throws IOException { + "nolabels 1.0\n", writer.toString()); } + @Test + public void testCounterOutputWithTimetamp() throws IOException { + Long now = System.currentTimeMillis(); + Counter noLabels = Counter.build().name("nolabels").help("help").register(registry); + noLabels.inc(); + noLabels.setTimestamp(now); + TextFormat.write004(writer, registry.metricFamilySamples()); + assertEquals("# HELP nolabels help\n" + + "# TYPE nolabels counter\n" + + "nolabels 1.0 "+ now +"\n", writer.toString()); + } + @Test public void testSummaryOutput() throws IOException { Summary noLabels = Summary.build().name("nolabels").help("help").register(registry); From 5d6af4327a2ad379315a9568284897f71df20220 Mon Sep 17 00:00:00 2001 From: Felipe Conde Date: Mon, 12 Feb 2018 20:30:14 +0100 Subject: [PATCH 16/19] Unit test added for metric with timestamp --- .../exporter/common/TextFormatTest.java | 29 ++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) 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 9cb9001b2..c21969e05 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 @@ -4,10 +4,13 @@ import java.io.IOException; import java.io.StringWriter; +import java.util.ArrayList; +import java.util.List; import org.junit.Before; import org.junit.Test; +import io.prometheus.client.Collector; import io.prometheus.client.CollectorRegistry; import io.prometheus.client.Counter; import io.prometheus.client.Gauge; @@ -65,7 +68,7 @@ public void testCounterOutput() throws IOException { + "# TYPE nolabels counter\n" + "nolabels 1.0\n", writer.toString()); } - + @Test public void testCounterOutputWithTimetamp() throws IOException { Long now = System.currentTimeMillis(); @@ -78,6 +81,30 @@ public void testCounterOutputWithTimetamp() throws IOException { + "nolabels 1.0 "+ now +"\n", writer.toString()); } + @Test + public void testMetricOutputWithTimetamp() throws IOException { + final Long now = System.currentTimeMillis(); + + class CustomCollector extends Collector { + public List collect() { + List mfs = new ArrayList(); + ArrayList labelNames = new ArrayList(); + ArrayList labelValues = new ArrayList(); + ArrayList samples = new ArrayList(); + MetricFamilySamples.Sample sample = new MetricFamilySamples.Sample("nolabels", labelNames, labelValues, 1.0, now); + samples.add(sample); + mfs.add(new MetricFamilySamples("nolabels", Collector.Type.UNTYPED, "help", samples)); + return mfs; + } + } + + new CustomCollector().register(registry); + TextFormat.write004(writer, registry.metricFamilySamples()); + assertEquals("# HELP nolabels help\n" + + "# TYPE nolabels untyped\n" + + "nolabels 1.0 "+ now +"\n", writer.toString()); + } + @Test public void testSummaryOutput() throws IOException { Summary noLabels = Summary.build().name("nolabels").help("help").register(registry); From 143bc074156f175f4a74a8b1024fc8e1a8d3aa36 Mon Sep 17 00:00:00 2001 From: Felipe Conde Date: Tue, 13 Feb 2018 12:56:35 +0100 Subject: [PATCH 17/19] Removed "timestamp" methods and Unit Tests for "Counter" and "Gauge" --- .../java/io/prometheus/client/Counter.java | 30 +------------- .../main/java/io/prometheus/client/Gauge.java | 33 +-------------- .../io/prometheus/client/CounterTest.java | 40 ------------------- .../java/io/prometheus/client/GaugeTest.java | 40 ------------------- .../exporter/common/TextFormatTest.java | 24 ----------- 5 files changed, 2 insertions(+), 165 deletions(-) diff --git a/simpleclient/src/main/java/io/prometheus/client/Counter.java b/simpleclient/src/main/java/io/prometheus/client/Counter.java index 56c4193eb..180a8d5c9 100644 --- a/simpleclient/src/main/java/io/prometheus/client/Counter.java +++ b/simpleclient/src/main/java/io/prometheus/client/Counter.java @@ -108,10 +108,6 @@ protected Child newChild() { */ public static class Child { private final DoubleAdder value = new DoubleAdder(); - /** - * Milliseconds since epoch. Optional value. - */ - private Long timestamp; /** * Increment the counter by 1. */ @@ -128,24 +124,12 @@ public void inc(double amt) { } value.add(amt); } - /** - * Set the optional value for time stamp in epoch format. - */ - public void setTimestamp(Long timestamp) { - this.timestamp = timestamp; - } /** * Get the value of the counter. */ public double get() { return value.sum(); } - /** - * Get the optional value for time stamp in epoch format. It could be null. - */ - public Long getTimestamp() { - return timestamp; - } } // Convenience methods. @@ -163,30 +147,18 @@ public void inc(double amt) { noLabelsChild.inc(amt); } - /** - * Set the optional value for time stamp in epoch format. - */ - public void setTimestamp(Long timestamp) { - noLabelsChild.setTimestamp(timestamp); - } /** * Get the value of the counter. */ public double get() { return noLabelsChild.get(); } - /** - * Get the optional value for time stamp in epoch format. It could be null. - */ - public Long getTimestamp() { - return noLabelsChild.getTimestamp(); - } @Override public List collect() { List samples = new ArrayList(children.size()); for(Map.Entry, Child> c: children.entrySet()) { - samples.add(new MetricFamilySamples.Sample(fullname, labelNames, c.getKey(), c.getValue().get(), c.getValue().getTimestamp())); + samples.add(new MetricFamilySamples.Sample(fullname, labelNames, c.getKey(), c.getValue().get())); } return familySamplesList(Type.COUNTER, samples); } diff --git a/simpleclient/src/main/java/io/prometheus/client/Gauge.java b/simpleclient/src/main/java/io/prometheus/client/Gauge.java index 0c9223a26..2da01d08c 100644 --- a/simpleclient/src/main/java/io/prometheus/client/Gauge.java +++ b/simpleclient/src/main/java/io/prometheus/client/Gauge.java @@ -134,10 +134,6 @@ public void close() { */ public static class Child { private final DoubleAdder value = new DoubleAdder(); - /** - * Milliseconds since epoch. Optional value. - */ - private Long timestamp; static TimeProvider timeProvider = new TimeProvider(); @@ -177,12 +173,6 @@ public void set(double val) { value.add(val); } } - /** - * Set the optional value for time stamp in epoch format, - */ - public void setTimestamp(Long timestamp) { - this.timestamp = timestamp; - } /** * Set the gauge to the current unixtime. */ @@ -229,13 +219,6 @@ public double get() { return value.sum(); } } - - /** - * Get the optional value for time stamp in epoch format. It could be null. - */ - public Long getTimestamp() { - return timestamp; - } } // Convenience methods. @@ -269,13 +252,6 @@ public void dec(double amt) { public void set(double val) { noLabelsChild.set(val); } - /** - * Set the optional value for time stamp in epoch format, - */ - public void setTimestamp(Long timestamp) { - noLabelsChild.setTimestamp(timestamp); - } - /** * Set the gauge with no labels to the current unixtime. */ @@ -311,19 +287,12 @@ public double setToTime(Runnable timeable){ public double get() { return noLabelsChild.get(); } - /** - * Get the optional value for time stamp in epoch format. It could be null. - */ - public Long getTimestamp() { - return noLabelsChild.getTimestamp(); - } - @Override public List collect() { List samples = new ArrayList(children.size()); for(Map.Entry, Child> c: children.entrySet()) { - samples.add(new MetricFamilySamples.Sample(fullname, labelNames, c.getKey(), c.getValue().get(), c.getValue().getTimestamp())); + samples.add(new MetricFamilySamples.Sample(fullname, labelNames, c.getKey(), c.getValue().get())); } return familySamplesList(Type.GAUGE, samples); } diff --git a/simpleclient/src/test/java/io/prometheus/client/CounterTest.java b/simpleclient/src/test/java/io/prometheus/client/CounterTest.java index 738176f68..16d025f73 100644 --- a/simpleclient/src/test/java/io/prometheus/client/CounterTest.java +++ b/simpleclient/src/test/java/io/prometheus/client/CounterTest.java @@ -82,44 +82,4 @@ public void testCollect() { assertEquals(mfsFixture, mfs.get(0)); } - @Test - public void testCollectLabelsWithTimestamp() { - Long now = System.currentTimeMillis(); - labels.labels("a").inc(); - labels.labels("a").setTimestamp(now); - List mfs = labels.collect(); - - ArrayList samples = new ArrayList(); - ArrayList labelNames = new ArrayList(); - labelNames.add("l"); - ArrayList labelValues = new ArrayList(); - labelValues.add("a"); - samples.add(new Collector.MetricFamilySamples.Sample("labels", labelNames, labelValues, 1.0, now)); - Collector.MetricFamilySamples mfsFixture = new Collector.MetricFamilySamples("labels", Collector.Type.COUNTER, "help", samples); - - assertEquals(1, mfs.size()); - assertEquals(mfsFixture, mfs.get(0)); - Long timestamp = mfs.get(0).samples.get(0).timestampMs; - assertEquals(now, timestamp); - } - - @Test - public void testCollectNoLabelsWithTimestamp() { - Long now = System.currentTimeMillis(); - noLabels.inc(); - noLabels.setTimestamp(now); - List mfs = noLabels.collect(); - - ArrayList samples = new ArrayList(); - ArrayList labelNames = new ArrayList(); - ArrayList labelValues = new ArrayList(); - samples.add(new Collector.MetricFamilySamples.Sample("nolabels", labelNames, labelValues, 1.0, now)); - Collector.MetricFamilySamples mfsFixture = new Collector.MetricFamilySamples("nolabels", Collector.Type.COUNTER, "help", samples); - - assertEquals(1, mfs.size()); - assertEquals(mfsFixture, mfs.get(0)); - Long timestamp = mfs.get(0).samples.get(0).timestampMs; - assertEquals(now, timestamp); - } - } diff --git a/simpleclient/src/test/java/io/prometheus/client/GaugeTest.java b/simpleclient/src/test/java/io/prometheus/client/GaugeTest.java index 6e0be8acf..7a9156684 100644 --- a/simpleclient/src/test/java/io/prometheus/client/GaugeTest.java +++ b/simpleclient/src/test/java/io/prometheus/client/GaugeTest.java @@ -138,44 +138,4 @@ public void testCollect() { assertEquals(1, mfs.size()); assertEquals(mfsFixture, mfs.get(0)); } - - @Test - public void testCollectLabelsWithTimestamp() { - Long now = System.currentTimeMillis(); - labels.labels("a").inc(); - labels.labels("a").setTimestamp(now); - List mfs = labels.collect(); - - ArrayList samples = new ArrayList(); - ArrayList labelNames = new ArrayList(); - labelNames.add("l"); - ArrayList labelValues = new ArrayList(); - labelValues.add("a"); - samples.add(new Collector.MetricFamilySamples.Sample("labels", labelNames, labelValues, 1.0, now)); - Collector.MetricFamilySamples mfsFixture = new Collector.MetricFamilySamples("labels", Collector.Type.GAUGE, "help", samples); - - assertEquals(1, mfs.size()); - assertEquals(mfsFixture, mfs.get(0)); - Long timestamp = mfs.get(0).samples.get(0).timestampMs; - assertEquals(now, timestamp); - } - - @Test - public void testCollectNoLabelsWithTimestamp() { - Long now = System.currentTimeMillis(); - noLabels.inc(); - noLabels.setTimestamp(now); - List mfs = noLabels.collect(); - - ArrayList samples = new ArrayList(); - ArrayList labelNames = new ArrayList(); - ArrayList labelValues = new ArrayList(); - samples.add(new Collector.MetricFamilySamples.Sample("nolabels", labelNames, labelValues, 1.0, now)); - Collector.MetricFamilySamples mfsFixture = new Collector.MetricFamilySamples("nolabels", Collector.Type.GAUGE, "help", samples); - - assertEquals(1, mfs.size()); - assertEquals(mfsFixture, mfs.get(0)); - Long timestamp = mfs.get(0).samples.get(0).timestampMs; - assertEquals(now, timestamp); - } } 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 c21969e05..db1924589 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 @@ -37,18 +37,6 @@ public void testGaugeOutput() throws IOException { + "nolabels 1.0\n", writer.toString()); } - @Test - public void testGaugeOutputWithTimetamp() throws IOException { - Long now = System.currentTimeMillis(); - Gauge noLabels = Gauge.build().name("nolabels").help("help").register(registry); - noLabels.inc(); - noLabels.setTimestamp(now); - TextFormat.write004(writer, registry.metricFamilySamples()); - assertEquals("# HELP nolabels help\n" - + "# TYPE nolabels gauge\n" - + "nolabels 1.0 "+ now +"\n", writer.toString()); - } - @Test public void testValueInfinity() throws IOException { Gauge noLabels = Gauge.build().name("nolabels").help("help").register(registry); @@ -68,18 +56,6 @@ public void testCounterOutput() throws IOException { + "# TYPE nolabels counter\n" + "nolabels 1.0\n", writer.toString()); } - - @Test - public void testCounterOutputWithTimetamp() throws IOException { - Long now = System.currentTimeMillis(); - Counter noLabels = Counter.build().name("nolabels").help("help").register(registry); - noLabels.inc(); - noLabels.setTimestamp(now); - TextFormat.write004(writer, registry.metricFamilySamples()); - assertEquals("# HELP nolabels help\n" - + "# TYPE nolabels counter\n" - + "nolabels 1.0 "+ now +"\n", writer.toString()); - } @Test public void testMetricOutputWithTimetamp() throws IOException { From c463ae6a5c8101ceb8a659b8ac1cc3c7a9eed3de Mon Sep 17 00:00:00 2001 From: Felipe Conde Date: Tue, 13 Feb 2018 12:56:45 +0100 Subject: [PATCH 18/19] Typo fixed --- .../io/prometheus/client/exporter/common/TextFormatTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 db1924589..ed663d4c7 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 @@ -58,7 +58,7 @@ public void testCounterOutput() throws IOException { } @Test - public void testMetricOutputWithTimetamp() throws IOException { + public void testMetricOutputWithTimestamp() throws IOException { final Long now = System.currentTimeMillis(); class CustomCollector extends Collector { From aae6c18272657c917a1d7a0a32e209acb80ab9a4 Mon Sep 17 00:00:00 2001 From: Felipe Conde Date: Wed, 14 Feb 2018 11:06:47 +0100 Subject: [PATCH 19/19] Changed timestamp to fixed value instead of dynamic value --- .../io/prometheus/client/exporter/common/TextFormatTest.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) 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 ed663d4c7..67a01b38e 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 @@ -59,7 +59,6 @@ public void testCounterOutput() throws IOException { @Test public void testMetricOutputWithTimestamp() throws IOException { - final Long now = System.currentTimeMillis(); class CustomCollector extends Collector { public List collect() { @@ -67,7 +66,7 @@ public List collect() { ArrayList labelNames = new ArrayList(); ArrayList labelValues = new ArrayList(); ArrayList samples = new ArrayList(); - MetricFamilySamples.Sample sample = new MetricFamilySamples.Sample("nolabels", labelNames, labelValues, 1.0, now); + MetricFamilySamples.Sample sample = new MetricFamilySamples.Sample("nolabels", labelNames, labelValues, 1.0, 1518123456L); samples.add(sample); mfs.add(new MetricFamilySamples("nolabels", Collector.Type.UNTYPED, "help", samples)); return mfs; @@ -78,7 +77,7 @@ public List collect() { TextFormat.write004(writer, registry.metricFamilySamples()); assertEquals("# HELP nolabels help\n" + "# TYPE nolabels untyped\n" - + "nolabels 1.0 "+ now +"\n", writer.toString()); + + "nolabels 1.0 1518123456\n", writer.toString()); } @Test