Skip to content
Prometheus Metrics for Flask Web App
Python Makefile
Branch: master
Clone or download
pyup-bot and pilosus Dependency update Scheduled weekly dependency update for week 24 (#18)
* Update prometheus-client from 0.6.0 to 0.7.0

* Update pytest from 4.5.0 to 4.6.3

* Pin codecov to latest version 2.0.15

* Pin safety to latest version 1.8.5
Latest commit 0b6e9dd Jun 18, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
flask_prometheus_metrics Package import simplified (#1) Jun 5, 2019
tests README added Jun 1, 2019
.gitignore README added Jun 1, 2019
.pyup.yml pyup integration added (#2) Jun 5, 2019
.travis.yml
CHANGELOG.md Classifiers fixed (#12) (#16) Jun 6, 2019
LICENSE Initial commit Jun 1, 2019
MANIFEST.in Package versioning fix Jun 1, 2019
Makefile Tweak CI config and Makefile (#13) Jun 6, 2019
README.md Fix relative links in README (#13) (#15) Jun 6, 2019
coverage.xml README added Jun 1, 2019
requirements.txt Dependency update Scheduled weekly dependency update for week 24 (#18) Jun 18, 2019
setup.cfg Tweak CI config and Makefile (#13) Jun 6, 2019
setup.py Classifiers fixed (#12) (#16) Jun 6, 2019
version.py Package versioning fix Jun 1, 2019

README.md

Flask Prometheus Metrics

Build Status Test Coverage Maintainability Code style: black

Prometheus metrics exporter for Flask web applications.

flask_prometheus_metrics uses official Prometheus Python Client providing basic metrics about process resource usage, app's requests metrics and information.

Installation

pip install -U flask_prometheus_metrics

You will need Flask to run examples below:

pip install -U 'flask_prometheus_metrics[flask]'

Usage

Run the following minimal example in Python shell:

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_prometheus_metrics import register_metrics

app = Flask(__name__)

@app.route("/")
def index():
    return "Test"

# provide app's version and deploy environment/config name to set a gauge metric
register_metrics(app, app_version="v0.1.2", app_config="staging")

# Plug metrics WSGI app to your main app with dispatcher
dispatcher = DispatcherMiddleware(app.wsgi_app, {"/metrics": make_wsgi_app()})

run_simple(hostname="localhost", port=5000, application=dispatcher)

Then go over http://localhost:5000/, refresh page a few times and check your app's metrics at http://localhost:5000/metrics.

See also example.py for more elaborate example of library usage in real Flask applications.

Metrics

flask_prometheus_metrics exposes the following application metrics:

  • app_request_latency_seconds (histogram) - Application request latency
  • app_request_count_total (counter) - application request count
  • app_version_info (gauge) - application version

Library also provides some metrics about a Python interpreter used and process resource usage:

  • python_gc_objects_collected_total (counter) - objects collected during gc
  • python_gc_objects_uncollectable_total (counter) - uncollectable object found during GC
  • python_gc_collections_total (counter) - number of times this generation was collected
  • python_info (gauge) - Python platform information
  • process_virtual_memory_bytes (gauge) - virtual memory size in bytes
  • process_resident_memory_bytes (gauge) - resident memory size in bytes
  • process_start_time_seconds (gauge) - start time of the process since unix epoch in seconds
  • process_cpu_seconds_total (counter) - total user and system CPU time spent in seconds
  • process_open_fds (gauge) - number of open file descriptors
  • process_max_fds (gauge) - maximum number of open file descriptors

Grafana dashboard

The metrics exported by flask_prometheus_metrics can be scraped by Prometheus monitoring system and then visualized in Grafana.

You can download Grafana dashboard crafted specifically for the flask_prometheus_metrics default metrics here.

Grafana visualisation

Testing

When testing Flask application with DispatcherMiddleware (see Usage example above) you may want to use a little hack in order to make Flask's test_client() work properly.

You can’t perform that action at this time.