11package io .prometheus .client .exporter .common ;
22
33import java .io .IOException ;
4- import java .io .StringWriter ;
54import java .io .Writer ;
65import java .util .ArrayList ;
76import java .util .Collections ;
87import java .util .Enumeration ;
8+ import java .util .List ;
99import java .util .Map ;
1010import java .util .TreeMap ;
1111
@@ -22,7 +22,8 @@ public class TextFormat {
2222 *
2323 * @since 0.10.0
2424 */
25- public final static String CONTENT_TYPE_OPENMETRICS_100 = "application/openmetrics-text; version=1.0.0; charset=utf-8" ;
25+ public final static String CONTENT_TYPE_OPENMETRICS_100 =
26+ "application/openmetrics-text; version=1.0.0; charset=utf-8" ;
2627
2728 /**
2829 * Return the content type that should be used for a given Accept HTTP header.
@@ -48,14 +49,15 @@ public static String chooseContentType(String acceptHeader) {
4849 *
4950 * @since 0.10.0
5051 */
51- public static void writeFormat (String contentType , Writer writer , Enumeration <Collector .MetricFamilySamples > mfs ) throws IOException {
52+ public static void writeFormat (
53+ String contentType , Writer writer , Enumeration <Collector .MetricFamilySamples > mfs ) throws IOException {
5254 if (CONTENT_TYPE_004 .equals (contentType )) {
53- write004 (writer , mfs );
54- return ;
55+ write004 (writer , mfs );
56+ return ;
5557 }
5658 if (CONTENT_TYPE_OPENMETRICS_100 .equals (contentType )) {
57- writeOpenMetrics100 (writer , mfs );
58- return ;
59+ writeOpenMetrics100 (writer , mfs );
60+ return ;
5961 }
6062 throw new IllegalArgumentException ("Unknown contentType " + contentType );
6163 }
@@ -67,7 +69,7 @@ public static void write004(Writer writer, Enumeration<Collector.MetricFamilySam
6769 Map <String , Collector .MetricFamilySamples > omFamilies = new TreeMap <String , Collector .MetricFamilySamples >();
6870 /* See http://prometheus.io/docs/instrumenting/exposition_formats/
6971 * for the output format specification. */
70- while (mfs .hasMoreElements ()) {
72+ while (mfs .hasMoreElements ()) {
7173 Collector .MetricFamilySamples metricFamilySamples = mfs .nextElement ();
7274 String name = metricFamilySamples .name ;
7375 writer .write ("# HELP " );
@@ -97,33 +99,23 @@ public static void write004(Writer writer, Enumeration<Collector.MetricFamilySam
9799 String createdName = name + "_created" ;
98100 String gcountName = name + "_gcount" ;
99101 String gsumName = name + "_gsum" ;
100- for (Collector .MetricFamilySamples .Sample sample : metricFamilySamples .samples ) {
102+ for (Collector .MetricFamilySamples .Sample sample : metricFamilySamples .samples ) {
101103 /* OpenMetrics specific sample, put in a gauge at the end. */
102104 if (sample .name .equals (createdName )
103- || sample .name .equals (gcountName )
104- || sample .name .equals (gsumName )) {
105+ || sample .name .equals (gcountName )
106+ || sample .name .equals (gsumName )) {
105107 Collector .MetricFamilySamples omFamily = omFamilies .get (sample .name );
106108 if (omFamily == null ) {
107- omFamily = new Collector .MetricFamilySamples (sample .name , Collector .Type .GAUGE , metricFamilySamples .help , new ArrayList <Collector .MetricFamilySamples .Sample >());
109+ omFamily = new Collector .MetricFamilySamples (sample .name ,
110+ Collector .Type .GAUGE , metricFamilySamples .help ,
111+ new ArrayList <Collector .MetricFamilySamples .Sample >());
108112 omFamilies .put (sample .name , omFamily );
109113 }
110114 omFamily .samples .add (sample );
111115 continue ;
112116 }
113- writer .write (sample .name );
114- if (sample .labelNames .size () > 0 ) {
115- writer .write ('{' );
116- for (int i = 0 ; i < sample .labelNames .size (); ++i ) {
117- writer .write (sample .labelNames .get (i ));
118- writer .write ("=\" " );
119- writeEscapedLabelValue (writer , sample .labelValues .get (i ));
120- writer .write ("\" ," );
121- }
122- writer .write ('}' );
123- }
124- writer .write (' ' );
125- writer .write (Collector .doubleToGoString (sample .value ));
126- if (sample .timestampMs != null ){
117+ appendSamples (writer , sample );
118+ if (sample .timestampMs != null ) {
127119 writer .write (' ' );
128120 writer .write (sample .timestampMs .toString ());
129121 }
@@ -136,6 +128,30 @@ public static void write004(Writer writer, Enumeration<Collector.MetricFamilySam
136128 }
137129 }
138130
131+ private static void appendSamples (Writer writer , Collector .MetricFamilySamples .Sample sample ) throws IOException {
132+ writer .write (sample .name );
133+ List <String > labelNames = sample .labelNames ;
134+ List <String > labelValues = sample .labelValues ;
135+ int sampleSize = labelNames .size ();
136+ if (sampleSize > 0 ) {
137+ writer .write ('{' );
138+ writer .write (labelNames .get (0 ));
139+ writer .write ("=\" " );
140+ writeEscapedLabelValue (writer , labelValues .get (0 ));
141+ writer .write ("\" " );
142+ for (int i = 1 ; i < sampleSize ; ++i ) {
143+ writer .write ("," );
144+ writer .write (labelNames .get (i ));
145+ writer .write ("=\" " );
146+ writeEscapedLabelValue (writer , labelValues .get (i ));
147+ writer .write ("\" " );
148+ }
149+ writer .write ('}' );
150+ }
151+ writer .write (' ' );
152+ writer .write (Collector .doubleToGoString (sample .value ));
153+ }
154+
139155 private static void writeEscapedHelp (Writer writer , String s ) throws IOException {
140156 for (int i = 0 ; i < s .length (); i ++) {
141157 char c = s .charAt (i );
@@ -197,8 +213,9 @@ private static String typeString(Collector.Type t) {
197213 *
198214 * @since 0.10.0
199215 */
200- public static void writeOpenMetrics100 (Writer writer , Enumeration <Collector .MetricFamilySamples > mfs ) throws IOException {
201- while (mfs .hasMoreElements ()) {
216+ public static void writeOpenMetrics100 (
217+ Writer writer , Enumeration <Collector .MetricFamilySamples > mfs ) throws IOException {
218+ while (mfs .hasMoreElements ()) {
202219 Collector .MetricFamilySamples metricFamilySamples = mfs .nextElement ();
203220 String name = metricFamilySamples .name ;
204221
@@ -221,31 +238,16 @@ public static void writeOpenMetrics100(Writer writer, Enumeration<Collector.Metr
221238 writer .write (' ' );
222239 writeEscapedLabelValue (writer , metricFamilySamples .help );
223240 writer .write ('\n' );
224-
225- for (Collector .MetricFamilySamples .Sample sample : metricFamilySamples .samples ) {
226- writer .write (sample .name );
227- if (sample .labelNames .size () > 0 ) {
228- writer .write ('{' );
229- for (int i = 0 ; i < sample .labelNames .size (); ++i ) {
230- if (i > 0 ) {
231- writer .write ("," );
232- }
233- writer .write (sample .labelNames .get (i ));
234- writer .write ("=\" " );
235- writeEscapedLabelValue (writer , sample .labelValues .get (i ));
236- writer .write ("\" " );
237- }
238- writer .write ('}' );
239- }
240- writer .write (' ' );
241- writer .write (Collector .doubleToGoString (sample .value ));
242- if (sample .timestampMs != null ){
241+
242+ for (Collector .MetricFamilySamples .Sample sample : metricFamilySamples .samples ) {
243+ appendSamples (writer , sample );
244+ if (sample .timestampMs != null ) {
243245 writer .write (' ' );
244246 omWriteTimestamp (writer , sample .timestampMs );
245247 }
246248 if (sample .exemplar != null ) {
247249 writer .write (" # {" );
248- for (int i = 0 ; i < sample .exemplar .getNumberOfLabels (); i ++) {
250+ for (int i = 0 ; i < sample .exemplar .getNumberOfLabels (); i ++) {
249251 if (i > 0 ) {
250252 writer .write ("," );
251253 }
0 commit comments