Skip to content

Commit f1eba6f

Browse files
committed
refactor: delegate exemplar writing to OM1 writer for consistency
Apply the same delegation pattern as histogram/summary: when exemplarCompliance=false (default), delegate writeScrapeTimestampAndExemplar and writeExemplar to om1Writer. When exemplarCompliance=true, apply the OM2 spec requirement (drop exemplars without timestamps) then delegate the actual writing to om1Writer.writeExemplar. Extracts writeExemplar from om1Writer.writeScrapeTimestampAndExemplar so OM2 can delegate exemplar formatting without duplicating it. Signed-off-by: Gregor Zeitlinger <gregor.zeitlinger@grafana.com>
1 parent 6e683d3 commit f1eba6f

File tree

2 files changed

+26
-22
lines changed

2 files changed

+26
-22
lines changed

prometheus-metrics-exposition-textformats/src/main/java/io/prometheus/metrics/expositionformats/OpenMetrics2TextFormatWriter.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ private void writeCompositeHistogramDataPoint(
257257
writer.write(" st@");
258258
writeOpenMetricsTimestamp(writer, data.getCreatedTimestampMillis());
259259
}
260-
writeExemplarIfAllowed(writer, data.getExemplars().getLatest(), scheme);
260+
writeExemplar(writer, data.getExemplars().getLatest(), scheme);
261261
writer.write('\n');
262262
}
263263

@@ -339,7 +339,7 @@ private void writeCompositeSummaryDataPoint(
339339
writer.write(" st@");
340340
writeOpenMetricsTimestamp(writer, data.getCreatedTimestampMillis());
341341
}
342-
writeExemplarIfAllowed(writer, data.getExemplars().getLatest(), scheme);
342+
writeExemplar(writer, data.getExemplars().getLatest(), scheme);
343343
writer.write('\n');
344344
}
345345

@@ -467,30 +467,30 @@ private void writeNameAndLabels(
467467
private void writeScrapeTimestampAndExemplar(
468468
Writer writer, DataPointSnapshot data, @Nullable Exemplar exemplar, EscapingScheme scheme)
469469
throws IOException {
470+
if (!openMetrics2Properties.getExemplarCompliance()) {
471+
om1Writer.writeScrapeTimestampAndExemplar(writer, data, exemplar, scheme);
472+
return;
473+
}
470474
if (data.hasScrapeTimestamp()) {
471475
writer.write(' ');
472476
writeOpenMetricsTimestamp(writer, data.getScrapeTimestampMillis());
473477
}
474-
writeExemplarIfAllowed(writer, exemplar, scheme);
478+
writeExemplar(writer, exemplar, scheme);
475479
writer.write('\n');
476480
}
477481

478-
private void writeExemplarIfAllowed(
479-
Writer writer, @Nullable Exemplar exemplar, EscapingScheme scheme) throws IOException {
482+
private void writeExemplar(Writer writer, @Nullable Exemplar exemplar, EscapingScheme scheme)
483+
throws IOException {
480484
if (exemplar == null) {
481485
return;
482486
}
483-
// In exemplarCompliance mode, exemplars MUST have a timestamp per the OM2 spec.
484-
if (openMetrics2Properties.getExemplarCompliance() && !exemplar.hasTimestamp()) {
487+
if (!openMetrics2Properties.getExemplarCompliance()) {
488+
om1Writer.writeExemplar(writer, exemplar, scheme);
485489
return;
486490
}
487-
writer.write(" # ");
488-
writeLabels(writer, exemplar.getLabels(), null, 0, false, scheme);
489-
writer.write(' ');
490-
writeDouble(writer, exemplar.getValue());
491+
// exemplarCompliance=true: exemplars MUST have a timestamp per the OM2 spec.
491492
if (exemplar.hasTimestamp()) {
492-
writer.write(' ');
493-
writeOpenMetricsTimestamp(writer, exemplar.getTimestampMillis());
493+
om1Writer.writeExemplar(writer, exemplar, scheme);
494494
}
495495
}
496496

prometheus-metrics-exposition-textformats/src/main/java/io/prometheus/metrics/expositionformats/OpenMetricsTextFormatWriter.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -424,26 +424,30 @@ private void writeNameAndLabels(
424424
writer.write(' ');
425425
}
426426

427-
private void writeScrapeTimestampAndExemplar(
427+
void writeScrapeTimestampAndExemplar(
428428
Writer writer, DataPointSnapshot data, @Nullable Exemplar exemplar, EscapingScheme scheme)
429429
throws IOException {
430430
if (data.hasScrapeTimestamp()) {
431431
writer.write(' ');
432432
writeOpenMetricsTimestamp(writer, data.getScrapeTimestampMillis());
433433
}
434434
if (exemplar != null) {
435-
writer.write(" # ");
436-
writeLabels(writer, exemplar.getLabels(), null, 0, false, scheme);
437-
writer.write(' ');
438-
writeDouble(writer, exemplar.getValue());
439-
if (exemplar.hasTimestamp()) {
440-
writer.write(' ');
441-
writeOpenMetricsTimestamp(writer, exemplar.getTimestampMillis());
442-
}
435+
writeExemplar(writer, exemplar, scheme);
443436
}
444437
writer.write('\n');
445438
}
446439

440+
void writeExemplar(Writer writer, Exemplar exemplar, EscapingScheme scheme) throws IOException {
441+
writer.write(" # ");
442+
writeLabels(writer, exemplar.getLabels(), null, 0, false, scheme);
443+
writer.write(' ');
444+
writeDouble(writer, exemplar.getValue());
445+
if (exemplar.hasTimestamp()) {
446+
writer.write(' ');
447+
writeOpenMetricsTimestamp(writer, exemplar.getTimestampMillis());
448+
}
449+
}
450+
447451
/**
448452
* Returns the full exposition name for a metric. If the original name already ends with the given
449453
* suffix (e.g. "_total" for counters), uses the original name directly. Otherwise, appends the

0 commit comments

Comments
 (0)