Skip to content

Commit

Permalink
Cleaning up name before appending unit on name (#543)
Browse files Browse the repository at this point in the history
Signed-off-by: Amim Knabben <amim.knabben@gmail.com>
  • Loading branch information
knabben authored May 10, 2020
1 parent 0497442 commit 18d9371
Show file tree
Hide file tree
Showing 3 changed files with 18 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
11 changes: 11 additions & 0 deletions tests/test_exposition.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,17 @@ def test_counter(self):
# HELP cc_created A counter
# TYPE cc_created gauge
cc_created 123.456
""", generate_latest(self.registry))

def test_counter_name_unit_append(self):
c = Counter('requests', 'Request counter', unit="total", registry=self.registry)
c.inc()
self.assertEqual(b"""# HELP requests_total_total Request counter
# TYPE requests_total_total counter
requests_total_total 1.0
# HELP requests_total_created Request counter
# TYPE requests_total_created gauge
requests_total_created 123.456
""", generate_latest(self.registry))

def test_counter_total(self):
Expand Down

0 comments on commit 18d9371

Please sign in to comment.