Skip to content

Commit

Permalink
Allow Exemplars on histogram _count (#930)
Browse files Browse the repository at this point in the history
Signed-off-by: Fabian Stäber <[email protected]>
  • Loading branch information
fstab authored Mar 21, 2024
1 parent fc447f8 commit 40c3599
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public boolean getIncludeCreatedTimestamps() {

/**
* Allow Exemplars on all metric types in OpenMetrics format?
* Default is {@code false}, which means Exemplars will only be added for Counters and Histograms.
* Default is {@code false}, which means Exemplars will only be added for Counters and Histogram buckets.
*/
public boolean getExemplarsOnAllMetricTypes() {
return exemplarsOnAllMetricTypes != null && exemplarsOnAllMetricTypes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,9 @@ private void writeClassicHistogramBuckets(OutputStreamWriter writer, MetricMetad
}
writeScrapeTimestampAndExemplar(writer, data, exemplar);
}
// In OpenMetrics format, histogram _count and _sum are either both present or both absent.
if (data.hasCount() && data.hasSum()) {
// In OpenMetrics format, histogram _count and _sum are either both present or both absent.
// While Prometheus allows Exemplars for histogram's _count and _sum now, we don't
// use Exemplars here to be backwards compatible with previous behavior.
writeCountAndSum(writer, metadata, data, countSuffix, sumSuffix, Exemplars.EMPTY);
writeCountAndSum(writer, metadata, data, countSuffix, sumSuffix, exemplars);
}
writeCreated(writer, metadata, data);
}
Expand Down Expand Up @@ -241,33 +239,27 @@ private void writeUnknown(OutputStreamWriter writer, UnknownSnapshot snapshot) t
writeNameAndLabels(writer, metadata.getPrometheusName(), null, data.getLabels());
writeDouble(writer, data.getValue());
if (exemplarsOnAllMetricTypesEnabled) {
writeScrapeTimestampAndExemplar(writer, data, data.getExemplar());
writeScrapeTimestampAndExemplar(writer, data, data.getExemplar());
} else {
writeScrapeTimestampAndExemplar(writer, data, null);
}
}
}

private void writeCountAndSum(OutputStreamWriter writer, MetricMetadata metadata, DistributionDataPointSnapshot data, String countSuffix, String sumSuffix, Exemplars exemplars) throws IOException {
int exemplarIndex = 0;
if (data.hasCount()) {
writeNameAndLabels(writer, metadata.getPrometheusName(), countSuffix, data.getLabels());
writeLong(writer, data.getCount());
if (exemplars.size() > 0) {
writeScrapeTimestampAndExemplar(writer, data, exemplars.get(exemplarIndex));
exemplarIndex = exemplarIndex + 1 % exemplars.size();
if (exemplarsOnAllMetricTypesEnabled) {
writeScrapeTimestampAndExemplar(writer, data, exemplars.getLatest());
} else {
writeScrapeTimestampAndExemplar(writer, data, null);
}
}
if (data.hasSum()) {
writeNameAndLabels(writer, metadata.getPrometheusName(), sumSuffix, data.getLabels());
writeDouble(writer, data.getSum());
if (exemplars.size() > 0) {
writeScrapeTimestampAndExemplar(writer, data, exemplars.get(exemplarIndex));
} else {
writeScrapeTimestampAndExemplar(writer, data, null);
}
writeScrapeTimestampAndExemplar(writer, data, null);
}
}

Expand Down
Loading

0 comments on commit 40c3599

Please sign in to comment.