-
Notifications
You must be signed in to change notification settings - Fork 804
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support PROMETHEUS_DISABLE_CREATED_SERIES env var to suppress `_creat…
…ed` time series
- Loading branch information
Showing
6 changed files
with
157 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
simpleclient/src/test/java/io/prometheus/client/DisableCreatedTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |