From de10d4dc768c3778ff9d2e109c091f2f7a6dea88 Mon Sep 17 00:00:00 2001 From: Christopher Krah <48338417+chrikrah@users.noreply.github.com> Date: Mon, 16 Nov 2026 10:11:17 +0100 Subject: [PATCH] Skip native histogram samples in the text format The Prometheus text format has no native histograms, so generate_latest wrote a native histogram sample as a bare sample under the histogram's TYPE line, and a sample whose value is None failed the scrape with a TypeError. OpenMetrics 1.0 output already skips these samples (#1128). Signed-off-by: Christopher Krah <48338417+chrikrah@users.noreply.github.com> --- prometheus_client/exposition.py | 3 +++ tests/test_exposition.py | 13 +++++++++++++ 2 files changed, 16 insertions(+) diff --git a/prometheus_client/exposition.py b/prometheus_client/exposition.py index 281fb1ea..af6a9fe6 100644 --- a/prometheus_client/exposition.py +++ b/prometheus_client/exposition.py @@ -339,6 +339,9 @@ def sample_line(samples): om_samples: Dict[str, List[str]] = {} for s in metric.samples: + # The text format has no native histograms, as in OpenMetrics 1.0. + if s.native_histogram: + continue for suffix in ['_created', '_gsum', '_gcount']: if s.name == metric.name + suffix: # OpenMetrics specific sample, put in a gauge at the end. diff --git a/tests/test_exposition.py b/tests/test_exposition.py index 92930d8e..aaa249f9 100644 --- a/tests/test_exposition.py +++ b/tests/test_exposition.py @@ -168,6 +168,19 @@ def test_gaugehistogram(self): # HELP gh_gsum help # TYPE gh_gsum gauge gh_gsum 7.0 +""", generate_latest(self.registry)) + + def test_native_histogram_is_skipped(self): + hfm = core.HistogramMetricFamily('nh', 'help', buckets=[('1.0', 4), ('+Inf', 5)], sum_value=7) + hfm.add_sample('nh', {}, 0, None, None, core.NativeHistogram( + 5, 7, 0, 0.001, 0, (core.BucketSpan(0, 1),), None, (5,), None)) + self.custom_collector(hfm) + self.assertEqual(b"""# HELP nh help +# TYPE nh histogram +nh_bucket{le="1.0"} 4.0 +nh_bucket{le="+Inf"} 5.0 +nh_count 5.0 +nh_sum 7.0 """, generate_latest(self.registry)) def test_info(self):