|
| 1 | +package io.prometheus.client; |
| 2 | + |
| 3 | +import org.junit.Assert; |
| 4 | +import org.junit.Before; |
| 5 | +import org.junit.Test; |
| 6 | +import uk.org.webcompere.systemstubs.environment.EnvironmentVariables; |
| 7 | + |
| 8 | +import java.util.Collections; |
| 9 | +import java.util.List; |
| 10 | + |
| 11 | +public class DisableCreatedTest { |
| 12 | + |
| 13 | + private CollectorRegistry registry; |
| 14 | + |
| 15 | + @Before |
| 16 | + public void setUp() { |
| 17 | + registry = new CollectorRegistry(true); |
| 18 | + } |
| 19 | + |
| 20 | + @Test |
| 21 | + public void testDisableCreatedAll() { |
| 22 | + EnvironmentVariables env = new EnvironmentVariables(Collector.DISABLE_CREATED_SERIES, "true"); |
| 23 | + Counter counter1 = Counter.build() |
| 24 | + .name("counter1") |
| 25 | + .help("test counter 1") |
| 26 | + .labelNames("path") |
| 27 | + .register(registry); |
| 28 | + counter1.labels("/hello").inc(); |
| 29 | + counter1.labels("/goodbye").inc(); |
| 30 | + Counter counter2 = Counter.build() |
| 31 | + .name("counter2") |
| 32 | + .help("test counter 2") |
| 33 | + .register(registry); |
| 34 | + counter2.inc(); |
| 35 | + |
| 36 | + Histogram histogram = Histogram.build() |
| 37 | + .name("test_histogram") |
| 38 | + .help("test histogram") |
| 39 | + .create(); |
| 40 | + histogram.observe(100); |
| 41 | + histogram.observe(200); |
| 42 | + registry.register(histogram); |
| 43 | + |
| 44 | + Summary noLabels = Summary.build().name("test_summary").help("test summary").register(registry); |
| 45 | + noLabels.observe(2); |
| 46 | + |
| 47 | + List<Collector.MetricFamilySamples> mfsList; |
| 48 | + try { |
| 49 | + env.setup(); |
| 50 | + mfsList = Collections.list(registry.metricFamilySamples()); |
| 51 | + String extracted = System.getenv(Collector.DISABLE_CREATED_SERIES); |
| 52 | + Assert.assertEquals("Env isn't set", "true", extracted); |
| 53 | + env.teardown(); |
| 54 | + } catch (Exception e) { |
| 55 | + throw new RuntimeException(e); |
| 56 | + } |
| 57 | + assertSamplesInclude(mfsList, "counter1_total", 2); |
| 58 | + assertSamplesInclude(mfsList, "counter1_created", 0); |
| 59 | + assertSamplesInclude(mfsList, "counter2_total", 1); |
| 60 | + assertSamplesInclude(mfsList, "counter2_created", 0); |
| 61 | + assertSamplesInclude(mfsList, "test_histogram_bucket", 15); |
| 62 | + assertSamplesInclude(mfsList, "test_histogram_count", 1); |
| 63 | + assertSamplesInclude(mfsList, "test_histogram_sum", 1); |
| 64 | + assertSamplesInclude(mfsList, "test_histogram_created", 0); |
| 65 | + assertSamplesInclude(mfsList, "test_summary_count", 1); |
| 66 | + assertSamplesInclude(mfsList, "test_summary_sum", 1); |
| 67 | + assertSamplesInclude(mfsList, "test_summary_created", 0); |
| 68 | + assertTotalNumberOfSamples(mfsList, 22); |
| 69 | + } |
| 70 | + |
| 71 | + private void assertSamplesInclude(List<Collector.MetricFamilySamples> mfsList, String name, int times) { |
| 72 | + int count = 0; |
| 73 | + for (Collector.MetricFamilySamples mfs : mfsList) { |
| 74 | + for (Collector.MetricFamilySamples.Sample sample : mfs.samples) { |
| 75 | + if (sample.name.equals(name)) { |
| 76 | + count++; |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + Assert.assertEquals("Wrong number of samples for " + name, times, count); |
| 81 | + } |
| 82 | + |
| 83 | + private void assertTotalNumberOfSamples(List<Collector.MetricFamilySamples> mfsList, int n) { |
| 84 | + int count = 0; |
| 85 | + for (Collector.MetricFamilySamples mfs : mfsList) { |
| 86 | + count += mfs.samples.size(); |
| 87 | + } |
| 88 | + Assert.assertEquals("Wrong total number of samples", n, count); |
| 89 | + } |
| 90 | +} |
0 commit comments