diff --git a/simpleclient_common/src/main/java/io/prometheus/client/exporter/common/TextFormat.java b/simpleclient_common/src/main/java/io/prometheus/client/exporter/common/TextFormat.java index db2de3a5b..4d09ec4e1 100644 --- a/simpleclient_common/src/main/java/io/prometheus/client/exporter/common/TextFormat.java +++ b/simpleclient_common/src/main/java/io/prometheus/client/exporter/common/TextFormat.java @@ -1,11 +1,11 @@ package io.prometheus.client.exporter.common; import java.io.IOException; -import java.io.StringWriter; 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; @@ -22,7 +22,8 @@ public class TextFormat { * * @since 0.10.0 */ - public final static String CONTENT_TYPE_OPENMETRICS_100 = "application/openmetrics-text; version=1.0.0; charset=utf-8"; + public final static String CONTENT_TYPE_OPENMETRICS_100 = + "application/openmetrics-text; version=1.0.0; charset=utf-8"; /** * Return the content type that should be used for a given Accept HTTP header. @@ -48,14 +49,15 @@ public static String chooseContentType(String acceptHeader) { * * @since 0.10.0 */ - public static void writeFormat(String contentType, Writer writer, Enumeration mfs) throws IOException { + public static void writeFormat( + String contentType, Writer writer, Enumeration mfs) throws IOException { if (CONTENT_TYPE_004.equals(contentType)) { - write004(writer, mfs); - return; + write004(writer, mfs); + return; } if (CONTENT_TYPE_OPENMETRICS_100.equals(contentType)) { - writeOpenMetrics100(writer, mfs); - return; + writeOpenMetrics100(writer, mfs); + return; } throw new IllegalArgumentException("Unknown contentType " + contentType); } @@ -67,7 +69,7 @@ public static void write004(Writer writer, Enumeration omFamilies = new TreeMap(); /* See http://prometheus.io/docs/instrumenting/exposition_formats/ * for the output format specification. */ - while(mfs.hasMoreElements()) { + while (mfs.hasMoreElements()) { Collector.MetricFamilySamples metricFamilySamples = mfs.nextElement(); String name = metricFamilySamples.name; writer.write("# HELP "); @@ -97,33 +99,23 @@ public static void write004(Writer writer, Enumeration()); + omFamily = new Collector.MetricFamilySamples(sample.name, + Collector.Type.GAUGE, metricFamilySamples.help, + new ArrayList()); 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) { - writer.write(sample.labelNames.get(i)); - writer.write("=\""); - writeEscapedLabelValue(writer, sample.labelValues.get(i)); - writer.write("\","); - } - writer.write('}'); - } - writer.write(' '); - writer.write(Collector.doubleToGoString(sample.value)); - if (sample.timestampMs != null){ + appendSamples(writer, sample); + if (sample.timestampMs != null) { writer.write(' '); writer.write(sample.timestampMs.toString()); } @@ -136,6 +128,30 @@ public static void write004(Writer writer, Enumeration labelNames = sample.labelNames; + List labelValues = sample.labelValues; + int sampleSize = labelNames.size(); + if (sampleSize > 0) { + writer.write('{'); + writer.write(labelNames.get(0)); + writer.write("=\""); + writeEscapedLabelValue(writer, labelValues.get(0)); + writer.write("\""); + for (int i = 1; i < sampleSize; ++i) { + writer.write(","); + writer.write(labelNames.get(i)); + writer.write("=\""); + writeEscapedLabelValue(writer, labelValues.get(i)); + writer.write("\""); + } + writer.write('}'); + } + writer.write(' '); + writer.write(Collector.doubleToGoString(sample.value)); + } + private static void writeEscapedHelp(Writer writer, String s) throws IOException { for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); @@ -197,8 +213,9 @@ private static String typeString(Collector.Type t) { * * @since 0.10.0 */ - public static void writeOpenMetrics100(Writer writer, Enumeration mfs) throws IOException { - while(mfs.hasMoreElements()) { + public static void writeOpenMetrics100( + Writer writer, Enumeration mfs) throws IOException { + while (mfs.hasMoreElements()) { Collector.MetricFamilySamples metricFamilySamples = mfs.nextElement(); String name = metricFamilySamples.name; @@ -221,31 +238,16 @@ public static void writeOpenMetrics100(Writer writer, Enumeration 0) { - writer.write('{'); - for (int i = 0; i < sample.labelNames.size(); ++i) { - if (i > 0) { - writer.write(","); - } - writer.write(sample.labelNames.get(i)); - writer.write("=\""); - writeEscapedLabelValue(writer, sample.labelValues.get(i)); - writer.write("\""); - } - writer.write('}'); - } - writer.write(' '); - writer.write(Collector.doubleToGoString(sample.value)); - if (sample.timestampMs != null){ + + for (Collector.MetricFamilySamples.Sample sample : metricFamilySamples.samples) { + appendSamples(writer, sample); + if (sample.timestampMs != null) { writer.write(' '); omWriteTimestamp(writer, sample.timestampMs); } if (sample.exemplar != null) { writer.write(" # {"); - for (int i=0; i 0) { writer.write(","); }