8000 Add ability to limit what HTTP exposition returns. · sonlinux/client_python@29e83ad · GitHub
[go: up one dir, main page]

Skip to content

Commit 29e83ad

Browse files
committed
8000
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

File tree

3 files changed

+50
-3
lines changed

3 files changed

+50
-3
lines changed

prometheus_client/core.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,35 @@ def collect(self):
9797
for metric in collector.collect():
9898
yield metric
9999

100+
def restricted_registry(self, names):
101+
'''Returns object that only collects some metrics.
102+
103+
Returns an object which upon collect() will return
104+
only samples with the given names.
105+
106+
Intended usage is:
107+
generate_latest(REGISTRY.restricted_registry(['a_timeseries']))
108+
109+
Experimental.'''
110+
names = set(names)
111+
collectors = set()
112+
with self._lock:
113+
for name in names:
114+
if name in self._names_to_collectors:
115+
collectors.add(self._names_to_collectors[name])
116+
metrics = []
117+
for collector in collectors:
118+
for metric in collector.collect():
119+
samples = [s for s in metric.samples if s[0] in names]
120+
if samples:
121+
m = Metric(metric.name, metric.documentation, metric.type)
122+
m.samples = samples
123+
metrics.append(m)
124+
class RestrictedRegistry(object):
125+
def collect(self):
126+
return metrics
127+
return RestrictedRegistry()
128+
100129
def get_sample_value(self, name, labels=None):
101130
'''Returns the sample value, or None if not found.
102131

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
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,15 @@ def test_autodescribe_disabled_by_default(self):
431431
self.custom_collector(CounterMetricFamily('c', 'help', value=1), registry)
432432
self.assertRaises(ValueError, self.custom_collector, CounterMetricFamily('c', 'help', value=1), registry)
433433

434+
def test_restricted_registry(self):
435+
registry = CollectorRegistry()
436+
Counter('c', 'help', registry=registry)
437+
Summary('s', 'help', registry=registry).observe(7)
438+
439+
m = Metric('s', 'help', 'summary')
440+
m.samples = [('s_sum', {}, 7)]
441+
self.assertEquals([m], registry.restricted_registry(['s_sum']).collect())
442+
434443

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

0 commit comments

Comments
 (0)
0