Prometheus for Monitoring a Flask App, Redis, and PostgreSQL
Prometheus is a powerful toolkit for monitoring applications and infrastructure. This guide provides clear, step-by-step instructions to install, configure, and verify Prometheus for your Flask app, Redis, and PostgreSQL.
Prometheus is a powerful monitoring and alerting toolkit. Follow this step-by-step guide to install and configure Prometheus for your Flask application.
Setup For an easier setup, you can use Docker and Docker Compose. Create a folder and put these two files inside it: 1. docker-compose.yml
services:
prometheus:
image: prom/prometheus:latest
container_name: prometheus
volumes:
- ./prometheus.yml:/etc/prometheus/prometheus.yml
- prometheus_data:/prometheus
ports:
- "9090:9090"
networks:
- backend
- app_network # ✅ Attach to app_network
redis_exporter:
image: oliver006/redis_exporter
container_name: redis_exporter
restart: unless-stopped
ports:
- "9121:9121"
networks:
- backend
- app_network # ✅ Attach to app_network
postgres_exporter:
image: wrouesnel/postgres_exporter
container_name: postgres_exporter
restart: unless-stopped
ports:
- "9187:9187"
networks:
- backend
- app_network # ✅ Attach to app_network
volumes:
prometheus_data:
networks:
backend:
driver: bridge
app_network:
external: true # ✅ Use the existing app_network
2. prometheus.yml
global: scrape_interval: 15s # How often Prometheus scrapes metrics
scrape_configs:
job_name: "prometheus" static_configs:
targets: ["localhost:9090"]
job_name: "flask_app" static_configs:
targets: ["myApp:5000"] # Flask app inside
job_name: "redis" static_configs:
targets: ["redis_exporter:9121"]
job_name: "postgres" static_configs:
targets: ["postgres_exporter:9187"]
Note that you need to have
networks:
app_network
for all your Redis, PostgreSQL, and myApp services defined in their Docker Compose files.
Access the Prometheus Web UI You can view graphs and check data using the web UI: [link:http://localhost:9090|text:http://localhost:9090].
Optional: Monitor Redis and PostgreSQL Monitor Redis Run the Redis Exporter container:
docker run -d --name=redis_exporter -p 9121:9121 oliver006/redis_exporter
Update Prometheus config:
scrape_configs:
job_name: 'redis' static_configs:
targets: ['localhost:9121']
Monitor PostgreSQL Run the PostgreSQL Exporter container:
docker run -d --name=postgres_exporter -p 9187:9187 wrouesnel/postgres_exporter
Update Prometheus config:
scrape_configs:
job_name: 'postgres' static_configs:
targets: ['localhost:9187']
Integrate Prometheus with Your Flask App Use the following code to expose metrics in your Flask app:
import time
from flask import Flask, Response, g, request
from prometheus_client import Counter, Histogram, generate_latest
app = Flask(name)
Prometheus metrics
REQUEST_COUNT = Counter( 'flask_app_requests_total', 'Total number of requests', ['method', 'endpoint'] )
REQUEST_LATENCY = Histogram( 'flask_app_request_latency_seconds', 'Request latency in seconds', ['method', 'endpoint'] )
@app.before_request
def before_request():
g.start_time = time.time()
REQUEST_COUNT.labels(method=request.method, endpoint=request.path).inc()
@app.after_request
def after_request(response):
if hasattr(g, 'start_time'):
latency = time.time() - g.start_time
REQUEST_LATENCY.labels(method=request.method, endpoint=request.path).observe(latency)
return response
@app.route('/metrics')
def metrics():
return Response(generate_latest(), mimetype='text/plain')
@app.route('/')
def index():
return "Hello, World!"
if name == 'main':
app.run(host='0.0.0.0', port=5000)
Install the required package:
pip install prometheus_client
Reload Prometheus and Verify Apply the changes:
curl -X POST http://localhost:9090/-/reload
Check the Prometheus UI under Status > Targets to ensure your Flask app is being monitored.
Test the Configuration Generate traffic:
curl http://localhost:5000/
or
curl http://localhost:5000/some-endpoint
You can simulate higher traffic using:
ab -n 100 -c 10 http://localhost:5000/
Conclusion Now your Flask application is being monitored with Prometheus. You can extend this setup by adding Grafana for visualization! Make sure to adjust your configurations as needed to suit your environment.