4
4
import static io .prometheus .metrics .expositionformats .TextFormatUtil .writeEscapedLabelValue ;
5
5
import static io .prometheus .metrics .expositionformats .TextFormatUtil .writeLabels ;
6
6
import static io .prometheus .metrics .expositionformats .TextFormatUtil .writeLong ;
7
- import static io .prometheus .metrics .expositionformats .TextFormatUtil .writeTimestamp ;
7
+ import static io .prometheus .metrics .expositionformats .TextFormatUtil .writeOpenMetricsTimestamp ;
8
8
9
9
import io .prometheus .metrics .model .snapshots .ClassicHistogramBuckets ;
10
10
import io .prometheus .metrics .model .snapshots .CounterSnapshot ;
37
37
*/
38
38
public class OpenMetricsTextFormatWriter implements ExpositionFormatWriter {
39
39
40
+ public static class Builder {
41
+ boolean createdTimestampsEnabled ;
42
+ boolean exemplarsOnAllMetricTypesEnabled ;
43
+
44
+ private Builder () {}
45
+
46
+ /**
47
+ * @param createdTimestampsEnabled whether to include the _created timestamp in the output
48
+ */
49
+ public Builder setCreatedTimestampsEnabled (boolean createdTimestampsEnabled ) {
50
+ this .createdTimestampsEnabled = createdTimestampsEnabled ;
51
+ return this ;
52
+ }
53
+
54
+ /**
55
+ * @param exemplarsOnAllMetricTypesEnabled whether to include exemplars in the output for all
56
+ * metric types
57
+ */
58
+ public Builder setExemplarsOnAllMetricTypesEnabled (boolean exemplarsOnAllMetricTypesEnabled ) {
59
+ this .exemplarsOnAllMetricTypesEnabled = exemplarsOnAllMetricTypesEnabled ;
60
+ return this ;
61
+ }
62
+
63
+ public OpenMetricsTextFormatWriter build () {
64
+ return new OpenMetricsTextFormatWriter (
65
+ createdTimestampsEnabled , exemplarsOnAllMetricTypesEnabled );
66
+ }
67
+ }
68
+
40
69
public static final String CONTENT_TYPE =
41
70
"application/openmetrics-text; version=1.0.0; charset=utf-8" ;
42
71
private final boolean createdTimestampsEnabled ;
43
72
private final boolean exemplarsOnAllMetricTypesEnabled ;
44
73
45
74
/**
46
- * @param createdTimestampsEnabled defines if {@code _created} timestamps should be included in
47
- * the output or not .
75
+ * @param createdTimestampsEnabled whether to include the _created timestamp in the output - This
76
+ * will produce an invalid OpenMetrics output, but is kept for backwards compatibility .
48
77
*/
49
78
public OpenMetricsTextFormatWriter (
50
79
boolean createdTimestampsEnabled , boolean exemplarsOnAllMetricTypesEnabled ) {
51
80
this .createdTimestampsEnabled = createdTimestampsEnabled ;
52
81
this .exemplarsOnAllMetricTypesEnabled = exemplarsOnAllMetricTypesEnabled ;
53
82
}
54
83
84
+ public static Builder builder () {
85
+ return new Builder ();
86
+ }
87
+
88
+ public static OpenMetricsTextFormatWriter create () {
89
+ return builder ().build ();
90
+ }
91
+
55
92
@ Override
56
93
public boolean accepts (String acceptHeader ) {
57
94
if (acceptHeader == null ) {
@@ -299,10 +336,10 @@ private void writeCreated(Writer writer, MetricMetadata metadata, DataPointSnaps
299
336
throws IOException {
300
337
if (createdTimestampsEnabled && data .hasCreatedTimestamp ()) {
301
338
writeNameAndLabels (writer , metadata .getPrometheusName (), "_created" , data .getLabels ());
302
- writeTimestamp (writer , data .getCreatedTimestampMillis ());
339
+ writeOpenMetricsTimestamp (writer , data .getCreatedTimestampMillis ());
303
340
if (data .hasScrapeTimestamp ()) {
304
341
writer .write (' ' );
305
- writeTimestamp (writer , data .getScrapeTimestampMillis ());
342
+ writeOpenMetricsTimestamp (writer , data .getScrapeTimestampMillis ());
306
343
}
307
344
writer .write ('\n' );
308
345
}
@@ -335,7 +372,7 @@ private void writeScrapeTimestampAndExemplar(
335
372
Writer writer , DataPointSnapshot data , Exemplar exemplar ) throws IOException {
336
373
if (data .hasScrapeTimestamp ()) {
337
374
writer .write (' ' );
338
- writeTimestamp (writer , data .getScrapeTimestampMillis ());
375
+ writeOpenMetricsTimestamp (writer , data .getScrapeTimestampMillis ());
339
376
}
340
377
if (exemplar != null ) {
341
378
writer .write (" # " );
@@ -344,7 +381,7 @@ private void writeScrapeTimestampAndExemplar(
344
381
writeDouble (writer , exemplar .getValue ());
345
382
if (exemplar .hasTimestamp ()) {
346
383
writer .write (' ' );
347
- writeTimestamp (writer , exemplar .getTimestampMillis ());
384
+ writeOpenMetricsTimestamp (writer , exemplar .getTimestampMillis ());
348
385
}
349
386
}
350
387
writer .write ('\n' );
0 commit comments