Skip to content

Commit

Permalink
Support PROMETHEUS_DISABLE_CREATED_SERIES env var to suppress `_creat…
Browse files Browse the repository at this point in the history
…ed` time series
  • Loading branch information
mindw committed Jun 5, 2022
1 parent 2be241c commit 175fec6
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 34 deletions.
18 changes: 18 additions & 0 deletions simpleclient/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,24 @@
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<version>3.12.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>uk.org.webcompere</groupId>
<artifactId>system-stubs-core</artifactId>
<version>2.0.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>uk.org.webcompere</groupId>
<artifactId>system-stubs-junit4</artifactId>
<version>2.0.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-math3</artifactId>
Expand Down
71 changes: 40 additions & 31 deletions simpleclient/src/main/java/io/prometheus/client/Collector.java
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 Expand Up @@ -151,43 +150,42 @@ public MetricFamilySamples filter(Predicate<String> sampleNameFilter) {
* we include the name without suffix here as well.
*/
public String[] getNames() {
List<String> names = new ArrayList<String>();
switch (type) {
case COUNTER:
return new String[]{
name + "_total",
name + "_created",
name
};
names.add(name + "_total");
if (getUseCreated()) {
names.add(name + "_created");
}
break;
case SUMMARY:
return new String[]{
name + "_count",
name + "_sum",
name + "_created",
name
};
names.add(name + "_count");
names.add(name + "_sum");
if (getUseCreated()) {
names.add(name + "_created");
}
break;
case HISTOGRAM:
return new String[]{
name + "_count",
name + "_sum",
name + "_bucket",
name + "_created",
name
};
names.add(name + "_count");
names.add(name + "_sum");
names.add(name + "_bucket");
if (getUseCreated()) {
names.add(name + "_created");
}
break;
case GAUGE_HISTOGRAM:
return new String[]{
name + "_gcount",
name + "_gsum",
name + "_bucket",
name
};
names.add(name + "_gcount");
names.add(name + "_gsum");
names.add(name + "_bucket");
break;
case INFO:
return new String[]{
name + "_info",
name
};
names.add(name + "_info");
break;
default:
return new String[]{name};
// NOP - `name` is added to all
}
names.add(name);
return names.toArray(new String[0]);
}


Expand Down Expand Up @@ -396,4 +394,15 @@ public static String doubleToGoString(double d) {
}
return Double.toString(d);
}

protected static final String DISABLE_CREATED_SERIES = "PROMETHEUS_DISABLE_CREATED_SERIES";
private static final List<String> TRUTHS = Arrays.asList("true", "1", "t");

protected static boolean getUseCreated() {
String disable_series = System.getenv(DISABLE_CREATED_SERIES);
if (disable_series != null) {
return !TRUTHS.contains(disable_series.toLowerCase());
}
return true;
}
}
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 (getUseCreated()) {
samples.add(new MetricFamilySamples.Sample(fullname + "_created", labelNames, c.getKey(), c.getValue().created() / 1000.0));
}
}
return familySamplesList(Type.COUNTER, samples);
}
Expand Down
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 (getUseCreated()) {
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 (getUseCreated()) {
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
@@ -0,0 +1,90 @@
package io.prometheus.client;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import uk.org.webcompere.systemstubs.environment.EnvironmentVariables;

import java.util.Collections;
import java.util.List;

public class DisableCreatedTest {

private CollectorRegistry registry;

@Before
public void setUp() {
registry = new CollectorRegistry(true);
}

@Test
public void testDisableCreatedAll() {
EnvironmentVariables env = new EnvironmentVariables(Collector.DISABLE_CREATED_SERIES, "true");
Counter counter1 = Counter.build()
.name("counter1")
.help("test counter 1")
.labelNames("path")
.register(registry);
counter1.labels("/hello").inc();
counter1.labels("/goodbye").inc();
Counter counter2 = Counter.build()
.name("counter2")
.help("test counter 2")
.register(registry);
counter2.inc();

Histogram histogram = Histogram.build()
.name("test_histogram")
.help("test histogram")
.create();
histogram.observe(100);
histogram.observe(200);
registry.register(histogram);

Summary noLabels = Summary.build().name("test_summary").help("test summary").register(registry);
noLabels.observe(2);

List<Collector.MetricFamilySamples> mfsList;
try {
env.setup();
mfsList = Collections.list(registry.metricFamilySamples());
String extracted = System.getenv(Collector.DISABLE_CREATED_SERIES);
Assert.assertEquals("Env isn't set", "true", extracted);
env.teardown();
} catch (Exception e) {
throw new RuntimeException(e);
}
assertSamplesInclude(mfsList, "counter1_total", 2);
assertSamplesInclude(mfsList, "counter1_created", 0);
assertSamplesInclude(mfsList, "counter2_total", 1);
assertSamplesInclude(mfsList, "counter2_created", 0);
assertSamplesInclude(mfsList, "test_histogram_bucket", 15);
assertSamplesInclude(mfsList, "test_histogram_count", 1);
assertSamplesInclude(mfsList, "test_histogram_sum", 1);
assertSamplesInclude(mfsList, "test_histogram_created", 0);
assertSamplesInclude(mfsList, "test_summary_count", 1);
assertSamplesInclude(mfsList, "test_summary_sum", 1);
assertSamplesInclude(mfsList, "test_summary_created", 0);
assertTotalNumberOfSamples(mfsList, 22);
}

private void assertSamplesInclude(List<Collector.MetricFamilySamples> mfsList, String name, int times) {
int count = 0;
for (Collector.MetricFamilySamples mfs : mfsList) {
for (Collector.MetricFamilySamples.Sample sample : mfs.samples) {
if (sample.name.equals(name)) {
count++;
}
}
}
Assert.assertEquals("Wrong number of samples for " + name, times, count);
}

private void assertTotalNumberOfSamples(List<Collector.MetricFamilySamples> mfsList, int n) {
int count = 0;
for (Collector.MetricFamilySamples mfs : mfsList) {
count += mfs.samples.size();
}
Assert.assertEquals("Wrong total number of samples", n, count);
}
}

0 comments on commit 175fec6

Please sign in to comment.