8000 Cleaning up name before appending unit on name (#543) · yzdann/client_python@18d9371 · GitHub
[go: up one dir, main page]

Skip to content

Commit 18d9371

Browse files
authored
Cleaning up name before appending unit on name (prometheus#543)
Signed-off-by: Amim Knabben <amim.knabben@gmail.com>
1 parent 0497442 commit 18d9371

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

prometheus_client/metrics.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ def _build_full_name(metric_type, name, namespace, subsystem, unit):
2727
if subsystem:
2828
full_name += subsystem + '_'
2929
full_name += name
30+
if metric_type == 'counter' and full_name.endswith('_total'):
31+
full_name = full_name[:-6] # Munge to OpenMetrics.
3032
if unit and not full_name.endswith("_" + unit):
3133
full_name += "_" + unit
3234
if unit and metric_type in ('info', 'stateset'):
3335
raise ValueError('Metric name is of a type that cannot have a unit: ' + full_name)
34-
if metric_type == 'counter' and full_name.endswith('_total'):
35-
full_name = full_name[:-6] # Munge to OpenMetrics.
3636
return full_name
3737

3838

tests/test_core.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,11 @@ def test_no_units_for_info_enum(self):
527527
self.assertRaises(ValueError, Info, 'foo', 'help', unit="x")
528528
self.assertRaises(ValueError, Enum, 'foo', 'help', unit="x")
529529

530+
def test_name_cleanup_before_unit_append(self):
531+
self.assertEqual(self.counter._name, 'c')
532+
self.c = Counter('c_total', 'help', unit="total", labelnames=['l'], registry=self.registry)
533+
self.assertEqual(self.c._name, 'c_total')
534+
530535

531536
class TestMetricFamilies(unittest.TestCase):
532537
def setUp(self):

tests/test_exposition.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,17 @@ def test_counter(self):
5959
# HELP cc_created A counter
6060
# TYPE cc_created gauge
6161
cc_created 123.456
62+
""", generate_latest(self.registry))
63+
64+
def test_counter_name_unit_append(self):
65+
c = Counter('requests', 'Request counter', unit="total", registry=self.registry)
66+
c.inc()
67+
self.assertEqual(b"""# HELP requests_total_total Request counter
68+
# TYPE requests_total_total counter
69+
requests_total_total 1.0
70+
# HELP requests_total_created Request counter
71+
# TYPE requests_total_created gauge
72+
requests_total_created 123.456
6273
""", generate_latest(self.registry))
6374

6475
def test_counter_total(self):

0 commit comments

Comments
 (0)
0