Add ability to limit what HTTP exposition returns. · pythonAI/client_python@29e83ad · GitHub
Skip to content

Commit 29e83ad

Browse files
committed
Add ability to limit what HTTP exposition returns.
This works by specifiny URL parameters called name[]. Only the required collectors will be called, and then filtered down. This depends on the collectors sufficiently implementing describe.
1 parent 65e3c8b commit 29e83ad

3 files changed

Lines changed: 50 additions & 3 deletions

File tree

prometheus_client/core.py

Lines changed: 29 additions & 0 deletions

prometheus_client/exposition.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@
1515
from BaseHTTPServer import HTTPServer
1616
from urllib2 import build_opener, Request, HTTPHandler
1717
from urllib import quote_plus
18+
from urlparse import parse_qs, urlparse
1819
except ImportError:
1920
# Python 3
2021
unicode = str
2122
from http.server import BaseHTTPRequestHandler
2223
from http.server import HTTPServer
2324
from urllib.request import build_opener, Request, HTTPHandler
24-
from urllib.parse import quote_plus
25+
from urllib.parse import quote_plus, parse_qs, urlparse
2526

2627

2728
CONTENT_TYPE_LATEST = str('text/plain; version=0.0.4; charset=utf-8')
@@ -34,7 +35,11 @@ def prometheus_app(environ, start_response):
3435
status = str('200 OK')
3536
headers = [(str('Content-type'), CONTENT_TYPE_LATEST)]
3637
start_response(status, headers)
37-
return [generate_latest(registry)]
38+
params = parse_qs(environ['QUERY_STRING'])
39+
r = registry
40+
if 'name[]' in params:
41+
r = r.restricted_registry(params['name[]'])
42+
return [generate_latest(r)]
3843
return prometheus_app
3944

4045

@@ -73,7 +78,11 @@ def do_GET(self):
7378
self.send_response(200)
7479
self.send_header('Content-Type', CONTENT_TYPE_LATEST)
7580
self.end_headers()
76-
self.wfile.write(generate_latest(core.REGISTRY))
81+
registry = core.REGISTRY
82+
params = parse_qs(urlparse(self.path).query)
83+
if 'name[]' in params:
84+
registry = registry.restricted_registry(params['name[]'])
85+
self.wfile.write(generate_latest(registry))
7786

7887
def log_message(self, format, *args):
7988
return

tests/test_core.py

Lines changed: 9 additions & 0 deletions

0 commit comments

Comments
 (0)