Overview
The monitoring stack provides:- Prometheus: Time-series metrics collection and storage
- Grafana: Dashboard visualization and alerting
- Redis Exporter: Redis-specific metrics
- PostgreSQL Exporter: Database performance metrics
- django-prometheus: Django and application metrics
Architecture
Prerequisites
Before setting up monitoring, ensure you have:- FKApi running (see Setup Guide)
- Docker and docker-compose (for containerized deployment)
- Redis running (for cache metrics)
- PostgreSQL running (for database metrics)
Installation
Docker Compose Setup
FKApi includes monitoring services indocker-compose.yml using the monitoring profile.
Start Monitoring Stack
Monitoring Services
The docker-compose configuration includes: PrometheusPrometheus Configuration
Createprometheus/prometheus.yml:
If using systemd PostgreSQL instead of Docker, change the postgres target to
host.docker.internal:9187 or your server IP.Custom Metrics
FKApi includes custom metrics defined incore/metrics.py:
User Collection Metrics
Cache Metrics
Celery Task Metrics
API Endpoint Metrics
Using Custom Metrics
Instrument your code with metrics:Grafana Setup
Install Grafana
- Docker
- Linux (systemd)
- macOS
Add to
docker-compose.yml:Add Prometheus Data Source
Create Dashboard
FKApi includes a pre-built Grafana dashboard. Import it: The dashboard includes:- Request Rate: Requests per second by endpoint
- Response Time: P50, P95, P99 latencies
- Error Rate: 4xx and 5xx error rates
- Cache Hit Rate: Cache effectiveness
- Database Performance: Query counts and durations
- Celery Tasks: Task success/failure rates
- Redis Metrics: Memory usage, operations/sec
- System Resources: CPU, memory, disk usage
Monitoring Best Practices
Metric Selection
Metric Selection
- Focus on user-facing metrics (latency, errors)
- Track resource utilization (CPU, memory, disk)
- Monitor cache hit rates
- Track task success/failure rates
- Measure database query performance
Alerting
Alerting
- Set up alerts for high error rates
- Alert on high latency (P95 > threshold)
- Monitor disk space usage
- Alert on cache connection failures
- Track task queue backlogs
Dashboard Design
Dashboard Design
- Group related metrics together
- Use appropriate time ranges
- Include both current and historical views
- Add annotations for deployments
- Use variables for filtering
Data Retention
Data Retention
- Configure Prometheus retention period
- Archive historical data if needed
- Monitor Prometheus storage size
- Consider using remote storage for long-term data
Troubleshooting
Metrics Endpoint Not Found
Error: 404 at/metrics
Solution:
- Verify
django_prometheusis installed:pip list | grep django-prometheus - Check
INSTALLED_APPSincludes'django_prometheus' - Verify URL configuration includes
path('', include('django_prometheus.urls')) - Restart Django server
Prometheus Not Scraping
Error: No data in Prometheus UI Solution:- Check Prometheus targets: http://localhost:9090/targets
- Verify all targets show as “UP”
- Check firewall rules allow access
- Verify service names in
prometheus.ymlmatch docker-compose - Check Prometheus logs:
docker compose logs prometheus
Grafana Connection Failed
Error: Cannot connect to data source Solution:- Verify Prometheus is running:
docker compose ps prometheus - Check Prometheus URL in Grafana (use service name for Docker)
- Test Prometheus UI: http://localhost:9090
- Check network connectivity between containers
Missing Metrics
Error: Some metrics not appearing Solution:- Verify exporters are running:
docker compose ps - Check exporter logs for errors
- Test exporter endpoints directly:
- Redis: http://localhost:9121/metrics
- PostgreSQL: http://localhost:9187/metrics
- Verify Prometheus scrape configuration
Accessing Monitoring Tools
Once everything is running:-
Prometheus UI: http://localhost:9090
- Query metrics
- View targets status
- Check service health
-
Grafana: http://localhost:3000
- View dashboards
- Create alerts
- Explore metrics
-
Flower (Celery): http://localhost:5555
- Monitor Celery tasks
- View worker status
- Track task history
-
Metrics Endpoint: http://localhost:8000/metrics
- Raw Prometheus metrics
- Verify instrumentation
Next Steps
Caching Strategy
Optimize performance with Redis caching
Celery Setup
Configure async tasks and monitoring