Skip to content

Commit

Permalink
Add OM output tests.
Browse files Browse the repository at this point in the history
Signed-off-by: Brian Brazil <[email protected]>
  • Loading branch information
brian-brazil committed Jan 18, 2021
1 parent d4f32a2 commit 706672a
Show file tree
Hide file tree
Showing 2 changed files with 183 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<MetricFamilySamples> collect() {
List<MetricFamilySamples> mfs = new ArrayList<MetricFamilySamples>();
ArrayList<String> labelNames = new ArrayList<String>();
ArrayList<String> labelValues = new ArrayList<String>();
ArrayList<MetricFamilySamples.Sample> samples = new ArrayList<Collector.MetricFamilySamples.Sample>();
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<MetricFamilySamples> collect() {
List<MetricFamilySamples> mfs = new ArrayList<MetricFamilySamples>();
ArrayList<String> labelNames = new ArrayList<String>();
ArrayList<String> labelValues = new ArrayList<String>();
ArrayList<MetricFamilySamples.Sample> samples = new ArrayList<Collector.MetricFamilySamples.Sample>();
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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
}

0 comments on commit 706672a

Please sign in to comment.