Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to collect performance metrics in a Flask Application?

Tags:

python

flask

What would be the best way to collect metrics on all HTTP requests made to a flask Application.

Things I would like to measure are :

  • Latency - time for each request.
  • Rate - No. of requests per minute etc.
  • No of failures - If there is a failure, how many etc.

Also I want to group requests to a variable path as one. For e.g All requests to the route '/resource/' should be measured for the metric named "RESOURCE" and not individually for each resource.

I plan to do this currently by writing a decorator. The disadvantage being I need to add the decorator for each method. Are there ways in which Flask can provide me hooks to measure these automatically.

like image 990
sheki Avatar asked May 30 '12 21:05

sheki


People also ask

What is Flask monitoring dashboard?

The Flask Monitoring Dashboard is an extension for Flask applications that offers four main functionalities with little effort from the Flask developer: Monitor the performance and utilization: The Dashboard allows you to see which endpoints process a lot of requests and how fast.

How many requests can Flask handle per second?

For reference, the Flask benchmarks on techempower give 25,000 requests per second.

Is Flask framework good for production?

Flask is a great way to get up and running quickly with a Python applications, but what if you wanted to make something a bit more robust? In this article, Toptal Freelance Python Developer Ivan PoleschyuI shares some tips and useful recipes for building a complete production-ready Flask application.


1 Answers

Check out the documentation for the flask.Flask.before_request and flask.Flask.teardown_request decorators. You'll want something simple and fast to send your metrics to - check out graphite and scales for an example suitable backend.

Once you have your log aggregating back end then it is a simple matter of registering two functions to execute before and after each request.

like image 77
Sean Vieira Avatar answered Oct 22 '22 01:10

Sean Vieira