1414import io .prometheus .metrics .model .snapshots .ClassicHistogramBuckets ;
1515import io .prometheus .metrics .model .snapshots .CounterSnapshot ;
1616import io .prometheus .metrics .model .snapshots .DataPointSnapshot ;
17- import io .prometheus .metrics .model .snapshots .DistributionDataPointSnapshot ;
1817import io .prometheus .metrics .model .snapshots .Exemplar ;
19- import io .prometheus .metrics .model .snapshots .Exemplars ;
2018import io .prometheus .metrics .model .snapshots .GaugeSnapshot ;
2119import io .prometheus .metrics .model .snapshots .HistogramSnapshot ;
2220import io .prometheus .metrics .model .snapshots .InfoSnapshot ;
3634import java .io .OutputStreamWriter ;
3735import java .io .Writer ;
3836import java .nio .charset .StandardCharsets ;
39- import java .util .List ;
4037import javax .annotation .Nullable ;
4138
4239/**
@@ -92,6 +89,7 @@ public OpenMetrics2TextFormatWriter build() {
9289 private final OpenMetrics2Properties openMetrics2Properties ;
9390 private final boolean createdTimestampsEnabled ;
9491 private final boolean exemplarsOnAllMetricTypesEnabled ;
92+ private final OpenMetricsTextFormatWriter om1Writer ;
9593
9694 /**
9795 * @param openMetrics2Properties OpenMetrics 2.0 feature flags
@@ -106,6 +104,8 @@ public OpenMetrics2TextFormatWriter(
106104 this .openMetrics2Properties = openMetrics2Properties ;
107105 this .createdTimestampsEnabled = createdTimestampsEnabled ;
108106 this .exemplarsOnAllMetricTypesEnabled = exemplarsOnAllMetricTypesEnabled ;
107+ this .om1Writer =
108+ new OpenMetricsTextFormatWriter (createdTimestampsEnabled , exemplarsOnAllMetricTypesEnabled );
109109 }
110110
111111 public static Builder builder () {
@@ -200,27 +200,21 @@ private void writeGauge(Writer writer, GaugeSnapshot snapshot, EscapingScheme sc
200200
201201 private void writeHistogram (Writer writer , HistogramSnapshot snapshot , EscapingScheme scheme )
202202 throws IOException {
203+ if (!openMetrics2Properties .getCompositeValues ()) {
204+ om1Writer .writeHistogram (writer , snapshot , scheme );
205+ return ;
206+ }
203207 MetricMetadata metadata = snapshot .getMetadata ();
204208 String name = getExpositionBaseMetadataName (metadata , scheme );
205209 if (snapshot .isGaugeHistogram ()) {
206210 writeMetadataWithName (writer , name , "gaugehistogram" , metadata );
207- if (openMetrics2Properties .getCompositeValues ()) {
208- for (HistogramSnapshot .HistogramDataPointSnapshot data : snapshot .getDataPoints ()) {
209- writeCompositeHistogramDataPoint (writer , name , "gcount" , "gsum" , data , scheme );
210- }
211- } else {
212- writeClassicHistogramBuckets (
213- writer , name , "_gcount" , "_gsum" , snapshot .getDataPoints (), scheme );
211+ for (HistogramSnapshot .HistogramDataPointSnapshot data : snapshot .getDataPoints ()) {
212+ writeCompositeHistogramDataPoint (writer , name , "gcount" , "gsum" , data , scheme );
214213 }
215214 } else {
216215 writeMetadataWithName (writer , name , "histogram" , metadata );
217- if (openMetrics2Properties .getCompositeValues ()) {
218- for (HistogramSnapshot .HistogramDataPointSnapshot data : snapshot .getDataPoints ()) {
219- writeCompositeHistogramDataPoint (writer , name , "count" , "sum" , data , scheme );
220- }
221- } else {
222- writeClassicHistogramBuckets (
223- writer , name , "_count" , "_sum" , snapshot .getDataPoints (), scheme );
216+ for (HistogramSnapshot .HistogramDataPointSnapshot data : snapshot .getDataPoints ()) {
217+ writeCompositeHistogramDataPoint (writer , name , "count" , "sum" , data , scheme );
224218 }
225219 }
226220 }
@@ -267,39 +261,6 @@ private void writeCompositeHistogramDataPoint(
267261 writer .write ('\n' );
268262 }
269263
270- private void writeClassicHistogramBuckets (
271- Writer writer ,
272- String name ,
273- String countSuffix ,
274- String sumSuffix ,
275- List <HistogramSnapshot .HistogramDataPointSnapshot > dataList ,
276- EscapingScheme scheme )
277- throws IOException {
278- for (HistogramSnapshot .HistogramDataPointSnapshot data : dataList ) {
279- ClassicHistogramBuckets buckets = getClassicBuckets (data );
280- Exemplars exemplars = data .getExemplars ();
281- long cumulativeCount = 0 ;
282- for (int i = 0 ; i < buckets .size (); i ++) {
283- cumulativeCount += buckets .getCount (i );
284- writeNameAndLabels (
285- writer , name , "_bucket" , data .getLabels (), scheme , "le" , buckets .getUpperBound (i ));
286- writeLong (writer , cumulativeCount );
287- Exemplar exemplar ;
288- if (i == 0 ) {
289- exemplar = exemplars .get (Double .NEGATIVE_INFINITY , buckets .getUpperBound (i ));
290- } else {
291- exemplar = exemplars .get (buckets .getUpperBound (i - 1 ), buckets .getUpperBound (i ));
292- }
293- writeScrapeTimestampAndExemplar (writer , data , exemplar , scheme );
294- }
295- // In OpenMetrics format, histogram _count and _sum are either both present or both absent.
296- if (data .hasCount () && data .hasSum ()) {
297- writeCountAndSum (writer , name , data , countSuffix , sumSuffix , exemplars , scheme );
298- }
299- writeCreated (writer , name , data , scheme );
300- }
301- }
302-
303264 private ClassicHistogramBuckets getClassicBuckets (
304265 HistogramSnapshot .HistogramDataPointSnapshot data ) {
305266 if (data .getClassicBuckets ().isEmpty ()) {
@@ -312,6 +273,10 @@ private ClassicHistogramBuckets getClassicBuckets(
312273
313274 private void writeSummary (Writer writer , SummarySnapshot snapshot , EscapingScheme scheme )
314275 throws IOException {
276+ if (!openMetrics2Properties .getCompositeValues ()) {
277+ om1Writer .writeSummary (writer , snapshot , scheme );
278+ return ;
279+ }
315280 boolean metadataWritten = false ;
316281 MetricMetadata metadata = snapshot .getMetadata ();
317282 String name = getExpositionBaseMetadataName (metadata , scheme );
@@ -323,11 +288,7 @@ private void writeSummary(Writer writer, SummarySnapshot snapshot, EscapingSchem
323288 writeMetadataWithName (writer , name , "summary" , metadata );
324289 metadataWritten = true ;
325290 }
326- if (openMetrics2Properties .getCompositeValues ()) {
327- writeCompositeSummaryDataPoint (writer , name , data , scheme );
328- } else {
329- writeNonCompositeSummaryDataPoint (writer , name , data , scheme );
330- }
291+ writeCompositeSummaryDataPoint (writer , name , data , scheme );
331292 }
332293 }
333294
@@ -382,33 +343,6 @@ private void writeCompositeSummaryDataPoint(
382343 writer .write ('\n' );
383344 }
384345
385- private void writeNonCompositeSummaryDataPoint (
386- Writer writer ,
387- String name ,
388- SummarySnapshot .SummaryDataPointSnapshot data ,
389- EscapingScheme scheme )
390- throws IOException {
391- Exemplars exemplars = data .getExemplars ();
392- // Exemplars for summaries are new, and there's no best practice yet which Exemplars to choose
393- // for which time series. We select exemplars[0] for _count, exemplars[1] for _sum, and
394- // exemplars[2...] for the quantiles, all indexes modulo exemplars.length.
395- int exemplarIndex = 1 ;
396- for (Quantile quantile : data .getQuantiles ()) {
397- writeNameAndLabels (
398- writer , name , null , data .getLabels (), scheme , "quantile" , quantile .getQuantile ());
399- writeDouble (writer , quantile .getValue ());
400- if (exemplars .size () > 0 && exemplarsOnAllMetricTypesEnabled ) {
401- exemplarIndex = (exemplarIndex + 1 ) % exemplars .size ();
402- writeScrapeTimestampAndExemplar (writer , data , exemplars .get (exemplarIndex ), scheme );
403- } else {
404- writeScrapeTimestampAndExemplar (writer , data , null , scheme );
405- }
406- }
407- // Unlike histograms, summaries can have only a count or only a sum according to OpenMetrics.
408- writeCountAndSum (writer , name , data , "_count" , "_sum" , exemplars , scheme );
409- writeCreated (writer , name , data , scheme );
410- }
411-
412346 private void writeInfo (Writer writer , InfoSnapshot snapshot , EscapingScheme scheme )
413347 throws IOException {
414348 MetricMetadata metadata = snapshot .getMetadata ();
@@ -475,31 +409,6 @@ private void writeUnknown(Writer writer, UnknownSnapshot snapshot, EscapingSchem
475409 }
476410 }
477411
478- private void writeCountAndSum (
479- Writer writer ,
480- String name ,
481- DistributionDataPointSnapshot data ,
482- String countSuffix ,
483- String sumSuffix ,
484- Exemplars exemplars ,
485- EscapingScheme scheme )
486- throws IOException {
487- if (data .hasCount ()) {
488- writeNameAndLabels (writer , name , countSuffix , data .getLabels (), scheme );
489- writeLong (writer , data .getCount ());
490- if (exemplarsOnAllMetricTypesEnabled ) {
491- writeScrapeTimestampAndExemplar (writer , data , exemplars .getLatest (), scheme );
492- } else {
493- writeScrapeTimestampAndExemplar (writer , data , null , scheme );
494- }
495- }
496- if (data .hasSum ()) {
497- writeNameAndLabels (writer , name , sumSuffix , data .getLabels (), scheme );
498- writeDouble (writer , data .getSum ());
499- writeScrapeTimestampAndExemplar (writer , data , null , scheme );
500- }
501- }
502-
503412 private void writeCreated (
504413 Writer writer , String name , DataPointSnapshot data , EscapingScheme scheme )
505414 throws IOException {
0 commit comments