Skip to main content
This guide covers setting up a complete monitoring stack for FKApi using Prometheus for metrics collection and Grafana for visualization.

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 in docker-compose.yml using the monitoring profile.

Start Monitoring Stack

Monitoring Services

The docker-compose configuration includes: Prometheus
Redis Exporter
PostgreSQL Exporter

Prometheus Configuration

Create prometheus/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 in core/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

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

  • 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
  • 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
  • Group related metrics together
  • Use appropriate time ranges
  • Include both current and historical views
  • Add annotations for deployments
  • Use variables for filtering
  • 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:
  1. Verify django_prometheus is installed: pip list | grep django-prometheus
  2. Check INSTALLED_APPS includes 'django_prometheus'
  3. Verify URL configuration includes path('', include('django_prometheus.urls'))
  4. Restart Django server

Prometheus Not Scraping

Error: No data in Prometheus UI Solution:
  1. Check Prometheus targets: http://localhost:9090/targets
  2. Verify all targets show as “UP”
  3. Check firewall rules allow access
  4. Verify service names in prometheus.yml match docker-compose
  5. Check Prometheus logs: docker compose logs prometheus

Grafana Connection Failed

Error: Cannot connect to data source Solution:
  1. Verify Prometheus is running: docker compose ps prometheus
  2. Check Prometheus URL in Grafana (use service name for Docker)
  3. Test Prometheus UI: http://localhost:9090
  4. Check network connectivity between containers

Missing Metrics

Error: Some metrics not appearing Solution:
  1. Verify exporters are running: docker compose ps
  2. Check exporter logs for errors
  3. Test exporter endpoints directly:
  4. Verify Prometheus scrape configuration

Accessing Monitoring Tools

Once everything is running:

Next Steps

Caching Strategy

Optimize performance with Redis caching

Celery Setup

Configure async tasks and monitoring