Skip to main content
This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (
Help us improve Python packaging - Donate today!

Prometheus metrics exporter for Flask

Project Description

Prometheus Flask exporter

|Travis| |PyPI| |PyPI| |Coverage Status| |Code Climate|

This library provides HTTP request metrics to export into
`Prometheus <>`__. It can also track method
invocations using convenient functions.


.. code:: python

from flask import Flask, request
from prometheus_flask_exporter import PrometheusMetrics

app = Flask(__name__)
metrics = PrometheusMetrics(app)

# static information as metric'app_info', 'Application info', version='1.0.3')

def main():
pass # requests tracked by default

def skip():
pass # default metrics are not collected

@metrics.counter('invocation_by_type', 'Number of invocations by type',
labels={'item_type': lambda: request.view_args['type']})
def by_type(item_type):
pass # only the counter is collected, not the default metrics

@metrics.gauge('in_progress', 'Long running requests in progress')
def long_running():

@metrics.summary('requests_by_status', 'Request latencies by status',
labels={'status': lambda r: r.status_code})
@metrics.histogram('requests_by_status_and_path', 'Request latencies by status and path',
labels={'status': lambda r: r.status_code, 'path': lambda: request.path})
def echo_status(status):
return 'Status: %s' % status, status

Default metrics

The following metrics are exported by default (unless the
``export_defaults`` is set to ``False``).

- ``flask_http_request_duration_seconds`` (Histogram)
Labels: ``method``, ``path`` and ``status``.
Flask HTTP request duration in seconds for all Flask requests.
- ``flask_http_request_total`` (Counter)
Labels: ``method`` and ``status``. Total number of HTTP requests for
all Flask requests.
- ``flask_exporter_info`` (Gauge)
Information about the Prometheus Flask exporter itself (e.g.


By default, the metrics are exposed on the same Flask application on the
``/metrics`` endpoint and using the core Prometheus registry. If this
doesn't suit your needs, set the ``path`` argument to ``None`` and/or
the ``export_defaults`` argument to ``False`` plus change the
``registry`` argument if needed.

The ``register_endpoint`` allows exposing the metrics endpoint on a
specific path. It also allows passing in a Flask application to register
it on but defaults to the main one if not defined.

Similarly, the ``start_http_server`` allows exposing the endpoint on an
independent Flask application on a selected HTTP port. It also supports
overriding the endpoint's path and the HTTP listen address.


When defining labels for metrics on functions, the following values are
supported in the dictionary:

- A simple static value
- A no-argument callable
- A single argument callable that will receive the Flask response as
the argument

Label values are evaluated within the request context.

Application information

The ```` method provides a way to expose
information as a ``Gauge`` metric, the application version for example.

The metric is returned from the method to allow changing its value from
the default ``1``:

.. code:: python

metrics = PrometheusMetrics(app)
info ='dynamic_info', 'Something dynamic')



.. |Travis| image::
.. |PyPI| image::
.. |PyPI| image::
.. |Coverage Status| image::
.. |Code Climate| image::

Release History

This version
History Node


History Node


History Node


History Node


History Node


History Node


History Node


History Node


History Node


History Node


History Node


Download Files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, Size & Hash SHA256 Hash Help File Type Python Version Upload Date
(6.6 kB) Copy SHA256 Hash SHA256
Source None Dec 29, 2017

Supported By

Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Google Google Cloud Servers DreamHost DreamHost Log Hosting