Skip to content

Commit 706672a

Browse files
committed
Add OM output tests.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
1 parent d4f32a2 commit 706672a

File tree

2 files changed

+183
-0
lines changed

2 files changed

+183
-0
lines changed
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
package io.prometheus.client.exporter.common;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import java.io.IOException;
6+
import java.io.StringWriter;
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
10+
import org.junit.Before;
11+
import org.junit.Test;
12+
13+
import io.prometheus.client.Collector;
14+
import io.prometheus.client.CollectorRegistry;
15+
import io.prometheus.client.Counter;
16+
import io.prometheus.client.Gauge;
17+
import io.prometheus.client.Summary;
18+
19+
20+
public class TextFormatOpenMetricsTest {
21+
CollectorRegistry registry;
22+
StringWriter writer;
23+
24+
@Before
25+
public void setUp() {
26+
registry = new CollectorRegistry();
27+
writer = new StringWriter();
28+
}
29+
30+
@Test
31+
public void testGaugeOutput() throws IOException {
32+
Gauge noLabels = Gauge.build().name("nolabels").help("help").register(registry);
33+
noLabels.inc();
34+
TextFormat.writeOpenMetrics100(writer, registry.metricFamilySamples());
35+
assertEquals("# TYPE nolabels gauge\n"
36+
+ "# HELP nolabels help\n"
37+
+ "nolabels 1.0\n"
38+
+ "# EOF\n", writer.toString());
39+
}
40+
41+
@Test
42+
public void testValueInfinity() throws IOException {
43+
Gauge noLabels = Gauge.build().name("nolabels").help("help").register(registry);
44+
noLabels.set(Double.POSITIVE_INFINITY);
45+
TextFormat.writeOpenMetrics100(writer, registry.metricFamilySamples());
46+
assertEquals("# TYPE nolabels gauge\n"
47+
+ "# HELP nolabels help\n"
48+
+ "nolabels +Inf\n"
49+
+ "# EOF\n", writer.toString());
50+
}
51+
52+
@Test
53+
public void testCounterOutput() throws IOException {
54+
Counter noLabels = Counter.build().name("nolabels").help("help").register(registry);
55+
noLabels.inc();
56+
TextFormat.writeOpenMetrics100(writer, registry.metricFamilySamples());
57+
assertEquals("# TYPE nolabels counter\n"
58+
+ "# HELP nolabels help\n"
59+
+ "nolabels_total 1.0\n"
60+
+ "# EOF\n", writer.toString());
61+
}
62+
63+
@Test
64+
public void testCounterSamplesMissingTotal() throws IOException {
65+
66+
class CustomCollector extends Collector {
67+
public List<MetricFamilySamples> collect() {
68+
List<MetricFamilySamples> mfs = new ArrayList<MetricFamilySamples>();
69+
ArrayList<String> labelNames = new ArrayList<String>();
70+
ArrayList<String> labelValues = new ArrayList<String>();
71+
ArrayList<MetricFamilySamples.Sample> samples = new ArrayList<Collector.MetricFamilySamples.Sample>();
72+
MetricFamilySamples.Sample sample = new MetricFamilySamples.Sample("nolabels", labelNames, labelValues, 1.0);
73+
samples.add(sample);
74+
mfs.add(new MetricFamilySamples("nolabels", Collector.Type.COUNTER, "help", samples));
75+
return mfs;
76+
}
77+
}
78+
79+
new CustomCollector().register(registry);
80+
TextFormat.writeOpenMetrics100(writer, registry.metricFamilySamples());
81+
assertEquals("# TYPE nolabels counter\n"
82+
+ "# HELP nolabels help\n"
83+
+ "nolabels_total 1.0\n"
84+
+ "# EOF\n", writer.toString());
85+
}
86+
87+
@Test
88+
public void testMetricOutputWithTimestamp() throws IOException {
89+
90+
class CustomCollector extends Collector {
91+
public List<MetricFamilySamples> collect() {
92+
List<MetricFamilySamples> mfs = new ArrayList<MetricFamilySamples>();
93+
ArrayList<String> labelNames = new ArrayList<String>();
94+
ArrayList<String> labelValues = new ArrayList<String>();
95+
ArrayList<MetricFamilySamples.Sample> samples = new ArrayList<Collector.MetricFamilySamples.Sample>();
96+
MetricFamilySamples.Sample sample = new MetricFamilySamples.Sample("nolabels", labelNames, labelValues, 1.0, 1518123006L);
97+
samples.add(sample);
98+
mfs.add(new MetricFamilySamples("nolabels", Collector.Type.UNTYPED, "help", samples));
99+
return mfs;
100+
}
101+
}
102+
103+
new CustomCollector().register(registry);
104+
TextFormat.writeOpenMetrics100(writer, registry.metricFamilySamples());
105+
assertEquals("# TYPE nolabels unknown\n"
106+
+ "# HELP nolabels help\n"
107+
+ "nolabels 1.0 1518123.006\n"
108+
+ "# EOF\n", writer.toString());
109+
}
110+
111+
@Test
112+
public void testSummaryOutput() throws IOException {
113+
Summary noLabels = Summary.build().name("nolabels").help("help").register(registry);
114+
noLabels.observe(2);
115+
TextFormat.writeOpenMetrics100(writer, registry.metricFamilySamples());
116+
assertEquals("# TYPE nolabels summary\n"
117+
+ "# HELP nolabels help\n"
118+
+ "nolabels_count 1.0\n"
119+
+ "nolabels_sum 2.0\n"
120+
+ "# EOF\n", writer.toString());
121+
}
122+
123+
@Test
124+
public void testSummaryOutputWithQuantiles() throws IOException {
125+
Summary labelsAndQuantiles = Summary.build()
126+
.quantile(0.5, 0.05).quantile(0.9, 0.01).quantile(0.99, 0.001)
127+
.labelNames("l").name("labelsAndQuantiles").help("help").register(registry);
128+
labelsAndQuantiles.labels("a").observe(2);
129+
writer = new StringWriter();
130+
TextFormat.writeOpenMetrics100(writer, registry.metricFamilySamples());
131+
assertEquals("# TYPE labelsAndQuantiles summary\n"
132+
+ "# HELP labelsAndQuantiles help\n"
133+
+ "labelsAndQuantiles{l=\"a\",quantile=\"0.5\"} 2.0\n"
134+
+ "labelsAndQuantiles{l=\"a\",quantile=\"0.9\"} 2.0\n"
135+
+ "labelsAndQuantiles{l=\"a\",quantile=\"0.99\"} 2.0\n"
136+
+ "labelsAndQuantiles_count{l=\"a\"} 1.0\n"
137+
+ "labelsAndQuantiles_sum{l=\"a\"} 2.0\n"
138+
+ "# EOF\n", writer.toString());
139+
}
140+
141+
@Test
142+
public void testLabelsOutput() throws IOException {
143+
Gauge labels = Gauge.build().name("labels").help("help").labelNames("l").register(registry);
144+
labels.labels("a").inc();
145+
TextFormat.writeOpenMetrics100(writer, registry.metricFamilySamples());
146+
assertEquals("# TYPE labels gauge\n"
147+
+ "# HELP labels help\n"
148+
+ "labels{l=\"a\"} 1.0\n"
149+
+ "# EOF\n", writer.toString());
150+
}
151+
152+
@Test
153+
public void testLabelValuesEscaped() throws IOException {
154+
Gauge labels = Gauge.build().name("labels").help("help").labelNames("l").register(registry);
155+
labels.labels("ąćčęntěd a\nb\\c\"d").inc();
156+
TextFormat.writeOpenMetrics100(writer, registry.metricFamilySamples());
157+
assertEquals("# TYPE labels gauge\n"
158+
+ "# HELP labels help\n"
159+
+ "labels{l=\"ąćčęntěd a\\nb\\\\c\\\"d\"} 1.0\n"
160+
+ "# EOF\n", writer.toString());
161+
}
162+
163+
@Test
164+
public void testHelpEscaped() throws IOException {
165+
Gauge noLabels = Gauge.build().name("nolabels").help("ąćčęntěd h\"e\\l\np").register(registry);
166+
noLabels.inc();
167+
TextFormat.writeOpenMetrics100(writer, registry.metricFamilySamples());
168+
assertEquals("# TYPE nolabels gauge\n"
169+
+ "# HELP nolabels ąćčęntěd h\\\"e\\\\l\\np\n"
170+
+ "nolabels 1.0\n"
171+
+ "# EOF\n", writer.toString());
172+
}
173+
}

simpleclient_common/src/test/java/io/prometheus/client/exporter/common/TextFormatTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,4 +160,14 @@ public void testHelpEscaped() throws IOException {
160160
+ "# TYPE nolabels gauge\n"
161161
+ "nolabels 1.0\n", writer.toString());
162162
}
163+
164+
@Test
165+
public void testChooseContentType() throws IOException {
166+
assertEquals(TextFormat.CONTENT_TYPE_004, TextFormat.chooseContentType(null));
167+
assertEquals(TextFormat.CONTENT_TYPE_004, TextFormat.chooseContentType(""));
168+
assertEquals(TextFormat.CONTENT_TYPE_004, TextFormat.chooseContentType("text/plain;version=0.0.4"));
169+
assertEquals(TextFormat.CONTENT_TYPE_004, TextFormat.chooseContentType("foo"));
170+
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"));
171+
assertEquals(TextFormat.CONTENT_TYPE_OPENMETRICS_100, TextFormat.chooseContentType("application/openmetrics-text; version=1.0.0"));
172+
}
163173
}

0 commit comments

Comments
 (0)