Skip to content

Commit 5b0a275

Browse files
Added optional "timestamp" value as part of metric sample (#337)
1 parent 80b44dc commit 5b0a275

File tree

5 files changed

+45
-5
lines changed

5 files changed

+45
-5
lines changed

simpleclient/src/main/java/io/prometheus/client/Collector.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,18 @@ public static class Sample {
7777
public final List<String> labelNames;
7878
public final List<String> labelValues; // Must have same length as labelNames.
7979
public final double value;
80+
public final Long timestampMs; // It's an epoch format with milliseconds value included (this field is subject to change).
8081

81-
public Sample(String name, List<String> labelNames, List<String> labelValues, double value) {
82+
public Sample(String name, List<String> labelNames, List<String> labelValues, double value, Long timestampMs) {
8283
this.name = name;
8384
this.labelNames = labelNames;
8485
this.labelValues = labelValues;
8586
this.value = value;
87+
this.timestampMs = timestampMs;
88+
}
89+
90+
public Sample(String name, List<String> labelNames, List<String> labelValues, double value) {
91+
this(name, labelNames, labelValues, value, null);
8692
}
8793

8894
@Override
@@ -91,8 +97,10 @@ public boolean equals(Object obj) {
9197
return false;
9298
}
9399
Sample other = (Sample) obj;
100+
94101
return other.name.equals(name) && other.labelNames.equals(labelNames)
95-
&& other.labelValues.equals(labelValues) && other.value == value;
102+
&& other.labelValues.equals(labelValues) && other.value == value
103+
&& ((timestampMs == null && other.timestampMs == null) || (other.timestampMs != null) && (other.timestampMs.equals(timestampMs)));
96104
}
97105

98106
@Override
@@ -103,13 +111,16 @@ public int hashCode() {
103111
hash = 37 * hash + labelValues.hashCode();
104112
long d = Double.doubleToLongBits(value);
105113
hash = 37 * hash + (int)(d ^ (d >>> 32));
114+
if (timestampMs != null) {
115+
hash = 37 * hash + timestampMs.hashCode();
116+
}
106117
return hash;
107118
}
108119

109120
@Override
110121
public String toString() {
111122
return "Name: " + name + " LabelNames: " + labelNames + " labelValues: " + labelValues +
112-
" Value: " + value;
123+
" Value: " + value + " TimestampMs: " + timestampMs;
113124
}
114125
}
115126
}

simpleclient/src/main/java/io/prometheus/client/Gauge.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ public static class Child {
136136
private final DoubleAdder value = new DoubleAdder();
137137

138138
static TimeProvider timeProvider = new TimeProvider();
139+
139140
/**
140141
* Increment the gauge by 1.
141142
*/
@@ -287,7 +288,6 @@ public double get() {
287288
return noLabelsChild.get();
288289
}
289290

290-
291291
@Override
292292
public List<MetricFamilySamples> collect() {
293293
List<MetricFamilySamples.Sample> samples = new ArrayList<MetricFamilySamples.Sample>(children.size());

simpleclient/src/test/java/io/prometheus/client/GaugeTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,5 +138,4 @@ public void testCollect() {
138138
assertEquals(1, mfs.size());
139139
assertEquals(mfsFixture, mfs.get(0));
140140
}
141-
142141
}

simpleclient_common/src/main/java/io/prometheus/client/exporter/common/TextFormat.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ public static void write004(Writer writer, Enumeration<Collector.MetricFamilySam
4646
}
4747
writer.write(' ');
4848
writer.write(Collector.doubleToGoString(sample.value));
49+
if (sample.timestampMs != null){
50+
writer.write(' ');
51+
writer.write(sample.timestampMs.toString());
52+
}
4953
writer.write('\n');
5054
}
5155
}

simpleclient_common/src/test/java/io/prometheus/client/exporter/common/TextFormatTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44

55
import java.io.IOException;
66
import java.io.StringWriter;
7+
import java.util.ArrayList;
8+
import java.util.List;
79

810
import org.junit.Before;
911
import org.junit.Test;
1012

13+
import io.prometheus.client.Collector;
1114
import io.prometheus.client.CollectorRegistry;
1215
import io.prometheus.client.Counter;
1316
import io.prometheus.client.Gauge;
@@ -54,6 +57,29 @@ public void testCounterOutput() throws IOException {
5457
+ "nolabels 1.0\n", writer.toString());
5558
}
5659

60+
@Test
61+
public void testMetricOutputWithTimestamp() throws IOException {
62+
63+
class CustomCollector extends Collector {
64+
public List<MetricFamilySamples> collect() {
65+
List<MetricFamilySamples> mfs = new ArrayList<MetricFamilySamples>();
66+
ArrayList<String> labelNames = new ArrayList<String>();
67+
ArrayList<String> labelValues = new ArrayList<String>();
68+
ArrayList<MetricFamilySamples.Sample> samples = new ArrayList<Collector.MetricFamilySamples.Sample>();
69+
MetricFamilySamples.Sample sample = new MetricFamilySamples.Sample("nolabels", labelNames, labelValues, 1.0, 1518123456L);
70+
samples.add(sample);
71+
mfs.add(new MetricFamilySamples("nolabels", Collector.Type.UNTYPED, "help", samples));
72+
return mfs;
73+
}
74+
}
75+
76+
new CustomCollector().register(registry);
77+
TextFormat.write004(writer, registry.metricFamilySamples());
78+
assertEquals("# HELP nolabels help\n"
79+
+ "# TYPE nolabels untyped\n"
80+
+ "nolabels 1.0 1518123456\n", writer.toString());
81+
}
82+
5783
@Test
5884
public void testSummaryOutput() throws IOException {
5985
Summary noLabels = Summary.build().name("nolabels").help("help").register(registry);

0 commit comments

Comments
 (0)