Prometheus Flask Exporter

0.23.2 · active · verified Fri Apr 10

This library provides HTTP request metrics for Flask applications, allowing easy integration with Prometheus. It automatically collects default metrics like request duration and total counts, and enables defining custom metrics using decorators. The current version is 0.23.2, with new releases occurring periodically, often in response to updates in Flask or the underlying Prometheus client library.

Warnings

Install

Imports

Quickstart

Initializes a Flask application with Prometheus metrics. Default HTTP request metrics (duration, total, exceptions) are exposed on the '/metrics' endpoint. Custom metrics like 'in_progress' gauge can be added to specific routes using decorators. To view metrics, run the app and navigate to '/metrics' after making some requests.

from flask import Flask, request
from prometheus_flask_exporter import PrometheusMetrics
import os

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

# Optional: Add static info about the app
metrics.info('app_info', 'Application info', version='1.0.0')

@app.route('/')
def main():
    return 'Hello World!'

@app.route('/long-running')
@metrics.gauge('in_progress', 'Long running requests in progress')
def long_running():
    import time
    time.sleep(2)
    return 'Done!'

# Run the app directly or using a WSGI server like Gunicorn
if __name__ == '__main__':
    # Example: Access http://localhost:5000/ and http://localhost:5000/metrics
    app.run(host='0.0.0.0', port=5000)

view raw JSON →