diff --git a/prometheus-metrics-core/src/test/java/io/prometheus/metrics/core/metrics/HistogramTest.java b/prometheus-metrics-core/src/test/java/io/prometheus/metrics/core/metrics/HistogramTest.java index cbfd5fade1..8d9359a058 100644 --- a/prometheus-metrics-core/src/test/java/io/prometheus/metrics/core/metrics/HistogramTest.java +++ b/prometheus-metrics-core/src/test/java/io/prometheus/metrics/core/metrics/HistogramTest.java @@ -91,9 +91,12 @@ private void run() throws NoSuchFieldException, IllegalAccessException { histogram.observe(observation); } } - Metrics.MetricFamily protobufData = + Metrics.MetricFamily.Builder protobufData = new PrometheusProtobufWriterImpl() - .convert(histogram.collect(), EscapingScheme.ALLOW_UTF8); + .convert(histogram.collect(), EscapingScheme.ALLOW_UTF8).toBuilder(); + // The client_golang test cases don't include the created timestamp. + assertThat(protobufData.getMetric(0).getHistogram().hasCreatedTimestamp()).isTrue(); + protobufData.getMetricBuilder(0).getHistogramBuilder().clearCreatedTimestamp(); String expectedWithMetadata = "name: \"test\" type: HISTOGRAM metric { histogram { " + expected + " } }"; assertThat(ProtobufUtil.shortDebugString(protobufData)) @@ -978,8 +981,11 @@ void testDefaults() throws IOException { """; // protobuf - Metrics.MetricFamily protobufData = - new PrometheusProtobufWriterImpl().convert(snapshot, EscapingScheme.ALLOW_UTF8); + Metrics.MetricFamily.Builder protobufData = + new PrometheusProtobufWriterImpl().convert(snapshot, EscapingScheme.ALLOW_UTF8).toBuilder(); + assertThat(protobufData.getMetric(0).getHistogram().getCreatedTimestamp().getSeconds()) + .isEqualTo(snapshot.getDataPoints().get(0).getCreatedTimestampMillis() / 1000); + protobufData.getMetricBuilder(0).getHistogramBuilder().clearCreatedTimestamp(); assertThat(ProtobufUtil.shortDebugString(protobufData)).isEqualTo(expectedProtobuf); // text 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..c7c6d4e780 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 @@ -220,6 +220,10 @@ private Metrics.Metric.Builder convert( if (data.hasSum()) { histogramBuilder.setSampleSum(data.getSum()); } + if (data.hasCreatedTimestamp()) { + histogramBuilder.setCreatedTimestamp( + ProtobufUtil.timestampFromMillis(data.getCreatedTimestampMillis())); + } metricBuilder.setHistogram(histogramBuilder.build()); return metricBuilder; } @@ -241,6 +245,10 @@ private Metrics.Metric.Builder convert( .setValue(quantiles.get(i).getValue()) .build()); } + if (data.hasCreatedTimestamp()) { + summaryBuilder.setCreatedTimestamp( + ProtobufUtil.timestampFromMillis(data.getCreatedTimestampMillis())); + } Metrics.Metric.Builder metricBuilder = Metrics.Metric.newBuilder(); addLabels(metricBuilder, data.getLabels(), scheme); metricBuilder.setSummary(summaryBuilder.build()); 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..0f35adc639 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 @@ -1014,6 +1014,7 @@ 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 { " @@ -1024,6 +1025,7 @@ 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 " + "}"; @@ -1587,6 +1589,7 @@ void testClassicHistogramComplete() throws Exception { + exemplar2protoString + " " + "} " + + "created_timestamp { seconds: 1672850385 nanos: 800000000 } " + "} " + "} metric { " + "label { name: \"status\" value: \"500\" } " @@ -1608,6 +1611,7 @@ void testClassicHistogramComplete() throws Exception { + exemplar2protoString + " " + "} " + + "created_timestamp { seconds: 1672850285 } " + "} " + "}"; // @formatter:on @@ -2016,6 +2020,7 @@ void testClassicGaugeHistogramComplete() throws IOException { + exemplar2protoString + " " + "} " + + "created_timestamp { seconds: 1672850385 nanos: 800000000 } " + "} " + "} metric { " + "label { name: \"db\" value: \"options\" } " @@ -2034,6 +2039,7 @@ void testClassicGaugeHistogramComplete() throws IOException { + exemplar2protoString + " " + "} " + + "created_timestamp { seconds: 1672850285 } " + "} " + "}"; // @formatter:on @@ -2435,6 +2441,7 @@ void testNativeHistogramComplete() throws IOException { + "zero_count: 0 " + "positive_span { offset: 0 length: 1 } " + "positive_delta: 2 " + + "created_timestamp { seconds: 1672850385 nanos: 800000000 } " + "} " + "} metric { " + "label { name: \"status\" value: \"500\" } " @@ -2500,7 +2507,8 @@ void testNativeHistogramComplete() throws IOException { + // index 46, count 2 "positive_delta: -1 " + // index 47, count 1 - "} " + "created_timestamp { seconds: 1672850285 } " + + "} " + "}"; // @formatter:on HistogramSnapshot nativeHistogram =