Skip to content

Commit

Permalink
Cleaning up name before appending unit on name
Browse files Browse the repository at this point in the history
  • Loading branch information
knabben committed May 9, 2020
1 parent 0497442 commit 7e86755
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
4 changes: 2 additions & 2 deletions prometheus_client/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ def _build_full_name(metric_type, name, namespace, subsystem, unit):
if subsystem:
full_name += subsystem + '_'
full_name += name
if metric_type == 'counter' and full_name.endswith('_total'):
full_name = full_name[:-6] # Munge to OpenMetrics.
if unit and not full_name.endswith("_" + unit):
full_name += "_" + unit
if unit and metric_type in ('info', 'stateset'):
raise ValueError('Metric name is of a type that cannot have a unit: ' + full_name)
if metric_type == 'counter' and full_name.endswith('_total'):
full_name = full_name[:-6] # Munge to OpenMetrics.
return full_name


Expand Down
5 changes: 5 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,11 @@ def test_no_units_for_info_enum(self):
self.assertRaises(ValueError, Info, 'foo', 'help', unit="x")
self.assertRaises(ValueError, Enum, 'foo', 'help', unit="x")

def test_name_cleanup_before_unit_append(self):
self.assertEqual(self.counter._name, 'c')
self.c = Counter('c_total', 'help', unit="total", labelnames=['l'], registry=self.registry)
self.assertEqual(self.c._name, 'c_total')


class TestMetricFamilies(unittest.TestCase):
def setUp(self):
Expand Down

0 comments on commit 7e86755

Please sign in to comment.