Skip to content

Commit

Permalink
Cleaning up name before appending unit on name
Browse files Browse the repository at this point in the history
Signed-off-by: Amim Knabben <amim.knabben@gmail.com>
  • Loading branch information
knabben committed May 9, 2020
1 parent 0497442 commit 644d4cf
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 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
4 changes: 2 additions & 2 deletions tests/test_asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,11 @@ def get_all_output(self):
break
return outputs

def validate_metrics(self, metric_name, help_text, increments):
def validate_metrics(self, metric_name, help_text, increments, unit=''):
"""
ASGI app serves the metrics from the provided registry.
"""
c = Counter(metric_name, help_text, registry=self.registry)
c = Counter(metric_name, help_text, unit=unit, registry=self.registry)
for _ in range(increments):
c.inc()
# Create and run ASGI app
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 644d4cf

Please sign in to comment.