Skip to content

Commit

Permalink
Support _created time series suppression (#791)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
mindw authored Jun 15, 2022
1 parent 75baa06 commit 6730f3e
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 7 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ There are canonical examples defined in the class definition Javadoc of the clie
Documentation can be found at the [Java Client
Github Project Page](http://prometheus.github.io/client_java).

### Disabling `_created` metrics

By default, counters, histograms, and summaries export an additional series
suffixed with `_created` and a value of the unix timestamp for when the metric
was created. If this information is not helpful, it can be disabled by setting
the environment variable `PROMETHEUS_DISABLE_CREATED_SERIES=true`.

## Instrumenting

Four types of metrics are offered: Counter, Gauge, Summary and Histogram.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@

import io.prometheus.client.exemplars.Exemplar;

import java.util.ArrayList;
import java.util.List;
import java.util.*;
import java.util.regex.Pattern;

/**
Expand Down
4 changes: 3 additions & 1 deletion simpleclient/src/main/java/io/prometheus/client/Counter.java
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,9 @@ public List<MetricFamilySamples> collect() {
List<MetricFamilySamples.Sample> samples = new ArrayList<MetricFamilySamples.Sample>(children.size());
for(Map.Entry<List<String>, Child> c: children.entrySet()) {
samples.add(new MetricFamilySamples.Sample(fullname + "_total", labelNames, c.getKey(), c.getValue().get(), c.getValue().getExemplar()));
samples.add(new MetricFamilySamples.Sample(fullname + "_created", labelNames, c.getKey(), c.getValue().created() / 1000.0));
if (Environment.includeCreatedSeries()) {
samples.add(new MetricFamilySamples.Sample(fullname + "_created", labelNames, c.getKey(), c.getValue().created() / 1000.0));
}
}
return familySamplesList(Type.COUNTER, samples);
}
Expand Down
23 changes: 23 additions & 0 deletions simpleclient/src/main/java/io/prometheus/client/Environment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.prometheus.client;

import java.util.Arrays;
import java.util.List;

class Environment {

private static final String DISABLE_CREATED_SERIES = "PROMETHEUS_DISABLE_CREATED_SERIES";
private static final List<String> DISABLE_CREATED_SERIES_TRUE = Arrays.asList("true", "1", "t");
private static final boolean includeCreatedSeries = !isTrue(DISABLE_CREATED_SERIES);

static boolean includeCreatedSeries() {
return includeCreatedSeries;
}

private static boolean isTrue(String envVarName) {
String stringValue = System.getenv(envVarName);
if (stringValue != null) {
return DISABLE_CREATED_SERIES_TRUE.contains(stringValue.toLowerCase());
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,9 @@ public List<MetricFamilySamples> collect() {
}
samples.add(new MetricFamilySamples.Sample(fullname + "_count", labelNames, c.getKey(), v.buckets[buckets.length-1]));
samples.add(new MetricFamilySamples.Sample(fullname + "_sum", labelNames, c.getKey(), v.sum));
samples.add(new MetricFamilySamples.Sample(fullname + "_created", labelNames, c.getKey(), v.created / 1000.0));
if (Environment.includeCreatedSeries()) {
samples.add(new MetricFamilySamples.Sample(fullname + "_created", labelNames, c.getKey(), v.created / 1000.0));
}
}

return familySamplesList(Type.HISTOGRAM, samples);
Expand Down
4 changes: 3 additions & 1 deletion simpleclient/src/main/java/io/prometheus/client/Summary.java
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,9 @@ public List<MetricFamilySamples> collect() {
}
samples.add(new MetricFamilySamples.Sample(fullname + "_count", labelNames, c.getKey(), v.count));
samples.add(new MetricFamilySamples.Sample(fullname + "_sum", labelNames, c.getKey(), v.sum));
samples.add(new MetricFamilySamples.Sample(fullname + "_created", labelNames, c.getKey(), v.created / 1000.0));
if (Environment.includeCreatedSeries()) {
samples.add(new MetricFamilySamples.Sample(fullname + "_created", labelNames, c.getKey(), v.created / 1000.0));
}
}

return familySamplesList(Type.SUMMARY, samples);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ private void assertExemplar(Histogram histogram, double value, String... labels)
}
if (lowerBound < value && value <= upperBound) {
Assert.assertNotNull("No exemplar found in bucket [" + lowerBound + ", " + upperBound + "]", bucket.exemplar);
Assert.assertEquals(value, bucket.exemplar.getValue(), 0.001);
Assert.assertEquals(value, bucket.exemplar.getValue(), 0.01);
Assert.assertEquals(labels.length/2, bucket.exemplar.getNumberOfLabels());
for (int i=0; i<labels.length; i+=2) {
Assert.assertEquals(labels[i], bucket.exemplar.getLabelName(i/2));
Expand Down Expand Up @@ -327,7 +327,7 @@ private void assertNoExemplar(Histogram histogram, double value) {
}
if (lowerBound < value && value <= upperBound) {
if (bucket.exemplar != null) {
Assert.assertNotEquals("expecting no exemplar with value " + value, value, bucket.exemplar.getValue(), 0.001);
Assert.assertNotEquals("expecting no exemplar with value " + value, value, bucket.exemplar.getValue(), 0.0001);
}
}
}
Expand Down

0 comments on commit 6730f3e

Please sign in to comment.