A Prometheus middleware to add basic but very useful metrics for your Python Flask app.
As valid Big Brother library it exposes the following metrics:
request_seconds_bucket{type, status, isError, method, addr, le}
request_seconds_count{type, status, isError, method, addr}
request_seconds_sum{type, status, isError, method, addr}
response_size_bytes{type, status, isError, method, addr}
dependency_up{name}
application_info{version}
In detail:
-
The
request_seconds_bucketmetric defines the histogram of how many requests are falling into the well defined buckets represented by the labelle; -
The
request_seconds_countis a counter that counts the overall number of requests with those exact label occurrences; -
The
request_seconds_sumis a counter that counts the overall sum of how long the requests with those exact label occurrences are taking; -
The
response_size_bytesis a counter that computes how much data is being sent back to the user for a given request type. It captures the response size from thecontent-lengthresponse header. If there is no such header, the value exposed as metric will be zero; -
The
dependency_upis a metric to register weather a specific dependency is up (1) or down (0). The labelnameregisters the dependency name; -
Finally,
application_infoholds static info of an application, such as it's semantic version number;
For a specific request:
typetells which request protocol was used (e.g.grpc,http,<your custom protocol>);statusregisters the response status code;isErrorlet you know if the request's response status is considered an error;methodregisters the request method (e.g.GETfor http get requests);addrregisters the requested endpoint address;- and
versiontells which version of your service has handled the request;
Add this package as a dependency:
pip install bb-flask-monitor
or
pipenv install bb-flask-monitor
Use it as middleware:
from flask import Flask
from prometheus_client import make_wsgi_app
from werkzeug.middleware.dispatcher import DispatcherMiddleware
from werkzeug.serving import run_simple
from flask_monitor import register_metrics
app = Flask(__name__)
app.config["APP_VERSION"] = "v0.1.2"
register_metrics(app)
# Plug metrics WSGI app to your main app with dispatcher
dispatcher = DispatcherMiddleware(app.wsgi_app, {"/metrics": make_wsgi_app()})One can optionally define the buckets of observation for the request_second histogram by doing:
register_metrics(app, buckets=[0.1]); // where only one bucket (of 100ms) will be given as output in the /metrics endpointOther optional parameters are also:
error_fn: an error callback to define what you consider as error.4**and5**considered as errors by default;
For you to know when a dependency is up or down, just provide a health check callback to be watch_dependencies function:
app = Flask(__name__)
app.config["APP_VERSION"] = "v0.1.2"
register_metrics(app)
# Plug metrics WSGI app to your main app with dispatcher
dispatcher = DispatcherMiddleware(app.wsgi_app, {"/metrics": make_wsgi_app()})
def check_db():
try:
response = req.get("http://localhost:7000/bd")
if response.status_code == 200:
return 1
except:
traceback.print_stack()
return 0
watch_dependencies("Bd", check_db, app=app)Other optional parameters are also:
watch_dependencies has the following parameters:
-
dependency: the name of the dependency; -
func: the health check callback function; -
time_execution: the interval time in seconds thatfuncwill be called.
Now run your app and point prometheus to the defined metrics endpoint of your server.
More details on how Prometheus works, you can find it here.
In the example folder, you'll find a very simple but useful example to get you started. On your terminal, navigate to the project's root folder and type:
cd example
pipenv installand then
python app.pyOn your browser, go to localhost:5000 and then go to localhost:5000/metrics to see the exposed metrics.
This is part of a more large application called Big Brother.