Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions prometheus_client/openmetrics/exposition.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
def _is_valid_exemplar_metric(metric, sample):
if metric.type == 'counter' and sample.name.endswith('_total'):
return True
if metric.type in ('gaugehistogram') and sample.name.endswith('_bucket'):
if metric.type == 'gaugehistogram' and sample.name.endswith('_bucket'):
return True
if metric.type in ('histogram') and sample.name.endswith('_bucket') or sample.name == metric.name:
if metric.type == 'histogram' and (sample.name.endswith('_bucket') or sample.name == metric.name):
return True
return False

Expand Down
15 changes: 15 additions & 0 deletions tests/openmetrics/test_exposition.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,21 @@ def collect(self):
with self.assertRaises(ValueError):
generate_latest(self.registry)

def test_gauge_exemplar(self) -> None:
class MyCollector:
def collect(self):
metric = Metric("gg", "A gauge", 'gauge')
# A sample whose name equals the metric name must not be
# treated as exemplar-eligible just because it matches;
# only histogram/gaugehistogram buckets, counter _total, and
# native histograms may carry exemplars.
metric.add_sample("gg", {}, 1, None, Exemplar({'a': 'b'}, 0.5))
yield metric

self.registry.register(MyCollector())
with self.assertRaises(ValueError):
generate_latest(self.registry)

def test_gaugehistogram(self) -> None:
self.custom_collector(
GaugeHistogramMetricFamily('gh', 'help', buckets=[('1.0', 4), ('+Inf', (5))], gsum_value=7))
Expand Down