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 <https://prometheus.io/>`__. It can also track method
invocations using convenient functions.
Usage
-----
.. code:: python
from flask import Flask, request
from prometheus_flask_exporter import PrometheusMetrics
app = Flask(__name__)
metrics = PrometheusMetrics(app)
# static information as metric
metrics.info('app_info', 'Application info', version='1.0.3')
@app.route('/')
def main():
pass # requests tracked by default
@app.route('/skip')
@metrics.do_not_track()
def skip():
pass # default metrics are not collected
@app.route('/<item_type>')
@metrics.do_not_track()
@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
@app.route('/long-running')
@metrics.gauge('in_progress', 'Long running requests in progress')
def long_running():
pass
@app.route('/status/<int:status>')
@metrics.do_not_track()
@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
Labels
~~~~~~
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 ``PrometheusMetrics.info(..)`` 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 = metrics.info('dynamic_info', 'Something dynamic')
...
info.set(42.1)
License
-------
MIT
.. |Travis| image:: https://img.shields.io/travis/rycus86/prometheus_flask_exporter.svg
:target: https://travis-ci.org/rycus86/prometheus_flask_exporter
.. |PyPI| image:: https://img.shields.io/pypi/v/prometheus-flask-exporter.svg
:target: https://pypi.python.org/pypi/prometheus-flask-exporter
.. |PyPI| image:: https://img.shields.io/pypi/pyversions/prometheus-flask-exporter.svg
:target: https://pypi.python.org/pypi/prometheus-flask-exporter
.. |Coverage Status| image:: https://coveralls.io/repos/github/rycus86/prometheus_flask_exporter/badge.svg?branch=master
:target: https://coveralls.io/github/rycus86/prometheus_flask_exporter?branch=master
.. |Code Climate| image:: https://codeclimate.com/github/rycus86/prometheus_flask_exporter/badges/gpa.svg
:target: https://codeclimate.com/github/rycus86/prometheus_flask_exporter
=========================
|Travis| |PyPI| |PyPI| |Coverage Status| |Code Climate|
This library provides HTTP request metrics to export into
`Prometheus <https://prometheus.io/>`__. It can also track method
invocations using convenient functions.
Usage
-----
.. code:: python
from flask import Flask, request
from prometheus_flask_exporter import PrometheusMetrics
app = Flask(__name__)
metrics = PrometheusMetrics(app)
# static information as metric
metrics.info('app_info', 'Application info', version='1.0.3')
@app.route('/')
def main():
pass # requests tracked by default
@app.route('/skip')
@metrics.do_not_track()
def skip():
pass # default metrics are not collected
@app.route('/<item_type>')
@metrics.do_not_track()
@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
@app.route('/long-running')
@metrics.gauge('in_progress', 'Long running requests in progress')
def long_running():
pass
@app.route('/status/<int:status>')
@metrics.do_not_track()
@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
Labels
~~~~~~
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 ``PrometheusMetrics.info(..)`` 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 = metrics.info('dynamic_info', 'Something dynamic')
...
info.set(42.1)
License
-------
MIT
.. |Travis| image:: https://img.shields.io/travis/rycus86/prometheus_flask_exporter.svg
:target: https://travis-ci.org/rycus86/prometheus_flask_exporter
.. |PyPI| image:: https://img.shields.io/pypi/v/prometheus-flask-exporter.svg
:target: https://pypi.python.org/pypi/prometheus-flask-exporter
.. |PyPI| image:: https://img.shields.io/pypi/pyversions/prometheus-flask-exporter.svg
:target: https://pypi.python.org/pypi/prometheus-flask-exporter
.. |Coverage Status| image:: https://coveralls.io/repos/github/rycus86/prometheus_flask_exporter/badge.svg?branch=master
:target: https://coveralls.io/github/rycus86/prometheus_flask_exporter?branch=master
.. |Code Climate| image:: https://codeclimate.com/github/rycus86/prometheus_flask_exporter/badges/gpa.svg
:target: https://codeclimate.com/github/rycus86/prometheus_flask_exporter
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file prometheus_flask_exporter-0.0.5.tar.gz.
File metadata
- Download URL: prometheus_flask_exporter-0.0.5.tar.gz
- Upload date:
- Size: 5.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f4814a6816e2c34b65a2face38f705b0cdc51c62bb35d45b97190ac93fb1f630
|
|
| MD5 |
5a405469e3f268e6a2070626dc791970
|
|
| BLAKE2b-256 |
2fd8391411ca50d312f805dbd65a9e9f3994c6fbd5d285a3b575773dffad9629
|