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 695e1bd
Show file tree
Hide file tree
Showing 4 changed files with 39 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
17 changes: 17 additions & 0 deletions tests/test_asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,20 @@ def test_report_metrics_3(self):

def test_report_metrics_4(self):
self.validate_metrics("failed_requests", "Number of failed requests", 7)

def test_report_metrics_with_unit_append(self):
help_text = "Requests counter"
increments = 6

c = Counter("requests", help_text, unit="total", registry=self.registry)
c.inc(increments)

app = make_asgi_app(self.registry)
self.seed_app(app)
self.send_default_request()
outputs = self.get_all_output()
output = outputs[1]['body'].decode('utf8')

self.assertIn("# HELP requests_total_total {0}".format(help_text), output)
self.assertIn("# TYPE requests_total_total counter\n", output)
self.assertIn("requests_total_total " + str(increments) + ".0\n", output)
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
15 changes: 15 additions & 0 deletions tests/test_wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,18 @@ def test_report_metrics_3(self):

def test_report_metrics_4(self):
self.validate_metrics("failed_requests", "Number of failed requests", 7)

def test_report_metrics_with_unit_append(self):
help_text = "Requests counter"
increments = 6

c = Counter("requests", help_text, unit="total", registry=self.registry)
c.inc(increments)

app = make_wsgi_app(self.registry)
outputs = app(self.environ, self.capture)
output = outputs[0].decode('utf8')

self.assertIn("# HELP requests_total_total {0}".format(help_text), output)
self.assertIn("# TYPE requests_total_total counter\n", output)
self.assertIn("requests_total_total " + str(increments) + ".0\n", output)

0 comments on commit 695e1bd

Please sign in to comment.