8000 Example on how to expose metrics in a Flask application (#297) · SysOfM/client_python@b476bff · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit b476bff

Browse files
banjohbrian-brazil
authored andcommitted
Example on how to expose metrics in a Flask application (prometheus#297)
Signed-off-by: Evans Mungai <mbuevans@gmail.com>
1 parent da8e6c6 commit b476bff

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,36 @@ from prometheus_client import start_wsgi_server
280280
start_wsgi_server(8000)
281281
```
282282

283+
#### Flask
284+
285+
To use Prometheus with [Flask](http://flask.pocoo.org/) we need to serve metrics through a Prometheus WSGI application. This can be achieved using [Flask's application dispatching](http://flask.pocoo.org/docs/latest/patterns/appdispatch/). Below is a working example.
286+
287+
Save the snippet below in a `myapp.py` file
288+
289+
```python
290+
from flask import Flask
291+
from werkzeug.wsgi import DispatcherMiddleware
292+
from prometheus_client import make_wsgi_app
293+
294+
# Create my app
295+
app = Flask(__name__)
296+
297+
# Add prometheus wsgi middleware to route /metrics requests
298+
app_dispatch = DispatcherMiddleware(app, {
299+
'/metrics': make_wsgi_app()
300+
})
301+
```
302+
303+
Run the example web application like this
304+
305+
```bash
306+
# Install uwsgi if you do not have it
307+
pip install uwsgi
308+
uwsgi --http 127.0.0.1:8000 --wsgi-file myapp.py --callable app_dispatch
309+
```
310+
311+
Visit http://localhost:8000/metrics to see the metrics
312+
283313
### Node exporter textfile collector
284314

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

0 commit comments

Comments
 (0)
0