File tree Expand file tree Collapse file tree 3 files changed +51
-1
lines changed Expand file tree Collapse file tree 3 files changed +51
-1
lines changed Original file line number Diff line number Diff line change @@ -247,6 +247,32 @@ reactor.listenTCP(8000, factory)
247247reactor.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
252278The [ textfile collector] ( https://github.com/prometheus/node_exporter#textfile-collector )
Original file line number Diff line number Diff line change 1919CONTENT_TYPE_LATEST = exposition .CONTENT_TYPE_LATEST
2020generate_latest = exposition .generate_latest
2121MetricsHandler = exposition .MetricsHandler
22+ make_wsgi_app = exposition .make_wsgi_app
2223start_http_server = exposition .start_http_server
24+ start_wsgi_server = exposition .start_wsgi_server
2325write_to_textfile = exposition .write_to_textfile
2426push_to_gateway = exposition .push_to_gateway
2527pushadd_to_gateway = exposition .pushadd_to_gateway
Original file line number Diff line number Diff line change 77import time
88import threading
99from contextlib import closing
10+ from wsgiref .simple_server import make_server
1011
1112from . import core
1213try :
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+
3052def generate_latest (registry = core .REGISTRY ):
3153 '''Returns the metrics from the registry in latest text format as a string.'''
3254 output = []
You can’t perform that action at this time.
0 commit comments