diff --git a/simpleclient/src/main/java/io/prometheus/client/Collector.java b/simpleclient/src/main/java/io/prometheus/client/Collector.java index 9dd82efe1..2d90cc8cc 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 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) { + 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.timestampMs = timestampMs; + } + + public Sample(String name, List labelNames, List labelValues, double value) { + this(name, labelNames, labelValues, value, null); } @Override @@ -91,8 +97,10 @@ public boolean equals(Object obj) { return false; } 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 + && ((timestampMs == null && other.timestampMs == null) || (other.timestampMs != null) && (other.timestampMs.equals(timestampMs))); } @Override @@ -103,13 +111,16 @@ public int hashCode() { hash = 37 * hash + labelValues.hashCode(); long d = Double.doubleToLongBits(value); hash = 37 * hash + (int)(d ^ (d >>> 32)); + if (timestampMs != null) { + hash = 37 * hash + timestampMs.hashCode(); + } return hash; } @Override public String toString() { return "Name: " + name + " LabelNames: " + labelNames + " labelValues: " + labelValues + - " Value: " + value; + " Value: " + value + " TimestampMs: " + timestampMs; } } } diff --git a/simpleclient/src/main/java/io/prometheus/client/Gauge.java b/simpleclient/src/main/java/io/prometheus/client/Gauge.java index 3473cedf0..2da01d08c 100644 --- a/simpleclient/src/main/java/io/prometheus/client/Gauge.java +++ b/simpleclient/src/main/java/io/prometheus/client/Gauge.java @@ -136,6 +136,7 @@ public static class Child { private final DoubleAdder value = new DoubleAdder(); static TimeProvider timeProvider = new TimeProvider(); + /** * Increment the gauge by 1. */ @@ -287,7 +288,6 @@ public double get() { return noLabelsChild.get(); } - @Override public List collect() { List samples = new ArrayList(children.size()); diff --git a/simpleclient/src/test/java/io/prometheus/client/GaugeTest.java b/simpleclient/src/test/java/io/prometheus/client/GaugeTest.java index 967c8a87b..7a9156684 100644 --- a/simpleclient/src/test/java/io/prometheus/client/GaugeTest.java +++ b/simpleclient/src/test/java/io/prometheus/client/GaugeTest.java @@ -138,5 +138,4 @@ public void testCollect() { assertEquals(1, mfs.size()); assertEquals(mfsFixture, mfs.get(0)); } - } 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..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,6 +46,10 @@ public static void write004(Writer writer, Enumeration 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, 1518123456L); + 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 1518123456\n", writer.toString()); + } + @Test public void testSummaryOutput() throws IOException { Summary noLabels = Summary.build().name("nolabels").help("help").register(registry);