This example demonstrates how to set up monitoring for a Django application using Prometheus and Grafana. You can use this as a template for implementing monitoring in your own Django projects.
-
Django Application:
- Ensure your Django application is ready for monitoring.
- Install the necessary Python packages:
pip install prometheus-client django-prometheus - Configure your Django application as mentioned in the previous instructions.
-
Prometheus:
- Create a
prometheus.ymlconfiguration file to specify scrape targets and other settings. Make sure it's correctly formatted. - Build the Prometheus Docker image:
docker build -t my-prometheus ./prometheus - Start the Prometheus container:
docker run -d -p 9090:9090 --name my-prometheus my-prometheus
- Create a
-
Grafana:
- Create dashboard and data source configurations (e.g.,
my-dashboard.jsonandmy-datasource.yml) in thegrafana/directory. - Build the Grafana Docker image:
docker build -t my-grafana ./grafana - Start the Grafana container:
docker run -d -p 3000:3000 --name my-grafana my-grafana
- Create dashboard and data source configurations (e.g.,
-
Docker Compose:
- Execute all three containers using the
docker-compose.yml. - Start the server:
docker-compose build- Run the applications:
docker-compose up- Stopping the applications:
docker-compose down - Execute all three containers using the
-
Access Metrics:
- Access the Django application at
http://localhost:8080/exampleor to see the data being send to prometheus athttp://localhost:8080/metrics. - Access the Prometheus metrics at
http://localhost:9090/metrics. - Access Grafana at
http://localhost:3000and configure it to use Prometheus as the data source. Import the dashboard using themy-dashboard.jsonfile.
- Access the Django application at
- Here is a sample of them running:
- Grafana
http:locahost:3000/dashboards/prometheus-2-0-stats
- Django http:localhost:8080/example
- Django http:localhost:8080/metrics
- Prometheus http:localhost:9090/metrics

- If you encounter any issues, refer to the troubleshooting steps in the provided documentation.
- Check the container logs for Prometheus and Grafana using
docker logs my-prometheusanddocker logs my-grafana, respectively.
This example provides a basic setup for monitoring a Django application. Customize the configurations and dashboard as needed for your specific use case.
Feel free to expand on this README with additional project-specific instructions or details.