Skip to content

Commit 6730f3e

Browse files
authored
Support _created time series suppression (#791)
* Support PROMETHEUS_DISABLE_CREATED_SERIES env var to suppress `_created` time series Signed-off-by: Gabi Davar <[email protected]> * switched to use a static variable. test tweaks. Signed-off-by: Gabi Davar <[email protected]> * docs Signed-off-by: Gabi Davar <[email protected]> * let's be truer. Signed-off-by: Gabi Davar <[email protected]> * Revert GetNames() redundant change. Signed-off-by: Gabi Davar <[email protected]> * make final, drop test for now Signed-off-by: Gabi Davar <[email protected]> * remove the extra deps too. Signed-off-by: Gabi Davar <[email protected]> * Add a brand new and shiny class Signed-off-by: Gabi Davar <[email protected]>
1 parent 75baa06 commit 6730f3e

File tree

7 files changed

+42
-7
lines changed

7 files changed

+42
-7
lines changed

README.md

+7
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,13 @@ There are canonical examples defined in the class definition Javadoc of the clie
7474
Documentation can be found at the [Java Client
7575
Github Project Page](http://prometheus.github.io/client_java).
7676

77+
### Disabling `_created` metrics
78+
79+
By default, counters, histograms, and summaries export an additional series
80+
suffixed with `_created` and a value of the unix timestamp for when the metric
81+
was created. If this information is not helpful, it can be disabled by setting
82+
the environment variable `PROMETHEUS_DISABLE_CREATED_SERIES=true`.
83+
7784
## Instrumenting
7885

7986
Four types of metrics are offered: Counter, Gauge, Summary and Histogram.

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33

44
import io.prometheus.client.exemplars.Exemplar;
55

6-
import java.util.ArrayList;
7-
import java.util.List;
6+
import java.util.*;
87
import java.util.regex.Pattern;
98

109
/**

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,9 @@ public List<MetricFamilySamples> collect() {
355355
List<MetricFamilySamples.Sample> samples = new ArrayList<MetricFamilySamples.Sample>(children.size());
356356
for(Map.Entry<List<String>, Child> c: children.entrySet()) {
357357
samples.add(new MetricFamilySamples.Sample(fullname + "_total", labelNames, c.getKey(), c.getValue().get(), c.getValue().getExemplar()));
358-
samples.add(new MetricFamilySamples.Sample(fullname + "_created", labelNames, c.getKey(), c.getValue().created() / 1000.0));
358+
if (Environment.includeCreatedSeries()) {
359+
samples.add(new MetricFamilySamples.Sample(fullname + "_created", labelNames, c.getKey(), c.getValue().created() / 1000.0));
360+
}
359361
}
360362
return familySamplesList(Type.COUNTER, samples);
361363
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package io.prometheus.client;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
class Environment {
7+
8+
private static final String DISABLE_CREATED_SERIES = "PROMETHEUS_DISABLE_CREATED_SERIES";
9+
private static final List<String> DISABLE_CREATED_SERIES_TRUE = Arrays.asList("true", "1", "t");
10+
private static final boolean includeCreatedSeries = !isTrue(DISABLE_CREATED_SERIES);
11+
12+
static boolean includeCreatedSeries() {
13+
return includeCreatedSeries;
14+
}
15+
16+
private static boolean isTrue(String envVarName) {
17+
String stringValue = System.getenv(envVarName);
18+
if (stringValue != null) {
19+
return DISABLE_CREATED_SERIES_TRUE.contains(stringValue.toLowerCase());
20+
}
21+
return false;
22+
}
23+
}

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,9 @@ public List<MetricFamilySamples> collect() {
574574
}
575575
samples.add(new MetricFamilySamples.Sample(fullname + "_count", labelNames, c.getKey(), v.buckets[buckets.length-1]));
576576
samples.add(new MetricFamilySamples.Sample(fullname + "_sum", labelNames, c.getKey(), v.sum));
577-
samples.add(new MetricFamilySamples.Sample(fullname + "_created", labelNames, c.getKey(), v.created / 1000.0));
577+
if (Environment.includeCreatedSeries()) {
578+
samples.add(new MetricFamilySamples.Sample(fullname + "_created", labelNames, c.getKey(), v.created / 1000.0));
579+
}
578580
}
579581

580582
return familySamplesList(Type.HISTOGRAM, samples);

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,9 @@ public List<MetricFamilySamples> collect() {
404404
}
405405
samples.add(new MetricFamilySamples.Sample(fullname + "_count", labelNames, c.getKey(), v.count));
406406
samples.add(new MetricFamilySamples.Sample(fullname + "_sum", labelNames, c.getKey(), v.sum));
407-
samples.add(new MetricFamilySamples.Sample(fullname + "_created", labelNames, c.getKey(), v.created / 1000.0));
407+
if (Environment.includeCreatedSeries()) {
408+
samples.add(new MetricFamilySamples.Sample(fullname + "_created", labelNames, c.getKey(), v.created / 1000.0));
409+
}
408410
}
409411

410412
return familySamplesList(Type.SUMMARY, samples);

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ private void assertExemplar(Histogram histogram, double value, String... labels)
299299
}
300300
if (lowerBound < value && value <= upperBound) {
301301
Assert.assertNotNull("No exemplar found in bucket [" + lowerBound + ", " + upperBound + "]", bucket.exemplar);
302-
Assert.assertEquals(value, bucket.exemplar.getValue(), 0.001);
302+
Assert.assertEquals(value, bucket.exemplar.getValue(), 0.01);
303303
Assert.assertEquals(labels.length/2, bucket.exemplar.getNumberOfLabels());
304304
for (int i=0; i<labels.length; i+=2) {
305305
Assert.assertEquals(labels[i], bucket.exemplar.getLabelName(i/2));
@@ -327,7 +327,7 @@ private void assertNoExemplar(Histogram histogram, double value) {
327327
}
328328
if (lowerBound < value && value <= upperBound) {
329329
if (bucket.exemplar != null) {
330-
Assert.assertNotEquals("expecting no exemplar with value " + value, value, bucket.exemplar.getValue(), 0.001);
330+
Assert.assertNotEquals("expecting no exemplar with value " + value, value, bucket.exemplar.getValue(), 0.0001);
331331
}
332332
}
333333
}

0 commit comments

Comments
 (0)