Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add __repr__ method to metric objects, make them debug friendly. #481

Merged
merged 2 commits into from
Oct 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions prometheus_client/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ def collect(self):
metric.add_sample(self._name + suffix, labels, value)
return [metric]

def __str__(self):
return "{0}:{1}".format(self._type, self._name)

def __repr__(self):
metric_type = type(self)
return "{0}.{1}({2})".format(metric_type.__module__, metric_type.__name__, self._name)

def __init__(self,
name,
documentation,
Expand Down
18 changes: 18 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,13 @@ def test_increment(self):
self.assertEqual(1, self.registry.get_sample_value('c_total'))
self.counter.inc(7)
self.assertEqual(8, self.registry.get_sample_value('c_total'))

def test_repr(self):
self.assertEqual(repr(self.counter), "prometheus_client.metrics.Counter(c)")

def test_negative_increment_raises(self):
self.assertRaises(ValueError, self.counter.inc, -1)


def test_function_decorator(self):
@self.counter.count_exceptions(ValueError)
Expand Down Expand Up @@ -83,6 +87,9 @@ class TestGauge(unittest.TestCase):
def setUp(self):
self.registry = CollectorRegistry()
self.gauge = Gauge('g', 'help', registry=self.registry)

def test_repr(self):
self.assertEqual(repr(self.gauge), "prometheus_client.metrics.Gauge(g)")

def test_gauge(self):
self.assertEqual(0, self.registry.get_sample_value('g'))
Expand Down Expand Up @@ -180,6 +187,9 @@ def setUp(self):
self.registry = CollectorRegistry()
self.summary = Summary('s', 'help', registry=self.registry)

def test_repr(self):
self.assertEqual(repr(self.summary), "prometheus_client.metrics.Summary(s)")

def test_summary(self):
self.assertEqual(0, self.registry.get_sample_value('s_count'))
self.assertEqual(0, self.registry.get_sample_value('s_sum'))
Expand Down Expand Up @@ -269,6 +279,10 @@ def setUp(self):
self.histogram = Histogram('h', 'help', registry=self.registry)
self.labels = Histogram('hl', 'help', ['l'], registry=self.registry)

def test_repr(self):
self.assertEqual(repr(self.histogram), "prometheus_client.metrics.Histogram(h)")
self.assertEqual(repr(self.labels), "prometheus_client.metrics.Histogram(hl)")

def test_histogram(self):
self.assertEqual(0, self.registry.get_sample_value('h_bucket', {'le': '1.0'}))
self.assertEqual(0, self.registry.get_sample_value('h_bucket', {'le': '2.5'}))
Expand Down Expand Up @@ -373,6 +387,10 @@ def setUp(self):
self.info = Info('i', 'help', registry=self.registry)
self.labels = Info('il', 'help', ['l'], registry=self.registry)

def test_repr(self):
self.assertEqual(repr(self.info), "prometheus_client.metrics.Info(i)")
self.assertEqual(repr(self.labels), "prometheus_client.metrics.Info(il)")

def test_info(self):
self.assertEqual(1, self.registry.get_sample_value('i_info', {}))
self.info.info({'a': 'b', 'c': 'd'})
Expand Down