Skip to content

Commit

Permalink
Add handling for gsum/gcount/created in Prometheus exposition format.
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 706672a commit 801f91e
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@

import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import io.prometheus.client.Collector;

Expand Down Expand Up @@ -53,31 +58,47 @@ public static void writeFormat(String contentType, Writer writer, Enumeration<Co
* Write out the text version 0.0.4 of the given MetricFamilySamples.
*/
public static void write004(Writer writer, Enumeration<Collector.MetricFamilySamples> mfs) throws IOException {
Map<String, Collector.MetricFamilySamples> omFamilies = new TreeMap<String, Collector.MetricFamilySamples>();
/* See http://prometheus.io/docs/instrumenting/exposition_formats/
* for the output format specification. */
while(mfs.hasMoreElements()) {
Collector.MetricFamilySamples metricFamilySamples = mfs.nextElement();
String name = metricFamilySamples.name;
if (metricFamilySamples.type == Collector.Type.COUNTER) {
name += "_total";
}
writer.write("# HELP ");
writer.write(name);
if (metricFamilySamples.type == Collector.Type.COUNTER) {
writer.write("_total");
}
writer.write(' ');
writeEscapedHelp(writer, metricFamilySamples.help);
writer.write('\n');

writer.write("# TYPE ");
writer.write(name);
if (metricFamilySamples.type == Collector.Type.COUNTER) {
writer.write("_total");
}
writer.write(' ');
writer.write(typeString(metricFamilySamples.type));
writer.write('\n');

String createdName = name + "_created";
String gcountName = name + "_gcount";
String gsumName = name + "_gsum";
for (Collector.MetricFamilySamples.Sample sample: metricFamilySamples.samples) {
writer.write(sample.name);
if (metricFamilySamples.type == Collector.Type.COUNTER && sample.name.equals(metricFamilySamples.name)) {
writer.write("_total");
/* OpenMetrics specific sample, put in a gauge at the end. */
if (sample.name.equals(createdName)
|| sample.name.equals(gcountName)
|| sample.name.equals(gsumName)) {
Collector.MetricFamilySamples omFamily = omFamilies.get(sample.name);
if (omFamily == null) {
omFamily = new Collector.MetricFamilySamples(sample.name, Collector.Type.GAUGE, metricFamilySamples.help, new ArrayList<Collector.MetricFamilySamples.Sample>());
omFamilies.put(sample.name, omFamily);
}
omFamily.samples.add(sample);
continue;
}
writer.write(sample.name);
if (sample.labelNames.size() > 0) {
writer.write('{');
for (int i = 0; i < sample.labelNames.size(); ++i) {
Expand All @@ -97,6 +118,10 @@ public static void write004(Writer writer, Enumeration<Collector.MetricFamilySam
writer.write('\n');
}
}
// Write out any OM-specific samples.
if (!omFamilies.isEmpty()) {
write004(writer, Collections.enumeration(omFamilies.values()));
}
}

private static void writeEscapedHelp(Writer writer, String s) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.junit.Before;
Expand Down Expand Up @@ -57,6 +58,17 @@ public void testCounterOutput() throws IOException {
+ "nolabels_total 1.0\n", writer.toString());
}

@Test
public void testCounterWithTotalOutput() throws IOException {
Counter noLabels = Counter.build().name("nolabels_total").help("help").register(registry);
noLabels.inc();
TextFormat.write004(writer, registry.metricFamilySamples());
assertEquals("# HELP nolabels_total help\n"
+ "# TYPE nolabels_total counter\n"
+ "nolabels_total 1.0\n", writer.toString());
}


@Test
public void testCounterSamplesMissingTotal() throws IOException {

Expand Down Expand Up @@ -131,6 +143,39 @@ public void testSummaryOutputWithQuantiles() throws IOException {
+ "labelsAndQuantiles_sum{l=\"a\",} 2.0\n", writer.toString());
}

@Test
public void testGaugeHistogramOutput() 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>();
samples.add(new MetricFamilySamples.Sample("nolabels_bucket", Arrays.asList("le"), Arrays.asList("+Inf"), 2.0));
samples.add(new MetricFamilySamples.Sample("nolabels_gcount", labelNames, labelValues, 2.0));
samples.add(new MetricFamilySamples.Sample("nolabels_gsum", labelNames, labelValues, 7.0));
samples.add(new MetricFamilySamples.Sample("nolabels_created", labelNames, labelValues, 1234.0));
mfs.add(new MetricFamilySamples("nolabels", Collector.Type.GAUGE_HISTOGRAM, "help", samples));
return mfs;
}
}
new CustomCollector().register(registry);
writer = new StringWriter();
TextFormat.write004(writer, registry.metricFamilySamples());
assertEquals("# HELP nolabels help\n"
+ "# TYPE nolabels histogram\n"
+ "nolabels_bucket{le=\"+Inf\",} 2.0\n"
+ "# HELP nolabels_created help\n"
+ "# TYPE nolabels_created gauge\n"
+ "nolabels_created 1234.0\n"
+ "# HELP nolabels_gcount help\n"
+ "# TYPE nolabels_gcount gauge\n"
+ "nolabels_gcount 2.0\n"
+ "# HELP nolabels_gsum help\n"
+ "# TYPE nolabels_gsum gauge\n"
+ "nolabels_gsum 7.0\n", writer.toString());
}

@Test
public void testLabelsOutput() throws IOException {
Gauge labels = Gauge.build().name("labels").help("help").labelNames("l").register(registry);
Expand Down

0 comments on commit 801f91e

Please sign in to comment.