From 706672af5d7266b9b94b576adb4f2eebc99e12de Mon Sep 17 00:00:00 2001 From: Brian Brazil Date: Fri, 15 Jan 2021 13:36:23 +0000 Subject: [PATCH] Add OM output tests. Signed-off-by: Brian Brazil --- .../common/TextFormatOpenMetricsTest.java | 173 ++++++++++++++++++ .../exporter/common/TextFormatTest.java | 10 + 2 files changed, 183 insertions(+) create mode 100644 simpleclient_common/src/test/java/io/prometheus/client/exporter/common/TextFormatOpenMetricsTest.java diff --git a/simpleclient_common/src/test/java/io/prometheus/client/exporter/common/TextFormatOpenMetricsTest.java b/simpleclient_common/src/test/java/io/prometheus/client/exporter/common/TextFormatOpenMetricsTest.java new file mode 100644 index 000000000..c8e6f4848 --- /dev/null +++ b/simpleclient_common/src/test/java/io/prometheus/client/exporter/common/TextFormatOpenMetricsTest.java @@ -0,0 +1,173 @@ +package io.prometheus.client.exporter.common; + +import static org.junit.Assert.assertEquals; + +import java.io.IOException; +import java.io.StringWriter; +import java.util.ArrayList; +import java.util.List; + +import org.junit.Before; +import org.junit.Test; + +import io.prometheus.client.Collector; +import io.prometheus.client.CollectorRegistry; +import io.prometheus.client.Counter; +import io.prometheus.client.Gauge; +import io.prometheus.client.Summary; + + +public class TextFormatOpenMetricsTest { + CollectorRegistry registry; + StringWriter writer; + + @Before + public void setUp() { + registry = new CollectorRegistry(); + writer = new StringWriter(); + } + + @Test + public void testGaugeOutput() throws IOException { + Gauge noLabels = Gauge.build().name("nolabels").help("help").register(registry); + noLabels.inc(); + TextFormat.writeOpenMetrics100(writer, registry.metricFamilySamples()); + assertEquals("# TYPE nolabels gauge\n" + + "# HELP nolabels help\n" + + "nolabels 1.0\n" + + "# EOF\n", writer.toString()); + } + + @Test + public void testValueInfinity() throws IOException { + Gauge noLabels = Gauge.build().name("nolabels").help("help").register(registry); + noLabels.set(Double.POSITIVE_INFINITY); + TextFormat.writeOpenMetrics100(writer, registry.metricFamilySamples()); + assertEquals("# TYPE nolabels gauge\n" + + "# HELP nolabels help\n" + + "nolabels +Inf\n" + + "# EOF\n", writer.toString()); + } + + @Test + public void testCounterOutput() throws IOException { + Counter noLabels = Counter.build().name("nolabels").help("help").register(registry); + noLabels.inc(); + TextFormat.writeOpenMetrics100(writer, registry.metricFamilySamples()); + assertEquals("# TYPE nolabels counter\n" + + "# HELP nolabels help\n" + + "nolabels_total 1.0\n" + + "# EOF\n", writer.toString()); + } + + @Test + public void testCounterSamplesMissingTotal() throws IOException { + + class CustomCollector extends Collector { + public List collect() { + List mfs = new ArrayList(); + ArrayList labelNames = new ArrayList(); + ArrayList labelValues = new ArrayList(); + ArrayList samples = new ArrayList(); + MetricFamilySamples.Sample sample = new MetricFamilySamples.Sample("nolabels", labelNames, labelValues, 1.0); + samples.add(sample); + mfs.add(new MetricFamilySamples("nolabels", Collector.Type.COUNTER, "help", samples)); + return mfs; + } + } + + new CustomCollector().register(registry); + TextFormat.writeOpenMetrics100(writer, registry.metricFamilySamples()); + assertEquals("# TYPE nolabels counter\n" + + "# HELP nolabels help\n" + + "nolabels_total 1.0\n" + + "# EOF\n", writer.toString()); + } + + @Test + public void testMetricOutputWithTimestamp() throws IOException { + + class CustomCollector extends Collector { + public List collect() { + List mfs = new ArrayList(); + ArrayList labelNames = new ArrayList(); + ArrayList labelValues = new ArrayList(); + ArrayList samples = new ArrayList(); + MetricFamilySamples.Sample sample = new MetricFamilySamples.Sample("nolabels", labelNames, labelValues, 1.0, 1518123006L); + samples.add(sample); + mfs.add(new MetricFamilySamples("nolabels", Collector.Type.UNTYPED, "help", samples)); + return mfs; + } + } + + new CustomCollector().register(registry); + TextFormat.writeOpenMetrics100(writer, registry.metricFamilySamples()); + assertEquals("# TYPE nolabels unknown\n" + + "# HELP nolabels help\n" + + "nolabels 1.0 1518123.006\n" + + "# EOF\n", writer.toString()); + } + + @Test + public void testSummaryOutput() throws IOException { + Summary noLabels = Summary.build().name("nolabels").help("help").register(registry); + noLabels.observe(2); + TextFormat.writeOpenMetrics100(writer, registry.metricFamilySamples()); + assertEquals("# TYPE nolabels summary\n" + + "# HELP nolabels help\n" + + "nolabels_count 1.0\n" + + "nolabels_sum 2.0\n" + + "# EOF\n", writer.toString()); + } + + @Test + public void testSummaryOutputWithQuantiles() throws IOException { + Summary labelsAndQuantiles = Summary.build() + .quantile(0.5, 0.05).quantile(0.9, 0.01).quantile(0.99, 0.001) + .labelNames("l").name("labelsAndQuantiles").help("help").register(registry); + labelsAndQuantiles.labels("a").observe(2); + writer = new StringWriter(); + TextFormat.writeOpenMetrics100(writer, registry.metricFamilySamples()); + assertEquals("# TYPE labelsAndQuantiles summary\n" + + "# HELP labelsAndQuantiles help\n" + + "labelsAndQuantiles{l=\"a\",quantile=\"0.5\"} 2.0\n" + + "labelsAndQuantiles{l=\"a\",quantile=\"0.9\"} 2.0\n" + + "labelsAndQuantiles{l=\"a\",quantile=\"0.99\"} 2.0\n" + + "labelsAndQuantiles_count{l=\"a\"} 1.0\n" + + "labelsAndQuantiles_sum{l=\"a\"} 2.0\n" + + "# EOF\n", writer.toString()); + } + + @Test + public void testLabelsOutput() throws IOException { + Gauge labels = Gauge.build().name("labels").help("help").labelNames("l").register(registry); + labels.labels("a").inc(); + TextFormat.writeOpenMetrics100(writer, registry.metricFamilySamples()); + assertEquals("# TYPE labels gauge\n" + + "# HELP labels help\n" + + "labels{l=\"a\"} 1.0\n" + + "# EOF\n", writer.toString()); + } + + @Test + public void testLabelValuesEscaped() throws IOException { + Gauge labels = Gauge.build().name("labels").help("help").labelNames("l").register(registry); + labels.labels("ąćčęntěd a\nb\\c\"d").inc(); + TextFormat.writeOpenMetrics100(writer, registry.metricFamilySamples()); + assertEquals("# TYPE labels gauge\n" + + "# HELP labels help\n" + + "labels{l=\"ąćčęntěd a\\nb\\\\c\\\"d\"} 1.0\n" + + "# EOF\n", writer.toString()); + } + + @Test + public void testHelpEscaped() throws IOException { + Gauge noLabels = Gauge.build().name("nolabels").help("ąćčęntěd h\"e\\l\np").register(registry); + noLabels.inc(); + TextFormat.writeOpenMetrics100(writer, registry.metricFamilySamples()); + assertEquals("# TYPE nolabels gauge\n" + + "# HELP nolabels ąćčęntěd h\\\"e\\\\l\\np\n" + + "nolabels 1.0\n" + + "# EOF\n", writer.toString()); + } +} diff --git a/simpleclient_common/src/test/java/io/prometheus/client/exporter/common/TextFormatTest.java b/simpleclient_common/src/test/java/io/prometheus/client/exporter/common/TextFormatTest.java index f1886eeb7..b98972a26 100644 --- a/simpleclient_common/src/test/java/io/prometheus/client/exporter/common/TextFormatTest.java +++ b/simpleclient_common/src/test/java/io/prometheus/client/exporter/common/TextFormatTest.java @@ -160,4 +160,14 @@ public void testHelpEscaped() throws IOException { + "# TYPE nolabels gauge\n" + "nolabels 1.0\n", writer.toString()); } + + @Test + public void testChooseContentType() throws IOException { + assertEquals(TextFormat.CONTENT_TYPE_004, TextFormat.chooseContentType(null)); + assertEquals(TextFormat.CONTENT_TYPE_004, TextFormat.chooseContentType("")); + assertEquals(TextFormat.CONTENT_TYPE_004, TextFormat.chooseContentType("text/plain;version=0.0.4")); + assertEquals(TextFormat.CONTENT_TYPE_004, TextFormat.chooseContentType("foo")); + assertEquals(TextFormat.CONTENT_TYPE_OPENMETRICS_100, TextFormat.chooseContentType("application/openmetrics-text; version=0.0.1,text/plain;version=0.0.4;q=0.5,*/*;q=0.1")); + assertEquals(TextFormat.CONTENT_TYPE_OPENMETRICS_100, TextFormat.chooseContentType("application/openmetrics-text; version=1.0.0")); + } }