Skip to content

Commit 801f91e

Browse files
committed
Add handling for gsum/gcount/created in Prometheus exposition format.
Signed-off-by: Brian Brazil <[email protected]>
1 parent 706672a commit 801f91e

File tree

2 files changed

+76
-6
lines changed

2 files changed

+76
-6
lines changed

simpleclient_common/src/main/java/io/prometheus/client/exporter/common/TextFormat.java

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22

33
import java.io.IOException;
44
import java.io.Writer;
5+
import java.util.ArrayList;
6+
import java.util.Collections;
57
import java.util.Enumeration;
8+
import java.util.List;
9+
import java.util.Map;
10+
import java.util.TreeMap;
611

712
import io.prometheus.client.Collector;
813

@@ -53,31 +58,47 @@ public static void writeFormat(String contentType, Writer writer, Enumeration<Co
5358
* Write out the text version 0.0.4 of the given MetricFamilySamples.
5459
*/
5560
public static void write004(Writer writer, Enumeration<Collector.MetricFamilySamples> mfs) throws IOException {
61+
Map<String, Collector.MetricFamilySamples> omFamilies = new TreeMap<String, Collector.MetricFamilySamples>();
5662
/* See http://prometheus.io/docs/instrumenting/exposition_formats/
5763
* for the output format specification. */
5864
while(mfs.hasMoreElements()) {
5965
Collector.MetricFamilySamples metricFamilySamples = mfs.nextElement();
6066
String name = metricFamilySamples.name;
61-
if (metricFamilySamples.type == Collector.Type.COUNTER) {
62-
name += "_total";
63-
}
6467
writer.write("# HELP ");
6568
writer.write(name);
69+
if (metricFamilySamples.type == Collector.Type.COUNTER) {
70+
writer.write("_total");
71+
}
6672
writer.write(' ');
6773
writeEscapedHelp(writer, metricFamilySamples.help);
6874
writer.write('\n');
6975

7076
writer.write("# TYPE ");
7177
writer.write(name);
78+
if (metricFamilySamples.type == Collector.Type.COUNTER) {
79+
writer.write("_total");
80+
}
7281
writer.write(' ');
7382
writer.write(typeString(metricFamilySamples.type));
7483
writer.write('\n');
7584

85+
String createdName = name + "_created";
86+
String gcountName = name + "_gcount";
87+
String gsumName = name + "_gsum";
7688
for (Collector.MetricFamilySamples.Sample sample: metricFamilySamples.samples) {
77-
writer.write(sample.name);
78-
if (metricFamilySamples.type == Collector.Type.COUNTER && sample.name.equals(metricFamilySamples.name)) {
79-
writer.write("_total");
89+
/* OpenMetrics specific sample, put in a gauge at the end. */
90+
if (sample.name.equals(createdName)
91+
|| sample.name.equals(gcountName)
92+
|| sample.name.equals(gsumName)) {
93+
Collector.MetricFamilySamples omFamily = omFamilies.get(sample.name);
94+
if (omFamily == null) {
95+
omFamily = new Collector.MetricFamilySamples(sample.name, Collector.Type.GAUGE, metricFamilySamples.help, new ArrayList<Collector.MetricFamilySamples.Sample>());
96+
omFamilies.put(sample.name, omFamily);
97+
}
98+
omFamily.samples.add(sample);
99+
continue;
80100
}
101+
writer.write(sample.name);
81102
if (sample.labelNames.size() > 0) {
82103
writer.write('{');
83104
for (int i = 0; i < sample.labelNames.size(); ++i) {
@@ -97,6 +118,10 @@ public static void write004(Writer writer, Enumeration<Collector.MetricFamilySam
97118
writer.write('\n');
98119
}
99120
}
121+
// Write out any OM-specific samples.
122+
if (!omFamilies.isEmpty()) {
123+
write004(writer, Collections.enumeration(omFamilies.values()));
124+
}
100125
}
101126

102127
private static void writeEscapedHelp(Writer writer, String s) throws IOException {

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.io.IOException;
66
import java.io.StringWriter;
77
import java.util.ArrayList;
8+
import java.util.Arrays;
89
import java.util.List;
910

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

61+
@Test
62+
public void testCounterWithTotalOutput() throws IOException {
63+
Counter noLabels = Counter.build().name("nolabels_total").help("help").register(registry);
64+
noLabels.inc();
65+
TextFormat.write004(writer, registry.metricFamilySamples());
66+
assertEquals("# HELP nolabels_total help\n"
67+
+ "# TYPE nolabels_total counter\n"
68+
+ "nolabels_total 1.0\n", writer.toString());
69+
}
70+
71+
6072
@Test
6173
public void testCounterSamplesMissingTotal() throws IOException {
6274

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

146+
@Test
147+
public void testGaugeHistogramOutput() throws IOException {
148+
class CustomCollector extends Collector {
149+
public List<MetricFamilySamples> collect() {
150+
List<MetricFamilySamples> mfs = new ArrayList<MetricFamilySamples>();
151+
ArrayList<String> labelNames = new ArrayList<String>();
152+
ArrayList<String> labelValues = new ArrayList<String>();
153+
ArrayList<MetricFamilySamples.Sample> samples = new ArrayList<Collector.MetricFamilySamples.Sample>();
154+
samples.add(new MetricFamilySamples.Sample("nolabels_bucket", Arrays.asList("le"), Arrays.asList("+Inf"), 2.0));
155+
samples.add(new MetricFamilySamples.Sample("nolabels_gcount", labelNames, labelValues, 2.0));
156+
samples.add(new MetricFamilySamples.Sample("nolabels_gsum", labelNames, labelValues, 7.0));
157+
samples.add(new MetricFamilySamples.Sample("nolabels_created", labelNames, labelValues, 1234.0));
158+
mfs.add(new MetricFamilySamples("nolabels", Collector.Type.GAUGE_HISTOGRAM, "help", samples));
159+
return mfs;
160+
}
161+
}
162+
new CustomCollector().register(registry);
163+
writer = new StringWriter();
164+
TextFormat.write004(writer, registry.metricFamilySamples());
165+
assertEquals("# HELP nolabels help\n"
166+
+ "# TYPE nolabels histogram\n"
167+
+ "nolabels_bucket{le=\"+Inf\",} 2.0\n"
168+
+ "# HELP nolabels_created help\n"
169+
+ "# TYPE nolabels_created gauge\n"
170+
+ "nolabels_created 1234.0\n"
171+
+ "# HELP nolabels_gcount help\n"
172+
+ "# TYPE nolabels_gcount gauge\n"
173+
+ "nolabels_gcount 2.0\n"
174+
+ "# HELP nolabels_gsum help\n"
175+
+ "# TYPE nolabels_gsum gauge\n"
176+
+ "nolabels_gsum 7.0\n", writer.toString());
177+
}
178+
134179
@Test
135180
public void testLabelsOutput() throws IOException {
136181
Gauge labels = Gauge.build().name("labels").help("help").labelNames("l").register(registry);

0 commit comments

Comments
 (0)