5

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.

3
  • 2
    That's nothing you should do on the application level but on the webserver level. And no, the builtin webserver is not something you should use in production. Commented May 30, 2012 at 21:21
  • I want to do this at the framework (flask/wsgi) level. I am not using the built in webserver. Commented May 31, 2012 at 3:24
  • @ThiefMaster collecting metrics in application and exporting them to Prometheus is a valid solution - WSGI server or proxies don't have all the information that matter, you cannot segment users, gather business metrics, separate speed by views (only by URL parsing), etc. There's plenty of advantages to this approach. Commented Aug 31, 2016 at 14:43

2 Answers 2

7

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.

Sign up to request clarification or add additional context in comments.

Comments

1

Check out New-Relic. It supports flask and will give you the measures you want (and more).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.