Monitoring with Prometheus & Grafana
1. What is Monitoring?
Monitoring is the process of collecting and visualizing application metrics to understand application health, performance, and usage. Important metrics include:- API request count
- Request rate
- Error count
- Response latency
- CPU and memory usage
- Model inference time
- Application uptime
2. Prometheus
Prometheus is an open-source monitoring system that collects and stores metrics as time-series data. Prometheus periodically scrapes metrics from configured targets. Example:3. Grafana
Grafana is a visualization platform used to create dashboards from data collected by Prometheus.- Counters
- Graphs
- Request rates
- Error rates
- Latency
- System metrics
4. Project Structure
5. Install Python Dependencies
requirements.txt
6. Create FastAPI Metrics Application
metrics.py
7. Understanding the Counter
/ increases the counter:
8. Run FastAPI
Start the application:9. Prometheus Configuration
prometheus.yml
Important
When Prometheus runs inside Docker and FastAPI runs directly on Windows:localhost inside the Prometheus container refers to the container itself.
10. Run Prometheus with Docker
Remove an existing Prometheus container if necessary:Day 47 - Monitoring directory, run:
Command explanation
prometheus.
11. Check Prometheus Container
Run:12. Open Prometheus
Open:13. Check Prometheus Targets
Open:UP
Prometheus successfully connected to the target.
DOWN
Prometheus could not scrape the target.
14. Test the FastAPI Target
Iffastapi is UP, Prometheus is successfully accessing:
15. Query Metrics in Prometheus
Open:16. Generate Requests
Open:17. Useful PromQL Queries
Total requests
Requests per second
Requests during the last 5 minutes
18. Install Grafana with Docker
Remove an existing Grafana container if necessary:19. Open Grafana
Open:20. Connect Grafana to Prometheus
In Grafana:21. Create a Grafana Dashboard
Go to:22. Requests Per Second Panel
Create another visualization. Use:23. Example Dashboard
A basic monitoring dashboard can contain:24. Additional Metrics
Monitoring can be extended beyond request counts.Error Counter
25. Gauge
A Gauge represents a value that can increase or decrease.26. Histogram
A Histogram is useful for measuring request latency.- API response time
- Model inference time
- Database operations