10000 Merge pull request #78 from rmohr/master · Syncano/client_python@9a188ac · GitHub
[go: up one dir, main page]

Skip to content

Commit 9a188ac

Browse files
committed
Merge pull request prometheus#78 from rmohr/master
Add WSGI helper methods
2 parents 83d40c1 + 54260fa commit 9a188ac

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,32 @@ reactor.listenTCP(8000, factory)
247247
reactor.run()
248248
```
249249

250+
#### WSGI
251+
252+
To use Prometheus with [WSGI](http://wsgi.readthedocs.org/en/latest/), there is
253+
`make_wsgi_app` which creates a WSGI application.
254+
255+
```python
256+
from prometheus_client import make_wsgi_app
257+
from wsgiref.simple_server import make_server
258+
259+
app = make_wsgi_app()
260+
httpd = make_server('', 8000, app)
261+
httpd.serve_forever()
262+
```
263+
264+
Such an application can be useful when integrating Prometheus metrics with WSGI
265+
apps.
266+
267+
The method `start_wsgi_server` can be used to serve the metrics through the
268+
WSGI reference implementation in a new thread.
269+
270+
```python
271+
from prometheus_client import start_wsgi_server
272+
273+
start_wsgi_server(8000)
274+
```
275+
250276
### Node exporter textfile collector
251277

252278
The [textfile collector](https://github.com/prometheus/node_exporter#textfile-collector)

prometheus_client/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
CONTENT_TYPE_LATEST = exposition.CONTENT_TYPE_LATEST
2020
generate_latest = exposition.generate_latest
2121
MetricsHandler = exposition.MetricsHandler
22+
make_wsgi_app = exposition.make_wsgi_app
2223
start_http_server = exposition.start_http_server
24+
start_wsgi_server = exposition.start_wsgi_server
2325
write_to_textfile = exposition.write_to_textfile
2426
push_to_gateway = exposition.push_to_gateway
2527
pushadd_to_gateway = exposition.pushadd_to_gateway

prometheus_client/exposition.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import time
88
import threading
99
from contextlib import closing
10+
from wsgiref.simple_server import make_server
1011

1112
from . import core
1213
try:
@@ -23,10 +24,31 @@
2324
from urllib.parse import quote_plus
2425

2526

26-
CONTENT_TYPE_LATEST = 'text/plain; version=0.0.4; charset=utf-8'
27+
CONTENT_TYPE_LATEST = str('text/plain; version=0.0.4; charset=utf-8')
2728
'''Content type of the latest text format'''
2829

2930

31+
def make_wsgi_app():
32+
'''Create a WSGI app which serves the metrics from the registry.'''
33+
def prometheus_app(environ, start_response):
34+
status = str('200 OK')
35+
headers = [(str('Content-type'), CONTENT_TYPE_LATEST)]
36+
start_response(status, headers)
37+
return [generate_latest(core.REGISTRY)]
38+
return prometheus_app
39+
40+
41+
def start_wsgi_server(port, addr=''):
42+
"""Starts a WSGI server for prometheus metrics as a daemon thread."""
43+
class PrometheusMetricsServer(threading.Thread):
44+
def run(self):
45+
httpd = make_server(addr, port, make_wsgi_app())
46+
httpd.serve_forever()
47+
t = PrometheusMetricsServer()
48+
t.daemon = True
49+
t.start()
50+
51+
3052
def generate_latest(registry=core.REGISTRY):
3153
'''Returns the metrics from the registry in latest text format as a string.'''
3254
output = []

0 commit comments

Comments
 (0)
0