8000 Support `_total` suffix for counter sample names, according to the spec · alopezz/client_python@fa74120 · GitHub
[go: up one dir, main page]

Skip to content

Commit fa74120

Browse files
committed
Support _total suffix for counter sample names, according to the spec
Signed-off-by: Alex López <alex.lopez.zorzano@gmail.com>
1 parent c48e26e commit fa74120

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

prometheus_client/parser.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,8 @@ def build_metric(name: str, documentation: str, typ: str, samples: List[Sample])
158158
else:
159159
new_samples = []
160160
for s in samples:
161-
new_samples.append(Sample(s[0] + '_total', *s[1:]))
161+
sample_name = s.name if s.name.endswith('_total') else s.name + '_total'
162+
new_samples.append(Sample(sample_name, *s[1:]))
162163
samples = new_samples
163164
metric = Metric(name, documentation, typ)
164165
metric.samples = samples
@@ -194,7 +195,7 @@ def build_metric(name: str, documentation: str, typ: str, samples: List[Sample])
194195
samples = []
195196
typ = parts[3]
196197
allowed_names = {
197-
'counter': [''],
198+
'counter': ['_total', ''],
198199
'gauge': [''],
199200
'summary': ['_count', '_sum', ''],
200201
'histogram': ['_count', '_sum', '_bucket'],

tests/test_parser.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ def test_simple_counter(self):
2222
families = text_string_to_metric_families("""# TYPE a counter
2323
# HELP a help
2424
a 1
25+
""")
26+
self.assertEqualMetrics([CounterMetricFamily("a", "help", value=1)], list(families))
27+
28+
def test_counter_with_suffix(self):
29+
families = text_string_to_metric_families("""# TYPE a counter
30+
# HELP a help
31+
a_total 1
2532
""")
2633
self.assertEqualMetrics([CounterMetricFamily("a", "help", value=1)], list(families))
2734

0 commit comments

Comments
 (0)
0