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 1 commit
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
4 changes: 4 additions & 0 deletions prometheus_client/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ def collect(self):
metric.add_sample(self._name + suffix, labels, value)
return [metric]

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A repr() should in principle allow instantiation of the object. This would be a __str__. Also _type covers you more here.

Copy link
Contributor Author

@asherf asherf Oct 29, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as using the _type, I added an __str__ implementation that uses it.
I think having the full type path is useful in repr output which what is used in debuggers.
I updated the __repr__ implementation to use parenthesis instead of a column to be more compatible with the ability to pass eval
However, I feel that making it a 100% compatible with that (i.e. an eval(repr(metric)) that produces an identical object) will result in a string that will be too noisy and thus less useful when debugging.


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