Skip to content
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
23 changes: 20 additions & 3 deletions prometheus_client/exposition.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,10 @@ def generate_latest(registry=core.REGISTRY):

class MetricsHandler(BaseHTTPRequestHandler):
"""HTTP handler that gives metrics from ``core.REGISTRY``."""
registry = core.REGISTRY

def do_GET(self):
registry = core.REGISTRY
registry = self.registry
params = parse_qs(urlparse(self.path).query)
if 'name[]' in params:
registry = registry.restricted_registry(params['name[]'])
Expand All @@ -102,14 +103,30 @@ def do_GET(self):
def log_message(self, format, *args):
"""Log nothing."""

@staticmethod
def factory(registry):
"""Returns a dynamic MetricsHandler class tied
to the passed registry.
"""
# This implementation relies on MetricsHandler.registry
# (defined above and defaulted to core.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),
{"registry": registry})
return MyMetricsHandler


class _ThreadingSimpleServer(ThreadingMixIn, HTTPServer):
"""Thread per request HTTP server."""


def start_http_server(port, addr=''):
def start_http_server(port, addr='', registry=core.REGISTRY):
"""Starts an HTTP server for prometheus metrics as a daemon thread"""
httpd = _ThreadingSimpleServer((addr, port), MetricsHandler)
CustomMetricsHandler = MetricsHandler.factory(registry)
httpd = _ThreadingSimpleServer((addr, port), CustomMetricsHandler)
t = threading.Thread(target=httpd.serve_forever)
t.daemon = True
t.start()
Expand Down
1 change: 1 addition & 0 deletions prometheus_client/multiprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from . import core


class MultiProcessCollector(object):
"""Collector for files for multi-process mode."""
def __init__(self, registry, path=None):
Expand Down
6 changes: 5 additions & 1 deletion tests/test_exposition.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
from prometheus_client import CollectorRegistry, generate_latest
from prometheus_client import push_to_gateway, pushadd_to_gateway, delete_from_gateway
from prometheus_client import CONTENT_TYPE_LATEST, instance_ip_grouping_key
from prometheus_client.exposition import default_handler, basic_auth_handler
from prometheus_client import core
from prometheus_client.exposition import default_handler, basic_auth_handler, MetricsHandler

try:
from BaseHTTPServer import BaseHTTPRequestHandler
Expand Down Expand Up @@ -195,6 +196,9 @@ def my_auth_handler(url, method, timeout, headers, data):
def test_instance_ip_grouping_key(self):
self.assertTrue('' != instance_ip_grouping_key()['instance'])

def test_metrics_handler(self):
MyHandler = MetricsHandler.factory(core.REGISTRY)


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