8000 Permit subclassing of MetricsHandler by jeanralphaviles · Pull Request #339 · prometheus/client_python · GitHub
[go: up one dir, main page]

Skip to content

Permit subclassing of MetricsHandler #339

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

Merged
merged 1 commit into from
Nov 7, 2018
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
8 changes: 4 additions & 4 deletions prometheus_client/exposition.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ def do_GET(self):
def log_message(self, format, *args):
"""Log nothing."""

@staticmethod
def factory(registry):
@classmethod
def factory(cls, registry):
"""Returns a dynamic MetricsHandler class tied
to the passed registry.
"""
Expand All @@ -164,8 +164,8 @@ def factory(registry):

# As we have unicode_literals, we need to create a str()
# object for type().
cls_name = str('MetricsHandler')
MyMetricsHandler = type(cls_name, (MetricsHandler, object),
cls_name = str(cls.__name__)
MyMetricsHandler = type(cls_name, (cls, object),
{"registry": registry})
return MyMetricsHandler

Expand Down
6 changes: 6 additions & 0 deletions tests/test_exposition.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,12 @@ def test_metrics_handler(self):
handler = MetricsHandler.factory(self.registry)
self.assertEqual(handler.registry, self.registry)

def test_metrics_handler_subclassing(self):
subclass = type(str('MetricsHandlerSubclass'), (MetricsHandler, object), {})
handler = subclass.factory(self.registry)

self.assertTrue(issubclass(handler, (MetricsHandler, subclass)))


if __name__ == '__main__':
unittest.main()
0