From 5b741ccc182ac45e54460899988d8c2ead5538f2 Mon Sep 17 00:00:00 2001 From: Tanishq Choudhary Date: Fri, 20 Nov 2026 18:59:17 +0530 Subject: [PATCH] fix: add scrape timestamps to protobuf untyped metrics The protobuf writer sets `timestamp_ms` from the scrape timestamp for every data point type except unknown. An `UnknownDataPointSnapshot` with a scrape timestamp is written without it, while the Prometheus text and both OpenMetrics text writers include it. Prometheus reads `timestamp_ms` for untyped samples the same way it does for counters and gauges, so untyped series scraped over protobuf lost their explicit timestamps. One path that sets them is the simpleclient bridge, which copies `Sample.timestampMs` onto unknown data points. The writer now calls `setScrapeTimestamp` for unknown data points like it does for all other types. Signed-off-by: Tanishq Choudhary --- .../internal/PrometheusProtobufWriterImpl.java | 1 + .../expositionformats/ExpositionFormatsTest.java | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/prometheus-metrics-exposition-formats/src/main/java/io/prometheus/metrics/expositionformats/internal/PrometheusProtobufWriterImpl.java b/prometheus-metrics-exposition-formats/src/main/java/io/prometheus/metrics/expositionformats/internal/PrometheusProtobufWriterImpl.java index 734dc5b1d5..accd735493 100644 --- a/prometheus-metrics-exposition-formats/src/main/java/io/prometheus/metrics/expositionformats/internal/PrometheusProtobufWriterImpl.java +++ b/prometheus-metrics-exposition-formats/src/main/java/io/prometheus/metrics/expositionformats/internal/PrometheusProtobufWriterImpl.java @@ -283,6 +283,7 @@ private Metrics.Metric.Builder convert( untypedBuilder.setValue(data.getValue()); addLabels(metricBuilder, data.getLabels(), scheme); metricBuilder.setUntyped(untypedBuilder); + setScrapeTimestamp(metricBuilder, data); return metricBuilder; } diff --git a/prometheus-metrics-exposition-textformats/src/test/java/io/prometheus/metrics/expositionformats/ExpositionFormatsTest.java b/prometheus-metrics-exposition-textformats/src/test/java/io/prometheus/metrics/expositionformats/ExpositionFormatsTest.java index c875b94b0a..1fc6be67b0 100644 --- a/prometheus-metrics-exposition-textformats/src/test/java/io/prometheus/metrics/expositionformats/ExpositionFormatsTest.java +++ b/prometheus-metrics-exposition-textformats/src/test/java/io/prometheus/metrics/expositionformats/ExpositionFormatsTest.java @@ -2925,6 +2925,21 @@ void testUnknownComplete() throws IOException { + "my_special_thing_bytes{env=\"prod\"} 0.7 " + scrapeTimestamp2s + "\n"; + String prometheusProtobuf = + // @formatter:off + "name: \"my_special_thing_bytes\" " + + "help: \"help message\" " + + "type: UNTYPED " + + "metric { " + + "label { name: \"env\" value: \"dev\" } " + + "untyped { value: 0.2 } " + + "timestamp_ms: 1672850685829 " + + "} metric { " + + "label { name: \"env\" value: \"prod\" } " + + "untyped { value: 0.7 } " + + "timestamp_ms: 1672850585820 " + + "}"; + // @formatter:on UnknownSnapshot unknown = UnknownSnapshot.builder() .name("my_special_thing_bytes") @@ -2951,6 +2966,7 @@ void testUnknownComplete() throws IOException { assertPrometheusText(prometheus, unknown); assertOpenMetricsTextWithoutCreated(openMetrics, unknown); assertPrometheusTextWithoutCreated(prometheus, unknown); + assertPrometheusProtobuf(prometheusProtobuf, unknown); } @Test