Skip to content

Commit 5c0d1e7

Browse files
committed
Add _created for direct instrumentation.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
1 parent 801f91e commit 5c0d1e7

File tree

9 files changed

+60
-12
lines changed

9 files changed

+60
-12
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,20 @@ private List<String> collectorNames(Collector m) {
113113
case SUMMARY:
114114
names.add(family.name + "_count");
115115
names.add(family.name + "_sum");
116+
names.add(family.name + "_created");
116117
names.add(family.name);
117118
break;
118119
case HISTOGRAM:
119120
names.add(family.name + "_count");
120121
names.add(family.name + "_sum");
121122
names.add(family.name + "_bucket");
123+
names.add(family.name + "_created");
124+
names.add(family.name);
125+
break;
126+
case GAUGE_HISTOGRAM:
127+
names.add(family.name + "_gcount");
128+
names.add(family.name + "_gsum");
129+
names.add(family.name + "_bucket");
122130
names.add(family.name);
123131
break;
124132
default:

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ protected Child newChild() {
112112
*/
113113
public static class Child {
114114
private final DoubleAdder value = new DoubleAdder();
115+
private final long created = System.currentTimeMillis();
115116
/**
116117
* Increment the counter by 1.
117118
*/
@@ -134,6 +135,12 @@ public void inc(double amt) {
134135
public double get() {
135136
return value.sum();
136137
}
138+
/**
139+
* Get the created time of the counter in milliseconds.
140+
*/
141+
public long created() {
142+
return created;
143+
}
137144
}
138145

139146
// Convenience methods.
@@ -163,6 +170,7 @@ public List<MetricFamilySamples> collect() {
163170
List<MetricFamilySamples.Sample> samples = new ArrayList<MetricFamilySamples.Sample>(children.size());
164171
for(Map.Entry<List<String>, Child> c: children.entrySet()) {
165172
samples.add(new MetricFamilySamples.Sample(fullname + "_total", labelNames, c.getKey(), c.getValue().get()));
173+
samples.add(new MetricFamilySamples.Sample(fullname + "_created", labelNames, c.getKey(), c.getValue().created() / 1000.0));
166174
}
167175
return familySamplesList(Type.COUNTER, samples);
168176
}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,10 +231,12 @@ public <E> E time(Callable<E> timeable) {
231231
public static class Value {
232232
public final double sum;
233233
public final double[] buckets;
234+
public final long created;
234235

235-
public Value(double sum, double[] buckets) {
236+
public Value(double sum, double[] buckets, long created) {
236237
this.sum = sum;
237238
this.buckets = buckets;
239+
this.created = created;
238240
}
239241
}
240242

@@ -248,6 +250,7 @@ private Child(double[] buckets) {
248250
private final double[] upperBounds;
249251
private final DoubleAdder[] cumulativeCounts;
250252
private final DoubleAdder sum = new DoubleAdder();
253+
private final long created = System.currentTimeMillis();
251254

252255

253256
/**
@@ -283,7 +286,7 @@ public Value get() {
283286
acc += cumulativeCounts[i].sum();
284287
buckets[i] = acc;
285288
}
286-
return new Value(sum.sum(), buckets);
289+
return new Value(sum.sum(), buckets, created);
287290
}
288291
}
289292

@@ -337,6 +340,7 @@ public List<MetricFamilySamples> collect() {
337340
}
338341
samples.add(new MetricFamilySamples.Sample(fullname + "_count", labelNames, c.getKey(), v.buckets[buckets.length-1]));
339342
samples.add(new MetricFamilySamples.Sample(fullname + "_sum", labelNames, c.getKey(), v.sum));
343+
samples.add(new MetricFamilySamples.Sample(fullname + "_created", labelNames, c.getKey(), v.created / 1000.0));
340344
}
341345

342346
return familySamplesList(Type.HISTOGRAM, samples);

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,11 +239,13 @@ public static class Value {
239239
public final double count;
240240
public final double sum;
241241
public final SortedMap<Double, Double> quantiles;
242+
public final long created;
242243

243-
private Value(double count, double sum, List<Quantile> quantiles, TimeWindowQuantiles quantileValues) {
244+
private Value(double count, double sum, List<Quantile> quantiles, TimeWindowQuantiles quantileValues, long created) {
244245
this.count = count;
245246
this.sum = sum;
246247
this.quantiles = Collections.unmodifiableSortedMap(snapshot(quantiles, quantileValues));
248+
this.created = created;
247249
}
248250

249251
private SortedMap<Double, Double> snapshot(List<Quantile> quantiles, TimeWindowQuantiles quantileValues) {
@@ -263,6 +265,7 @@ private SortedMap<Double, Double> snapshot(List<Quantile> quantiles, TimeWindowQ
263265
private final DoubleAdder sum = new DoubleAdder();
264266
private final List<Quantile> quantiles;
265267
private final TimeWindowQuantiles quantileValues;
268+
private final long created = System.currentTimeMillis();
266269

267270
private Child(List<Quantile> quantiles, long maxAgeSeconds, int ageBuckets) {
268271
this.quantiles = quantiles;
@@ -297,7 +300,7 @@ public Timer startTimer() {
297300
* <em>Warning:</em> The definition of {@link Value} is subject to change.
298301
*/
299302
public Value get() {
300-
return new Value(count.sum(), sum.sum(), quantiles, quantileValues);
303+
return new Value(count.sum(), sum.sum(), quantiles, quantileValues, created);
301304
}
302305
}
303306

@@ -360,6 +363,7 @@ public List<MetricFamilySamples> collect() {
360363
}
361364
samples.add(new MetricFamilySamples.Sample(fullname + "_count", labelNames, c.getKey(), v.count));
362365
samples.add(new MetricFamilySamples.Sample(fullname + "_sum", labelNames, c.getKey(), v.sum));
366+
samples.add(new MetricFamilySamples.Sample(fullname + "_created", labelNames, c.getKey(), v.created / 1000.0));
363367
}
364368

365369
return familySamplesList(Type.SUMMARY, samples);

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ public void testCollect() {
9797
ArrayList<String> labelValues = new ArrayList<String>();
9898
labelValues.add("a");
9999
samples.add(new Collector.MetricFamilySamples.Sample("labels_seconds_total", labelNames, labelValues, 1.0));
100+
samples.add(new Collector.MetricFamilySamples.Sample("labels_seconds_created", labelNames, labelValues, labels.labels("a").created() / 1000.0));
100101
Collector.MetricFamilySamples mfsFixture = new Collector.MetricFamilySamples("labels_seconds", "seconds", Collector.Type.COUNTER, "help", samples);
101102

102103
assertEquals(1, mfs.size());

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,8 @@ public void testCollect() {
204204
}
205205
samples.add(new Collector.MetricFamilySamples.Sample("labels_count", labelNames, labelValues, 1.0));
206206
samples.add(new Collector.MetricFamilySamples.Sample("labels_sum", labelNames, labelValues, 2.0));
207+
samples.add(new Collector.MetricFamilySamples.Sample("labels_created", labelNames, labelValues, labels.labels("a").get().created / 1000.0));
208+
207209
Collector.MetricFamilySamples mfsFixture = new Collector.MetricFamilySamples("labels", Collector.Type.HISTOGRAM, "help", samples);
208210

209211
assertEquals(1, mfs.size());

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ public void testCollect() {
183183
labelValues.add("a");
184184
samples.add(new Collector.MetricFamilySamples.Sample("labels_count", labelNames, labelValues, 1.0));
185185
samples.add(new Collector.MetricFamilySamples.Sample("labels_sum", labelNames, labelValues, 2.0));
186+
samples.add(new Collector.MetricFamilySamples.Sample("labels_created", labelNames, labelValues, labels.labels("a").get().created / 1000.0));
186187
Collector.MetricFamilySamples mfsFixture = new Collector.MetricFamilySamples("labels", Collector.Type.SUMMARY, "help", samples);
187188

188189
assertEquals(1, mfs.size());
@@ -200,6 +201,7 @@ public void testCollectWithQuantiles() {
200201
samples.add(new Collector.MetricFamilySamples.Sample("labels_and_quantiles", asList("l", "quantile"), asList("a", "0.99"), 2.0));
201202
samples.add(new Collector.MetricFamilySamples.Sample("labels_and_quantiles_count", asList("l"), asList("a"), 1.0));
202203
samples.add(new Collector.MetricFamilySamples.Sample("labels_and_quantiles_sum", asList("l"), asList("a"), 2.0));
204+
samples.add(new Collector.MetricFamilySamples.Sample("labels_and_quantiles_created", asList("l"), asList("a"), labelsAndQuantiles.labels("a").get().created / 1000.0));
203205
Collector.MetricFamilySamples mfsFixture = new Collector.MetricFamilySamples("labels_and_quantiles", Collector.Type.SUMMARY, "help", samples);
204206

205207
assertEquals(1, mfs.size());

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ public void testCounterOutput() throws IOException {
5757
assertEquals("# TYPE nolabels counter\n"
5858
+ "# HELP nolabels help\n"
5959
+ "nolabels_total 1.0\n"
60-
+ "# EOF\n", writer.toString());
60+
+ "nolabels_created 1234.0\n"
61+
+ "# EOF\n", writer.toString().replaceAll("_created [0-9E.]+", "_created 1234.0"));
6162
}
6263

6364
@Test
@@ -117,7 +118,8 @@ public void testSummaryOutput() throws IOException {
117118
+ "# HELP nolabels help\n"
118119
+ "nolabels_count 1.0\n"
119120
+ "nolabels_sum 2.0\n"
120-
+ "# EOF\n", writer.toString());
121+
+ "nolabels_created 1234.0\n"
122+
+ "# EOF\n", writer.toString().replaceAll("_created [0-9E.]+", "_created 1234.0"));
121123
}
122124

123125
@Test
@@ -135,7 +137,8 @@ public void testSummaryOutputWithQuantiles() throws IOException {
135137
+ "labelsAndQuantiles{l=\"a\",quantile=\"0.99\"} 2.0\n"
136138
+ "labelsAndQuantiles_count{l=\"a\"} 1.0\n"
137139
+ "labelsAndQuantiles_sum{l=\"a\"} 2.0\n"
138-
+ "# EOF\n", writer.toString());
140+
+ "labelsAndQuantiles_created{l=\"a\"} 1234.0\n"
141+
+ "# EOF\n", writer.toString().replaceAll("(_created\\{.*\\}) [0-9E.]+", "$1 1234.0"));
139142
}
140143

141144
@Test

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

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,11 @@ public void testCounterOutput() throws IOException {
5555
TextFormat.write004(writer, registry.metricFamilySamples());
5656
assertEquals("# HELP nolabels_total help\n"
5757
+ "# TYPE nolabels_total counter\n"
58-
+ "nolabels_total 1.0\n", writer.toString());
58+
+ "nolabels_total 1.0\n"
59+
+ "# HELP nolabels_created help\n"
60+
+ "# TYPE nolabels_created gauge\n"
61+
+ "nolabels_created 1234.0\n",
62+
writer.toString().replaceAll("_created [0-9E.]+", "_created 1234.0"));
5963
}
6064

6165
@Test
@@ -65,7 +69,11 @@ public void testCounterWithTotalOutput() throws IOException {
6569
TextFormat.write004(writer, registry.metricFamilySamples());
6670
assertEquals("# HELP nolabels_total help\n"
6771
+ "# TYPE nolabels_total counter\n"
68-
+ "nolabels_total 1.0\n", writer.toString());
72+
+ "nolabels_total 1.0\n"
73+
+ "# HELP nolabels_created help\n"
74+
+ "# TYPE nolabels_created gauge\n"
75+
+ "nolabels_created 1234.0\n",
76+
writer.toString().replaceAll("_created [0-9E.]+", "_created 1234.0"));
6977
}
7078

7179

@@ -107,7 +115,7 @@ public List<MetricFamilySamples> collect() {
107115
return mfs;
108116
}
109117
}
110-
118+
111119
new CustomCollector().register(registry);
112120
TextFormat.write004(writer, registry.metricFamilySamples());
113121
assertEquals("# HELP nolabels help\n"
@@ -123,7 +131,11 @@ public void testSummaryOutput() throws IOException {
123131
assertEquals("# HELP nolabels help\n"
124132
+ "# TYPE nolabels summary\n"
125133
+ "nolabels_count 1.0\n"
126-
+ "nolabels_sum 2.0\n", writer.toString());
134+
+ "nolabels_sum 2.0\n"
135+
+ "# HELP nolabels_created help\n"
136+
+ "# TYPE nolabels_created gauge\n"
137+
+ "nolabels_created 1234.0\n",
138+
writer.toString().replaceAll("_created [0-9E.]+", "_created 1234.0"));
127139
}
128140

129141
@Test
@@ -140,7 +152,11 @@ public void testSummaryOutputWithQuantiles() throws IOException {
140152
+ "labelsAndQuantiles{l=\"a\",quantile=\"0.9\",} 2.0\n"
141153
+ "labelsAndQuantiles{l=\"a\",quantile=\"0.99\",} 2.0\n"
142154
+ "labelsAndQuantiles_count{l=\"a\",} 1.0\n"
143-
+ "labelsAndQuantiles_sum{l=\"a\",} 2.0\n", writer.toString());
155+
+ "labelsAndQuantiles_sum{l=\"a\",} 2.0\n"
156+
+ "# HELP labelsAndQuantiles_created help\n"
157+
+ "# TYPE labelsAndQuantiles_created gauge\n"
158+
+ "labelsAndQuantiles_created{l=\"a\",} 1234.0\n",
159+
writer.toString().replaceAll("(_created\\{.*\\}) [0-9E.]+", "$1 1234.0"));
144160
}
145161

146162
@Test

0 commit comments

Comments
 (0)