8000 Add exemplar support to CounterMetricFamily (#1063) · HelloBroBro/client_python@37cd873 · GitHub
[go: up one dir, main page]

Skip to content

Commit 37cd873

Browse files
authored
Add exemplar support to CounterMetricFamily (prometheus#1063)
Fixes prometheus#1062 Signed-off-by: David Tulloh <git-david@tulloh.id.au>
1 parent d7c9cd8 commit 37cd873

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

prometheus_client/metrics_core.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ def __init__(self,
116116
labels: Optional[Sequence[str]] = None,
117117
created: Optional[float] = None,
118118
unit: str = '',
119+
exemplar: Optional[Exemplar] = None,
119120
):
120121
# Glue code for pre-OpenMetrics metrics.
121122
if name.endswith('_total'):
@@ -127,13 +128,14 @@ def __init__(self,
127128
labels = []
128129
self._labelnames = tuple(labels)
129130
if value is not None:
130-
self.add_metric([], value, created)
131+
self.add_metric([], value, created, exemplar=exemplar)
131132

132133
def add_metric(self,
133134
labels: Sequence[str],
134135
value: float,
135136
created: Optional[float] = None,
136137
timestamp: Optional[Union[Timestamp, float]] = None,
138+
exemplar: Optional[Exemplar] = None,
137139
) -> None:
138140
"""Add a metric to the metric family.
139141
@@ -142,7 +144,7 @@ def add_metric(self,
142144
value: The value of the metric
143145
created: Optional unix timestamp the child was created at.
144146
"""
145-
self.samples.append(Sample(self.name + '_total', dict(zip(self._labelnames, labels)), value, timestamp))
147+
self.samples.append(Sample(self.name + '_total', dict(zip(self._labelnames, labels)), value, timestamp, exemplar))
146148
if created is not None:
147149
self.samples.append(Sample(self.name + '_created', dict(zip(self._labelnames, labels)), created, timestamp))
148150

tests/test_core.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,21 @@ def test_counter_labels(self):
724724
self.custom_collector(cmf)
725725
self.assertEqual(2, self.registry.get_sample_value('c_total', {'a': 'b', 'c_total': 'd'}))
726726

727+
def test_counter_exemplars_oneline(self):
728+
cmf = CounterMetricFamily('c_total', 'help', value=23, exemplar={"bob": "osbourne"})
729+
self.custom_collector(cmf)
730+
sample = [c.samples for c in self.registry.collect()][0][0]
731+
self.assertDictEqual({"bob": "osbourne"}, sample.exemplar)
732+
733+
def test_counter_exemplars_add(self):
734+
cmf = CounterMetricFamily('c_total', 'help')
735+
cmf.add_metric([], 12, exemplar={"bob": "osbourne"}, created=23)
736+
self.custom_collector(cmf)
737+
total_sample, created_sample = [c.samples for c in self.registry.collect()][0]
738+
self.assertEqual("c_created", created_sample.name)
739+
self.assertDictEqual({"bob": "osbourne"}, total_sample.exemplar)
740+
self.assertIsNone(created_sample.exemplar)
741+
727742
def test_gauge(self):
728743
self.custom_collector(GaugeMetricFamily('g', 'help', value=1))
729744
self.assertEqual(1, self.registry.get_sample_value('g', {}))

0 commit comments

Comments
 (0)
0