diff --git a/prometheus-metrics-exposition-formats/src/main/java/io/prometheus/metrics/expositionformats/ExpositionFormats.java b/prometheus-metrics-exposition-formats/src/main/java/io/prometheus/metrics/expositionformats/ExpositionFormats.java index 01ea9ef40..6baad933e 100644 --- a/prometheus-metrics-exposition-formats/src/main/java/io/prometheus/metrics/expositionformats/ExpositionFormats.java +++ b/prometheus-metrics-exposition-formats/src/main/java/io/prometheus/metrics/expositionformats/ExpositionFormats.java @@ -24,7 +24,7 @@ public static ExpositionFormats init() { public static ExpositionFormats init(ExporterProperties properties) { return new ExpositionFormats( - new PrometheusProtobufWriter(), + new PrometheusProtobufWriter(properties.getIncludeCreatedTimestamps()), new PrometheusTextFormatWriter(properties.getIncludeCreatedTimestamps()), new OpenMetricsTextFormatWriter( properties.getIncludeCreatedTimestamps(), properties.getExemplarsOnAllMetricTypes())); diff --git a/prometheus-metrics-exposition-formats/src/main/java/io/prometheus/metrics/expositionformats/PrometheusProtobufWriter.java b/prometheus-metrics-exposition-formats/src/main/java/io/prometheus/metrics/expositionformats/PrometheusProtobufWriter.java index 87e50ed8b..f928c7d7e 100644 --- a/prometheus-metrics-exposition-formats/src/main/java/io/prometheus/metrics/expositionformats/PrometheusProtobufWriter.java +++ b/prometheus-metrics-exposition-formats/src/main/java/io/prometheus/metrics/expositionformats/PrometheusProtobufWriter.java @@ -3,6 +3,7 @@ import static io.prometheus.metrics.expositionformats.ProtobufUtil.timestampFromMillis; import com.google.protobuf.TextFormat; +import com.google.protobuf.Timestamp; import io.prometheus.metrics.expositionformats.generated.com_google_protobuf_4_28_2.Metrics; import io.prometheus.metrics.model.snapshots.ClassicHistogramBuckets; import io.prometheus.metrics.model.snapshots.CounterSnapshot; @@ -35,6 +36,16 @@ public class PrometheusProtobufWriter implements ExpositionFormatWriter { public static final String CONTENT_TYPE = "application/vnd.google.protobuf; proto=io.prometheus.client.MetricFamily; encoding=delimited"; + private final boolean writeCreatedTimestamps; + + public PrometheusProtobufWriter() { + this(false); + } + + public PrometheusProtobufWriter(boolean writeCreatedTimestamps) { + this.writeCreatedTimestamps = writeCreatedTimestamps; + } + @Override public boolean accepts(String acceptHeader) { if (acceptHeader == null) { @@ -149,6 +160,9 @@ private Metrics.Metric.Builder convert(CounterDataPointSnapshot data) { counterBuilder.setExemplar(convert(data.getExemplar())); } addLabels(metricBuilder, data.getLabels()); + if (writeCreatedTimestamps) { + counterBuilder.setCreatedTimestamp(createTimestamp(data)); + } metricBuilder.setCounter(counterBuilder.build()); setScrapeTimestamp(metricBuilder, data); return metricBuilder; @@ -209,12 +223,16 @@ private Metrics.Metric.Builder convert(HistogramSnapshot.HistogramDataPointSnaps } addLabels(metricBuilder, data.getLabels()); setScrapeTimestamp(metricBuilder, data); + if (data.hasCount()) { histogramBuilder.setSampleCount(data.getCount()); } if (data.hasSum()) { histogramBuilder.setSampleSum(data.getSum()); } + if (writeCreatedTimestamps) { + histogramBuilder.setCreatedTimestamp(createTimestamp(data)); + } metricBuilder.setHistogram(histogramBuilder.build()); return metricBuilder; } @@ -302,6 +320,9 @@ private Metrics.Metric.Builder convert(SummarySnapshot.SummaryDataPointSnapshot .build()); } addLabels(metricBuilder, data.getLabels()); + if (writeCreatedTimestamps) { + summaryBuilder.setCreatedTimestamp(createTimestamp(data)); + } metricBuilder.setSummary(summaryBuilder.build()); setScrapeTimestamp(metricBuilder, data); return metricBuilder; @@ -378,4 +399,11 @@ private void setScrapeTimestamp(Metrics.Metric.Builder metricBuilder, DataPointS metricBuilder.setTimestampMs(data.getScrapeTimestampMillis()); } } + + private Timestamp createTimestamp(DataPointSnapshot data) { + final long createdTimestamp = data.getCreatedTimestampMillis(); + final long seconds = createdTimestamp / 1000; + final int nanos = (int) (createdTimestamp % 1000 * 1000000); + return Timestamp.newBuilder().setSeconds(seconds).setNanos(nanos).build(); + } } diff --git a/prometheus-metrics-exposition-formats/src/test/java/io/prometheus/metrics/expositionformats/ExpositionFormatsTest.java b/prometheus-metrics-exposition-formats/src/test/java/io/prometheus/metrics/expositionformats/ExpositionFormatsTest.java index cdcfd5f0b..fdcef92c1 100644 --- a/prometheus-metrics-exposition-formats/src/test/java/io/prometheus/metrics/expositionformats/ExpositionFormatsTest.java +++ b/prometheus-metrics-exposition-formats/src/test/java/io/prometheus/metrics/expositionformats/ExpositionFormatsTest.java @@ -177,6 +177,31 @@ public void testCounterComplete() throws IOException { + scrapeTimestamp2s + "\n"; String prometheusProtobuf = + // @formatter:off + "name: \"service_time_seconds_total\" " + + "help: \"total time spent serving\" " + + "type: COUNTER " + + "metric { " + + "label { name: \"path\" value: \"/hello\" } " + + "label { name: \"status\" value: \"200\" } " + + "counter { " + + "value: 0.8 " + + exemplar1protoString + + " created_timestamp { seconds: 1672850385 nanos: 800000000 } " + + "} " + + "timestamp_ms: 1672850685829 " + + "} " + + "metric { " + + "label { name: \"path\" value: \"/hello\" } " + + "label { name: \"status\" value: \"500\" } " + + "counter { " + + "value: 0.9 " + + exemplar2protoString + + " created_timestamp { seconds: 1672850285 } " + + "} " + + "timestamp_ms: 1672850585820 " + + "}"; + String prometheusProtobufWithoutCreated = // @formatter:off "name: \"service_time_seconds_total\" " + "help: \"total time spent serving\" " @@ -230,6 +255,7 @@ public void testCounterComplete() throws IOException { assertOpenMetricsTextWithoutCreated(openMetricsTextWithoutCreated, counter); assertPrometheusTextWithoutCreated(prometheusTextWithoutCreated, counter); assertPrometheusProtobuf(prometheusProtobuf, counter); + assertPrometheusProtobufWithoutCreated(prometheusProtobufWithoutCreated, counter); } @Test @@ -247,7 +273,7 @@ public void testCounterMinimal() throws IOException { assertPrometheusText(prometheusText, counter); assertOpenMetricsTextWithoutCreated(openMetricsText, counter); assertPrometheusTextWithoutCreated(prometheusText, counter); - assertPrometheusProtobuf(prometheusProtobuf, counter); + assertPrometheusProtobufWithoutCreated(prometheusProtobuf, counter); } @Test @@ -287,7 +313,7 @@ public void testCounterWithDots() throws IOException { .build(); assertOpenMetricsText(openMetricsText, counter); assertPrometheusText(prometheusText, counter); - assertPrometheusProtobuf(prometheusProtobuf, counter); + assertPrometheusProtobufWithoutCreated(prometheusProtobuf, counter); } @Test @@ -368,7 +394,7 @@ public void testGaugeComplete() throws IOException { assertPrometheusText(prometheusText, gauge); assertOpenMetricsTextWithoutCreated(openMetricsText, gauge); assertPrometheusTextWithoutCreated(prometheusText, gauge); - assertPrometheusProtobuf(prometheusProtobuf, gauge); + assertPrometheusProtobufWithoutCreated(prometheusProtobuf, gauge); } @Test @@ -388,7 +414,7 @@ public void testGaugeMinimal() throws IOException { assertPrometheusText(prometheusText, gauge); assertOpenMetricsTextWithoutCreated(openMetricsText, gauge); assertPrometheusTextWithoutCreated(prometheusText, gauge); - assertPrometheusProtobuf(prometheusProtobuf, gauge); + assertPrometheusProtobufWithoutCreated(prometheusProtobuf, gauge); } @Test @@ -440,7 +466,7 @@ public void testGaugeWithDots() throws IOException { assertOpenMetricsTextWithExemplarsOnAllTimeSeries( openMetricsTextWithExemplarsOnAllTimeSeries, gauge); assertPrometheusText(prometheusText, gauge); - assertPrometheusProtobuf(prometheusProtobuf, gauge); + assertPrometheusProtobufWithoutCreated(prometheusProtobuf, gauge); } @Test @@ -664,6 +690,32 @@ public void testSummaryComplete() throws IOException { + "http_request_duration_seconds_sum{status=\"500\"} 2.2 " + scrapeTimestamp2s + "\n"; + String prometheusProtobufWithoutCreated = + // @formatter:off + "name: \"http_request_duration_seconds\" " + + "help: \"request duration\" " + + "type: SUMMARY " + + "metric { " + + "label { name: \"status\" value: \"200\" } " + + "summary { " + + "sample_count: 3 " + + "sample_sum: 1.2 " + + "quantile { quantile: 0.5 value: 225.3 } " + + "quantile { quantile: 0.9 value: 240.7 } " + + "quantile { quantile: 0.95 value: 245.1 } " + + "} " + + "timestamp_ms: 1672850685829 " + + "} metric { " + + "label { name: \"status\" value: \"500\" } " + + "summary { " + + "sample_count: 7 " + + "sample_sum: 2.2 " + + "quantile { quantile: 0.5 value: 225.3 } " + + "quantile { quantile: 0.9 value: 240.7 } " + + "quantile { quantile: 0.95 value: 245.1 } " + + "} " + + "timestamp_ms: 1672850585820 " + + "}"; String prometheusProtobuf = // @formatter:off "name: \"http_request_duration_seconds\" " @@ -677,6 +729,7 @@ public void testSummaryComplete() throws IOException { + "quantile { quantile: 0.5 value: 225.3 } " + "quantile { quantile: 0.9 value: 240.7 } " + "quantile { quantile: 0.95 value: 245.1 } " + + "created_timestamp { seconds: 1672850385 nanos: 800000000 } " + "} " + "timestamp_ms: 1672850685829 " + "} metric { " @@ -687,6 +740,7 @@ public void testSummaryComplete() throws IOException { + "quantile { quantile: 0.5 value: 225.3 } " + "quantile { quantile: 0.9 value: 240.7 } " + "quantile { quantile: 0.95 value: 245.1 } " + + "created_timestamp { seconds: 1672850285 } " + "} " + "timestamp_ms: 1672850585820 " + "}"; @@ -733,6 +787,7 @@ public void testSummaryComplete() throws IOException { assertPrometheusText(prometheusText, summary); assertOpenMetricsTextWithoutCreated(openMetricsTextWithoutCreated, summary); assertPrometheusTextWithoutCreated(prometheusTextWithoutCreated, summary); + assertPrometheusProtobufWithoutCreated(prometheusProtobufWithoutCreated, summary); assertPrometheusProtobuf(prometheusProtobuf, summary); } @@ -773,7 +828,7 @@ public void testSummaryWithoutQuantiles() throws IOException { assertPrometheusText(prometheusText, summary); assertOpenMetricsTextWithoutCreated(openMetricsText, summary); assertPrometheusTextWithoutCreated(prometheusText, summary); - assertPrometheusProtobuf(prometheusProtobuf, summary); + assertPrometheusProtobufWithoutCreated(prometheusProtobuf, summary); } @Test @@ -806,7 +861,7 @@ public void testSummaryNoCountAndSum() throws IOException { assertPrometheusText(prometheusText, summary); assertOpenMetricsTextWithoutCreated(openMetricsText, summary); assertPrometheusTextWithoutCreated(prometheusText, summary); - assertPrometheusProtobuf(prometheusProtobuf, summary); + assertPrometheusProtobufWithoutCreated(prometheusProtobuf, summary); } @Test @@ -833,7 +888,7 @@ public void testSummaryJustCount() throws IOException { assertPrometheusText(prometheusText, summary); assertOpenMetricsTextWithoutCreated(openMetricsText, summary); assertPrometheusTextWithoutCreated(prometheusText, summary); - assertPrometheusProtobuf(prometheusProtobuf, summary); + assertPrometheusProtobufWithoutCreated(prometheusProtobuf, summary); } @Test @@ -860,7 +915,7 @@ public void testSummaryJustSum() throws IOException { assertPrometheusText(prometheusText, summary); assertOpenMetricsTextWithoutCreated(openMetricsText, summary); assertPrometheusTextWithoutCreated(prometheusText, summary); - assertPrometheusProtobuf(prometheusProtobuf, summary); + assertPrometheusProtobufWithoutCreated(prometheusProtobuf, summary); } @Test @@ -878,7 +933,7 @@ public void testSummaryEmptyData() throws IOException { assertPrometheusText("", summary); assertOpenMetricsTextWithoutCreated("# EOF\n", summary); assertPrometheusTextWithoutCreated("", summary); - assertPrometheusProtobuf("", summary); + assertPrometheusProtobufWithoutCreated("", summary); } @Test @@ -920,7 +975,7 @@ public void testSummaryEmptyAndNonEmpty() throws IOException { assertPrometheusText(prometheusText, summary); assertOpenMetricsTextWithoutCreated(openMetricsText, summary); assertPrometheusTextWithoutCreated(prometheusText, summary); - assertPrometheusProtobuf(prometheusProtobuf, summary); + assertPrometheusProtobufWithoutCreated(prometheusProtobuf, summary); } @Test @@ -974,7 +1029,7 @@ public void testSummaryWithDots() throws IOException { assertOpenMetricsTextWithExemplarsOnAllTimeSeries( openMetricsTextWithExemplarsOnAllTimeSeries, summary); assertPrometheusText(prometheusText, summary); - assertPrometheusProtobuf(prometheusProtobuf, summary); + assertPrometheusProtobufWithoutCreated(prometheusProtobuf, summary); } @Test @@ -1196,6 +1251,53 @@ public void testClassicHistogramComplete() throws Exception { + scrapeTimestamp2s + "\n"; String prometheusProtobuf = + // @formatter:off + "name: \"response_size_bytes\" " + + "help: \"help\" " + + "type: HISTOGRAM " + + "metric { " + + "label { name: \"status\" value: \"200\" } " + + "timestamp_ms: 1672850685829 " + + "histogram { " + + "sample_count: 3 " + + "sample_sum: 4.1 " + + "bucket { " + + "cumulative_count: 2 " + + "upper_bound: 2.2 " + + exemplar1protoString + + " " + + "} bucket { " + + "cumulative_count: 3 " + + "upper_bound: Infinity " + + exemplar2protoString + + " " + + "} " + + "created_timestamp { seconds: 1672850385 nanos: 800000000 } " + + "} " + + "} metric { " + + "label { name: \"status\" value: \"500\" } " + + "timestamp_ms: 1672850585820 " + + "histogram { " + + "sample_count: 5 " + + "sample_sum: 3.2 " + + "bucket { " + + "cumulative_count: 3 " + + "upper_bound: 1.0 " + + "} bucket { " + + "cumulative_count: 5 " + + "upper_bound: 2.2 " + + exemplar1protoString + + " " + + "} bucket { " + + "cumulative_count: 5 " + + "upper_bound: Infinity " + + exemplar2protoString + + " " + + "} " + + "created_timestamp { seconds: 1672850285 } " + + "} " + + "}"; + String prometheusProtobufWithoutCreated = // @formatter:off "name: \"response_size_bytes\" " + "help: \"help\" " @@ -1281,6 +1383,7 @@ public void testClassicHistogramComplete() throws Exception { assertOpenMetricsTextWithoutCreated(openMetricsTextWithoutCreated, histogram); assertPrometheusTextWithoutCreated(prometheusTextWithoutCreated, histogram); assertPrometheusProtobuf(prometheusProtobuf, histogram); + assertPrometheusProtobufWithoutCreated(prometheusProtobufWithoutCreated, histogram); } @Test @@ -1324,7 +1427,7 @@ public void testClassicHistogramMinimal() throws Exception { assertPrometheusText(prometheusText, histogram); assertOpenMetricsTextWithoutCreated(openMetricsText, histogram); assertPrometheusTextWithoutCreated(prometheusText, histogram); - assertPrometheusProtobuf(prometheusProtobuf, histogram); + assertPrometheusProtobufWithoutCreated(prometheusProtobuf, histogram); } @Test @@ -1371,7 +1474,7 @@ public void testClassicHistogramCountAndSum() throws Exception { assertPrometheusText(prometheusText, histogram); assertOpenMetricsTextWithoutCreated(openMetricsText, histogram); assertPrometheusTextWithoutCreated(prometheusText, histogram); - assertPrometheusProtobuf(prometheusProtobuf, histogram); + assertPrometheusProtobufWithoutCreated(prometheusProtobuf, histogram); } @Test @@ -1554,6 +1657,50 @@ public void testClassicGaugeHistogramComplete() throws IOException { + scrapeTimestamp2s + "\n" + "# EOF\n"; + String prometheusProtobuf = + // @formatter:off + "name: \"cache_size_bytes\" " + + "help: \"number of bytes in the cache\" " + + "type: GAUGE_HISTOGRAM " + + "metric { " + + "label { name: \"db\" value: \"items\" } " + + "timestamp_ms: 1672850685829 " + + "histogram { " + + "sample_count: 4 " + + "sample_sum: 17.0 " + + "bucket { " + + "cumulative_count: 3 " + + "upper_bound: 2.0 " + + exemplar1protoString + + " " + + "} bucket { " + + "cumulative_count: 4 " + + "upper_bound: Infinity " + + exemplar2protoString + + " " + + "} " + + "created_timestamp { seconds: 1672850385 nanos: 800000000 } " + + "} " + + "} metric { " + + "label { name: \"db\" value: \"options\" } " + + "timestamp_ms: 1672850585820 " + + "histogram { " + + "sample_count: 4 " + + "sample_sum: 18.0 " + + "bucket { " + + "cumulative_count: 4 " + + "upper_bound: 2.0 " + + exemplar1protoString + + " " + + "} bucket { " + + "cumulative_count: 4 " + + "upper_bound: Infinity " + + exemplar2protoString + + " " + + "} " + + "created_timestamp { seconds: 1672850285 } " + + "} " + + "}"; String prometheusTextWithoutCreated = "# HELP cache_size_bytes number of bytes in the cache\n" + "# TYPE cache_size_bytes histogram\n" @@ -1585,7 +1732,7 @@ public void testClassicGaugeHistogramComplete() throws IOException { + "cache_size_bytes_gsum{db=\"options\"} 18.0 " + scrapeTimestamp2s + "\n"; - String prometheusProtobuf = + String prometheusProtobufWithoutCreated = // @formatter:off "name: \"cache_size_bytes\" " + "help: \"number of bytes in the cache\" " @@ -1668,6 +1815,7 @@ public void testClassicGaugeHistogramComplete() throws IOException { assertOpenMetricsTextWithoutCreated(openMetricsTextWithoutCreated, gaugeHistogram); assertPrometheusTextWithoutCreated(prometheusTextWithoutCreated, gaugeHistogram); assertPrometheusProtobuf(prometheusProtobuf, gaugeHistogram); + assertPrometheusProtobufWithoutCreated(prometheusProtobufWithoutCreated, gaugeHistogram); } @Test @@ -1713,7 +1861,7 @@ public void testClassicGaugeHistogramMinimal() throws IOException { assertPrometheusText(prometheusText, gaugeHistogram); assertOpenMetricsTextWithoutCreated(openMetricsText, gaugeHistogram); assertPrometheusTextWithoutCreated(prometheusText, gaugeHistogram); - assertPrometheusProtobuf(prometheusProtobuf, gaugeHistogram); + assertPrometheusProtobufWithoutCreated(prometheusProtobuf, gaugeHistogram); } @Test @@ -1763,7 +1911,7 @@ public void testClassicGaugeHistogramCountAndSum() throws IOException { assertPrometheusText(prometheusText, gaugeHistogram); assertOpenMetricsTextWithoutCreated(openMetricsText, gaugeHistogram); assertPrometheusTextWithoutCreated(prometheusText, gaugeHistogram); - assertPrometheusProtobuf(prometheusProtobuf, gaugeHistogram); + assertPrometheusProtobufWithoutCreated(prometheusProtobuf, gaugeHistogram); } @Test @@ -1833,7 +1981,7 @@ public void testClassicHistogramWithDots() throws IOException { assertOpenMetricsTextWithExemplarsOnAllTimeSeries( openMetricsTextWithExemplarsOnAllTimeSeries, histogram); assertPrometheusText(prometheusText, histogram); - assertPrometheusProtobuf(prometheusProtobuf, histogram); + assertPrometheusProtobufWithoutCreated(prometheusProtobuf, histogram); } @Test @@ -1998,6 +2146,94 @@ public void testNativeHistogramComplete() throws IOException { + scrapeTimestamp2s + "\n"; String prometheusProtobuf = + // @formatter:off + "name: \"response_size_bytes\" " + + "help: \"help\" " + + "type: HISTOGRAM " + + "metric { " + + "label { name: \"status\" value: \"200\" } " + + "timestamp_ms: 1672850685829 " + + "histogram { " + + "sample_count: 2 " + + "sample_sum: 4.2 " + + "bucket { cumulative_count: 2 upper_bound: Infinity " + + exemplar2protoString + + " } " + + "schema: 5 " + + "zero_threshold: 0.0 " + + "zero_count: 0 " + + "positive_span { offset: 0 length: 1 } " + + "positive_delta: 2 " + + "created_timestamp { seconds: 1672850385 nanos: 800000000 } " + + "} " + + "} metric { " + + "label { name: \"status\" value: \"500\" } " + + "timestamp_ms: 1672850585820 " + + "histogram { " + + "sample_count: 55 " + + // bucket counts + zero count + "sample_sum: 3.2 " + + "bucket { cumulative_count: 55 upper_bound: Infinity " + + exemplar2protoString + + " } " + + "schema: 5 " + + "zero_threshold: 0.0 " + + "zero_count: 1 " + + "negative_span { offset: 0 length: 1 } " + + "negative_span { offset: 9 length: 1 } " + + "negative_delta: 1 " + + "negative_delta: -1 " + + // span with count 0 + "positive_span { offset: 2 length: 3 } " + + // span with 3 buckets (indexes 2-4) + "positive_span { offset: 7 length: 1 } " + + // span with 1 bucket (index 12) + "positive_span { offset: 9 length: 4 } " + + // span with gap of size 1 (indexes 22-25) + "positive_span { offset: 6 length: 5 } " + + // span with gap of size 2 (indexes 32-36) + "positive_span { offset: 4 length: 2 } " + + // span with gap of size 3 part 1 (indexes 41-42) + "positive_span { offset: 3 length: 2 } " + + // span with gap of size 3 part 2 (indexes 46-47) + "positive_delta: 3 " + + // index 2, count 3 + "positive_delta: 2 " + + // index 3, count 5 + "positive_delta: -1 " + + // index 4, count 4 + "positive_delta: 2 " + + // index 12, count 6 + "positive_delta: -4 " + + // index 22, count 2 + "positive_delta: -2 " + + // index 23, gap + "positive_delta: 1 " + + // index 24, count 1 + "positive_delta: 2 " + + // index 25, count 3 + "positive_delta: 1 " + + // index 32, count 4 + "positive_delta: -1 " + + // index 33, count 3 + "positive_delta: -3 " + + // index 34, gap + "positive_delta: 0 " + + // index 35, gap + "positive_delta: 7 " + + // index 36, count 7 + "positive_delta: -4 " + + // index 41, count 3 + "positive_delta: 6 " + + // index 42, count 9 + "positive_delta: -7 " + + // index 46, count 2 + "positive_delta: -1 " + + // index 47, count 1 + "created_timestamp { seconds: 1672850285 } " + + "} " + + "}"; + String prometheusProtobufWithoutCreated = // @formatter:off "name: \"response_size_bytes\" " + "help: \"help\" " @@ -2145,6 +2381,7 @@ public void testNativeHistogramComplete() throws IOException { assertOpenMetricsTextWithoutCreated(openMetricsTextWithoutCreated, nativeHistogram); assertPrometheusTextWithoutCreated(prometheusTextWithoutCreated, nativeHistogram); assertPrometheusProtobuf(prometheusProtobuf, nativeHistogram); + assertPrometheusProtobufWithoutCreated(prometheusProtobufWithoutCreated, nativeHistogram); } @Test @@ -2178,7 +2415,7 @@ public void testNativeHistogramMinimal() throws IOException { .build(); assertOpenMetricsText(openMetricsText, nativeHistogram); assertPrometheusText(prometheusText, nativeHistogram); - assertPrometheusProtobuf(prometheusProtobuf, nativeHistogram); + assertPrometheusProtobufWithoutCreated(prometheusProtobuf, nativeHistogram); } @Test @@ -2253,7 +2490,7 @@ public void testNativeHistogramWithDots() throws IOException { assertOpenMetricsTextWithExemplarsOnAllTimeSeries( openMetricsTextWithExemplarsOnAllTimeSeries, histogram); assertPrometheusText(prometheusText, histogram); - assertPrometheusProtobuf(prometheusProtobuf, histogram); + assertPrometheusProtobufWithoutCreated(prometheusProtobuf, histogram); } // TODO: Gauge Native Histogram @@ -2316,7 +2553,7 @@ public void testInfoWithDots() throws IOException { .build(); assertOpenMetricsText(openMetricsText, info); assertPrometheusText(prometheusText, info); - assertPrometheusProtobuf(prometheusProtobuf, info); + assertPrometheusProtobufWithoutCreated(prometheusProtobuf, info); } @Test @@ -2442,7 +2679,7 @@ public void testStateSetWithDots() throws IOException { .build(); assertOpenMetricsText(openMetricsText, stateSet); assertPrometheusText(prometheusText, stateSet); - assertPrometheusProtobuf(prometheusProtobuf, stateSet); + assertPrometheusProtobufWithoutCreated(prometheusProtobuf, stateSet); } @Test @@ -2571,7 +2808,7 @@ public void testUnknownWithDots() throws IOException { assertOpenMetricsTextWithExemplarsOnAllTimeSeries( openMetricsWithExemplarsOnAllTimeSeries, unknown); assertPrometheusText(prometheus, unknown); - assertPrometheusProtobuf(prometheusProtobuf, unknown); + assertPrometheusProtobufWithoutCreated(prometheusProtobuf, unknown); } @Test @@ -2660,6 +2897,13 @@ private void assertPrometheusTextWithoutCreated(String expected, MetricSnapshot< } private void assertPrometheusProtobuf(String expected, MetricSnapshot snapshot) { + PrometheusProtobufWriter writer = new PrometheusProtobufWriter(true); + Metrics.MetricFamily protobufData = writer.convert(snapshot); + String actual = TextFormatUtil.shortDebugString(protobufData); + assertThat(actual).isEqualTo(expected); + } + + private void assertPrometheusProtobufWithoutCreated(String expected, MetricSnapshot snapshot) { PrometheusProtobufWriter writer = new PrometheusProtobufWriter(); Metrics.MetricFamily protobufData = writer.convert(snapshot); String actual = TextFormatUtil.shortDebugString(protobufData);