File tree 3 files changed +51
-1
lines changed 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)
247
247
reactor.run()
248
248
```
249
249
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
+
250
276
### Node exporter textfile collector
251
277
252
278
The [ textfile collector] ( https://github.com/prometheus/node_exporter#textfile-collector )
Original file line number Diff line number Diff line change 19
19
CONTENT_TYPE_LATEST = exposition .CONTENT_TYPE_LATEST
20
20
generate_latest = exposition .generate_latest
21
21
MetricsHandler = exposition .MetricsHandler
22
+ make_wsgi_app = exposition .make_wsgi_app
22
23
start_http_server = exposition .start_http_server
24
+ start_wsgi_server = exposition .start_wsgi_server
23
25
write_to_textfile = exposition .write_to_textfile
24
26
push_to_gateway = exposition .push_to_gateway
25
27
pushadd_to_gateway = exposition .pushadd_to_gateway
Original file line number Diff line number Diff line change 7
7
import time
8
8
import threading
9
9
from contextlib import closing
10
+ from wsgiref .simple_server import make_server
10
11
11
12
from . import core
12
13
try :
23
24
from urllib .parse import quote_plus
24
25
25
26
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' )
27
28
'''Content type of the latest text format'''
28
29
29
30
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
+
30
52
def generate_latest (registry = core .REGISTRY ):
31
53
'''Returns the metrics from the registry in latest text format as a string.'''
32
54
output = []
You can’t perform that action at this time.
0 commit comments