@@ -89,6 +89,7 @@ public OpenMetrics2TextFormatWriter build() {
8989 public static final String CONTENT_TYPE =
9090 "application/openmetrics-text; version=2.0.0; charset=utf-8" ;
9191 private final OpenMetrics2Properties openMetrics2Properties ;
92+ private final boolean createdTimestampsEnabled ;
9293 private final boolean exemplarsOnAllMetricTypesEnabled ;
9394 private final OpenMetricsTextFormatWriter om1Writer ;
9495
@@ -102,6 +103,7 @@ public OpenMetrics2TextFormatWriter(
102103 boolean createdTimestampsEnabled ,
103104 boolean exemplarsOnAllMetricTypesEnabled ) {
104105 this .openMetrics2Properties = openMetrics2Properties ;
106+ this .createdTimestampsEnabled = createdTimestampsEnabled ;
105107 this .exemplarsOnAllMetricTypesEnabled = exemplarsOnAllMetricTypesEnabled ;
106108 this .om1Writer =
107109 new OpenMetricsTextFormatWriter (createdTimestampsEnabled , exemplarsOnAllMetricTypesEnabled );
@@ -209,12 +211,12 @@ private void writeHistogram(Writer writer, HistogramSnapshot snapshot, EscapingS
209211 throws IOException {
210212 boolean compositeHistogram =
211213 openMetrics2Properties .getCompositeValues () || openMetrics2Properties .getNativeHistograms ();
214+ MetricMetadata metadata = snapshot .getMetadata ();
215+ String name = getOriginalMetadataName (metadata , scheme );
212216 if (!compositeHistogram && !openMetrics2Properties .getExemplarCompliance ()) {
213- om1Writer . writeHistogram (writer , snapshot , scheme );
217+ writeClassicHistogram (writer , name , snapshot , scheme );
214218 return ;
215219 }
216- MetricMetadata metadata = snapshot .getMetadata ();
217- String name = getOriginalMetadataName (metadata , scheme );
218220 if (snapshot .isGaugeHistogram ()) {
219221 writeMetadataWithName (writer , name , "gaugehistogram" , metadata );
220222 for (HistogramSnapshot .HistogramDataPointSnapshot data : snapshot .getDataPoints ()) {
@@ -236,6 +238,88 @@ private void writeHistogram(Writer writer, HistogramSnapshot snapshot, EscapingS
236238 }
237239 }
238240
241+ private void writeClassicHistogram (
242+ Writer writer , String name , HistogramSnapshot snapshot , EscapingScheme scheme )
243+ throws IOException {
244+ if (snapshot .isGaugeHistogram ()) {
245+ writeMetadataWithName (writer , name , "gaugehistogram" , snapshot .getMetadata ());
246+ writeClassicHistogramDataPoints (writer , name , "_gcount" , "_gsum" , snapshot , scheme );
247+ } else {
248+ writeMetadataWithName (writer , name , "histogram" , snapshot .getMetadata ());
249+ writeClassicHistogramDataPoints (writer , name , "_count" , "_sum" , snapshot , scheme );
250+ }
251+ }
252+
253+ private void writeClassicHistogramDataPoints (
254+ Writer writer ,
255+ String name ,
256+ String countSuffix ,
257+ String sumSuffix ,
258+ HistogramSnapshot snapshot ,
259+ EscapingScheme scheme )
260+ throws IOException {
261+ for (HistogramSnapshot .HistogramDataPointSnapshot data : snapshot .getDataPoints ()) {
262+ ClassicHistogramBuckets buckets = getClassicBuckets (data );
263+ Exemplars exemplars = data .getExemplars ();
264+ long cumulativeCount = 0 ;
265+ for (int i = 0 ; i < buckets .size (); i ++) {
266+ cumulativeCount += buckets .getCount (i );
267+ writeNameAndLabels (
268+ writer , name , "_bucket" , data .getLabels (), scheme , "le" , buckets .getUpperBound (i ));
269+ writeLong (writer , cumulativeCount );
270+ Exemplar exemplar ;
271+ if (i == 0 ) {
272+ exemplar = exemplars .get (Double .NEGATIVE_INFINITY , buckets .getUpperBound (i ));
273+ } else {
274+ exemplar = exemplars .get (buckets .getUpperBound (i - 1 ), buckets .getUpperBound (i ));
275+ }
276+ writeScrapeTimestampAndExemplar (writer , data , exemplar , scheme );
277+ }
278+ if (data .hasCount () && data .hasSum ()) {
279+ writeClassicCountAndSum (writer , name , data , countSuffix , sumSuffix , exemplars , scheme );
280+ }
281+ writeClassicCreated (writer , name , data , scheme );
282+ }
283+ }
284+
285+ private void writeClassicCountAndSum (
286+ Writer writer ,
287+ String name ,
288+ HistogramSnapshot .HistogramDataPointSnapshot data ,
289+ String countSuffix ,
290+ String sumSuffix ,
291+ Exemplars exemplars ,
292+ EscapingScheme scheme )
293+ throws IOException {
294+ writeNameAndLabels (writer , name , countSuffix , data .getLabels (), scheme );
295+ writeLong (writer , data .getCount ());
296+ if (exemplarsOnAllMetricTypesEnabled ) {
297+ writeScrapeTimestampAndExemplar (writer , data , exemplars .getLatest (), scheme );
298+ } else {
299+ writeScrapeTimestampAndExemplar (writer , data , null , scheme );
300+ }
301+ writeNameAndLabels (writer , name , sumSuffix , data .getLabels (), scheme );
302+ writeDouble (writer , data .getSum ());
303+ writeScrapeTimestampAndExemplar (writer , data , null , scheme );
304+ }
305+
306+ private void writeClassicCreated (
307+ Writer writer ,
308+ String name ,
309+ HistogramSnapshot .HistogramDataPointSnapshot data ,
310+ EscapingScheme scheme )
311+ throws IOException {
312+ if (createdTimestampsEnabled && data .hasCreatedTimestamp ()) {
313+ writeNameAndLabels (writer , name , "_created" , data .getLabels (), scheme );
314+ writeOpenMetricsTimestamp (writer , data .getCreatedTimestampMillis ());
315+ if (data .hasScrapeTimestamp ()) {
316+ writer .write (' ' );
317+ writeOpenMetricsTimestamp (writer , data .getScrapeTimestampMillis ());
318+ }
319+ writer .write ('\n' );
320+ }
321+ }
322+
239323 private void writeCompositeHistogramDataPoint (
240324 Writer writer ,
241325 String name ,
0 commit comments