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):