Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions simpleclient/src/main/java/io/prometheus/client/Collector.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,18 @@ public static class Sample {
public final List<String> labelNames;
public final List<String> 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<String> labelNames, List<String> labelValues, double value) {
public Sample(String name, List<String> labelNames, List<String> 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<String> labelNames, List<String> labelValues, double value) {
this(name, labelNames, labelValues, value, null);
}

@Override
Expand All @@ -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
Expand All @@ -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;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion simpleclient/src/main/java/io/prometheus/client/Gauge.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ public static class Child {
private final DoubleAdder value = new DoubleAdder();

static TimeProvider timeProvider = new TimeProvider();

/**
* Increment the gauge by 1.
*/
Expand Down Expand Up @@ -287,7 +288,6 @@ public double get() {
return noLabelsChild.get();
}


@Override
public List<MetricFamilySamples> collect() {
List<MetricFamilySamples.Sample> samples = new ArrayList<MetricFamilySamples.Sample>(children.size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,5 +138,4 @@ public void testCollect() {
assertEquals(1, mfs.size());
assertEquals(mfsFixture, mfs.get(0));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ public static void write004(Writer writer, Enumeration<Collector.MetricFamilySam
}
writer.write(' ');
writer.write(Collector.doubleToGoString(sample.value));
if (sample.timestampMs != null){
writer.write(' ');
writer.write(sample.timestampMs.toString());
}
writer.write('\n');
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -54,6 +57,29 @@ public void testCounterOutput() throws IOException {
+ "nolabels 1.0\n", writer.toString());
}

@Test
public void testMetricOutputWithTimestamp() throws IOException {

class CustomCollector extends Collector {
public List<MetricFamilySamples> collect() {
List<MetricFamilySamples> mfs = new ArrayList<MetricFamilySamples>();
ArrayList<String> labelNames = new ArrayList<String>();
ArrayList<String> labelValues = new ArrayList<String>();
ArrayList<MetricFamilySamples.Sample> samples = new ArrayList<Collector.MetricFamilySamples.Sample>();
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);
Expand Down