1
1
package io .prometheus .client .exporter .common ;
2
2
3
3
import java .io .IOException ;
4
- import java .io .StringWriter ;
5
4
import java .io .Writer ;
6
5
import java .util .ArrayList ;
7
6
import java .util .Collections ;
8
7
import java .util .Enumeration ;
8
+ import java .util .List ;
9
9
import java .util .Map ;
10
10
import java .util .TreeMap ;
11
11
@@ -22,7 +22,8 @@ public class TextFormat {
22
22
*
23
23
* @since 0.10.0
24
24
*/
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" ;
26
27
27
28
/**
28
29
* Return the content type that should be used for a given Accept HTTP header.
@@ -48,14 +49,15 @@ public static String chooseContentType(String acceptHeader) {
48
49
*
49
50
* @since 0.10.0
50
51
*/
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 {
52
54
if (CONTENT_TYPE_004 .equals (contentType )) {
53
- write004 (writer , mfs );
54
- return ;
55
+ write004 (writer , mfs );
56
+ return ;
55
57
}
56
58
if (CONTENT_TYPE_OPENMETRICS_100 .equals (contentType )) {
57
- writeOpenMetrics100 (writer , mfs );
58
- return ;
59
+ writeOpenMetrics100 (writer , mfs );
60
+ return ;
59
61
}
60
62
throw new IllegalArgumentException ("Unknown contentType " + contentType );
61
63
}
@@ -67,7 +69,7 @@ public static void write004(Writer writer, Enumeration<Collector.MetricFamilySam
67
69
Map <String , Collector .MetricFamilySamples > omFamilies = new TreeMap <String , Collector .MetricFamilySamples >();
68
70
/* See http://prometheus.io/docs/instrumenting/exposition_formats/
69
71
* for the output format specification. */
70
- while (mfs .hasMoreElements ()) {
72
+ while (mfs .hasMoreElements ()) {
71
73
Collector .MetricFamilySamples metricFamilySamples = mfs .nextElement ();
72
74
String name = metricFamilySamples .name ;
73
75
writer .write ("# HELP " );
@@ -97,33 +99,23 @@ public static void write004(Writer writer, Enumeration<Collector.MetricFamilySam
97
99
String createdName = name + "_created" ;
98
100
String gcountName = name + "_gcount" ;
99
101
String gsumName = name + "_gsum" ;
100
- for (Collector .MetricFamilySamples .Sample sample : metricFamilySamples .samples ) {
102
+ for (Collector .MetricFamilySamples .Sample sample : metricFamilySamples .samples ) {
101
103
/* OpenMetrics specific sample, put in a gauge at the end. */
102
104
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 )) {
105
107
Collector .MetricFamilySamples omFamily = omFamilies .get (sample .name );
106
108
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 >());
108
112
omFamilies .put (sample .name , omFamily );
109
113
}
110
114
omFamily .samples .add (sample );
111
115
continue ;
112
116
}
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 ) {
127
119
writer .write (' ' );
128
120
writer .write (sample .timestampMs .toString ());
129
121
}
@@ -136,6 +128,30 @@ public static void write004(Writer writer, Enumeration<Collector.MetricFamilySam
136
128
}
137
129
}
138
130
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
+
139
155
private static void writeEscapedHelp (Writer writer , String s ) throws IOException {
140
156
for (int i = 0 ; i < s .length (); i ++) {
141
157
char c = s .charAt (i );
@@ -197,8 +213,9 @@ private static String typeString(Collector.Type t) {
197
213
*
198
214
* @since 0.10.0
199
215
*/
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 ()) {
202
219
Collector .MetricFamilySamples metricFamilySamples = mfs .nextElement ();
203
220
String name = metricFamilySamples .name ;
204
221
@@ -221,31 +238,16 @@ public static void writeOpenMetrics100(Writer writer, Enumeration<Collector.Metr
221
238
writer .write (' ' );
222
239
writeEscapedLabelValue (writer , metricFamilySamples .help );
223
240
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 ) {
243
245
writer .write (' ' );
244
246
omWriteTimestamp (writer , sample .timestampMs );
245
247
}
246
248
if (sample .exemplar != null ) {
247
249
writer .write (" # {" );
248
- for (int i = 0 ; i < sample .exemplar .getNumberOfLabels (); i ++) {
250
+ for (int i = 0 ; i < sample .exemplar .getNumberOfLabels (); i ++) {
249
251
if (i > 0 ) {
250
252
writer .write ("," );
251
253
}
0 commit comments