{"id":2668,"library":"prometheus-flask-exporter","title":"Prometheus Flask Exporter","description":"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.","status":"active","version":"0.23.2","language":"en","source_language":"en","source_url":"https://github.com/rycus86/prometheus_flask_exporter","tags":["flask","prometheus","metrics","monitoring","exporter","observability"],"install":[{"cmd":"pip install prometheus-flask-exporter","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Core web framework being extended.","package":"Flask","optional":false},{"reason":"Underlying Prometheus client library for metric exposition.","package":"prometheus_client","optional":false}],"imports":[{"symbol":"PrometheusMetrics","correct":"from prometheus_flask_exporter import PrometheusMetrics"},{"symbol":"Flask","correct":"from flask import Flask"},{"note":"Required for custom label functions that inspect the request object.","symbol":"request","correct":"from flask import request"}],"quickstart":{"code":"from flask import Flask, request\nfrom prometheus_flask_exporter import PrometheusMetrics\nimport os\n\napp = Flask(__name__)\nmetrics = PrometheusMetrics(app)\n\n# Optional: Add static info about the app\nmetrics.info('app_info', 'Application info', version='1.0.0')\n\n@app.route('/')\ndef main():\n    return 'Hello World!'\n\n@app.route('/long-running')\n@metrics.gauge('in_progress', 'Long running requests in progress')\ndef long_running():\n    import time\n    time.sleep(2)\n    return 'Done!'\n\n# Run the app directly or using a WSGI server like Gunicorn\nif __name__ == '__main__':\n    # Example: Access http://localhost:5000/ and http://localhost:5000/metrics\n    app.run(host='0.0.0.0', port=5000)\n","lang":"python","description":"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."},"warnings":[{"fix":"Use the `group_by` argument instead, which offers more flexible grouping options. For example, `PrometheusMetrics(app, group_by='endpoint')`.","message":"The `group_by_endpoint` argument in `PrometheusMetrics` is deprecated.","severity":"deprecated","affected_versions":"<= 0.3.x"},{"fix":"Use the `default_labels` argument instead, which supports both static and dynamic label values.","message":"The `static_labels` argument in `PrometheusMetrics` is deprecated.","severity":"deprecated","affected_versions":"<= 0.14.x"},{"fix":"For accurate metrics during development with live-reloading, set the `DEBUG_METRICS` environment variable. For production, disable debug mode and use a WSGI server.","message":"Running Flask with `debug=True` (live-reloading) might not reflect metrics for the latest code unless the `DEBUG_METRICS` environment variable is set. Additionally, `PrometheusMetrics.start_http_server()` is not expected to work reliably in this debug scenario.","severity":"gotcha","affected_versions":"All versions >= 0.5.1"},{"fix":"Avoid using high-cardinality values (e.g., user IDs, full dynamic URL paths) as labels. Aggregate or generalize label values (e.g., `/user/{id}` instead of `/user/123`).","message":"Carefully manage metric label cardinality to avoid 'cardinality explosion', which can lead to excessive memory usage and Prometheus server crashes.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Always pin the `prometheus_client` dependency to a known compatible version in your `requirements.txt` or `pyproject.toml` to prevent unexpected breakage during upgrades. Ensure `prometheus-flask-exporter` is also kept updated to versions that explicitly support newer `prometheus_client` releases.","message":"Past versions experienced issues due to breaking changes in the underlying `prometheus_client` library's `exposition` module (`choose_encoder`).","severity":"breaking","affected_versions":"Around `prometheus_client` 0.14.0, specifically `prometheus-flask-exporter` versions that had unversioned `prometheus_client` dependencies."},{"fix":"Monitor memory usage closely in production. If a leak is suspected, review custom metric decorators, ensure proper resource cleanup, and consult `prometheus_flask_exporter` or `prometheus_client` issue trackers.","message":"Reports of memory leaks have been observed in specific pull-based Prometheus configurations when custom decorators for latency measurement are used.","severity":"gotcha","affected_versions":"Undetermined; reported in some production setups."}],"env_vars":null,"last_verified":"2026-04-10T00:00:00.000Z","next_check":"2026-07-09T00:00:00.000Z"}