-
Notifications
You must be signed in to change notification settings - Fork 800
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Brian Brazil <[email protected]>
- Loading branch information
1 parent
d4f32a2
commit 706672a
Showing
2 changed files
with
183 additions
and
0 deletions.
There are no files selected for viewing
173 changes: 173 additions & 0 deletions
173
..._common/src/test/java/io/prometheus/client/exporter/common/TextFormatOpenMetricsTest.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,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()); | ||
} | ||
} |
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