Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support _created time series suppression #791

Merged
merged 8 commits into from
Jun 15, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
fstab marked this conversation as resolved.
Show resolved Hide resolved
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