| 1 | --- |
| 2 | title: Flask |
| 3 | weight: 4 |
| 4 | --- |
| 5 | |
| 6 | 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. |
| 7 | |
| 8 | Save the snippet below in a `myapp.py` file |
| 9 | |
| 10 | ```python |
| 11 | from flask import Flask |
| 12 | from werkzeug.middleware.dispatcher import DispatcherMiddleware |
| 13 | from prometheus_client import make_wsgi_app |
| 14 | |
| 15 | # Create my app |
| 16 | app = Flask(__name__) |
| 17 | |
| 18 | # Add prometheus wsgi middleware to route /metrics requests |
| 19 | app.wsgi_app = DispatcherMiddleware(app.wsgi_app, { |
| 20 | '/metrics': make_wsgi_app() |
| 21 | }) |
| 22 | ``` |
| 23 | |
| 24 | Run the example web application like this |
| 25 | |
| 26 | ```bash |
| 27 | # Install uwsgi if you do not have it |
| 28 | pip install uwsgi |
| 29 | uwsgi --http 127.0.0.1:8000 --wsgi-file myapp.py --callable app |
| 30 | ``` |
| 31 | |
| 32 | Visit http://localhost:8000/metrics to see the metrics |