Skip to main content

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
Basic architecture:

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.
Grafana can display:
  • Counters
  • Graphs
  • Request rates
  • Error rates
  • Latency
  • System metrics

4. Project Structure


5. Install Python Dependencies

requirements.txt

Install:

6. Create FastAPI Metrics Application

metrics.py


7. Understanding the Counter

Creates a Prometheus counter called:
Every request to / increases the counter:
Example:

8. Run FastAPI

Start the application:
FastAPI runs at:
Test the application:
Test the metrics:
The metrics page should contain something similar to:

9. Prometheus Configuration

prometheus.yml

Important

When Prometheus runs inside Docker and FastAPI runs directly on Windows:
is used to allow the Prometheus container to access the host machine. Do not use:
for this setup because localhost inside the Prometheus container refers to the container itself.

10. Run Prometheus with Docker

Remove an existing Prometheus container if necessary:
From the Day 47 - Monitoring directory, run:

Command explanation

Prevents Git Bash on Windows from incorrectly converting Linux-style Docker paths.
Starts the container in detached mode.
Names the container prometheus.
Maps the Prometheus container port to the host.
Mounts the local configuration file into the Prometheus container.
Uses the official Prometheus Docker image.

11. Check Prometheus Container

Run:
Expected:
Check logs:

12. Open Prometheus

Open:
Prometheus UI should appear.

13. Check Prometheus Targets

Open:
Two targets should appear:

UP

Prometheus successfully connected to the target.

DOWN

Prometheus could not scrape the target.

14. Test the FastAPI Target

If fastapi is UP, Prometheus is successfully accessing:
The architecture is:

15. Query Metrics in Prometheus

Open:
Enter:
Click Execute. Example result:

16. Generate Requests

Open:
Refresh the page several times. Then run:
again in Prometheus. The value should increase.

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:
Run Grafana:
Check:
You should see:

19. Open Grafana

Open:
The Grafana login page will appear. For a fresh local Grafana installation, the initial credentials are commonly:
Grafana may ask for a new password after login.

20. Connect Grafana to Prometheus

In Grafana:
If both Prometheus and Grafana are Docker containers on the same Docker network, use:
Click:

21. Create a Grafana Dashboard

Go to:
Select:
Use this query:
Select a Stat visualization. This displays the total number of API requests.

22. Requests Per Second Panel

Create another visualization. Use:
Select:
This displays the request rate over time.

23. Example Dashboard

A basic monitoring dashboard can contain:

24. Additional Metrics

Monitoring can be extended beyond request counts.

Error Counter

Increment when an error occurs:
Query:

25. Gauge

A Gauge represents a value that can increase or decrease.
Set value:
Increase:
Decrease:

26. Histogram

A Histogram is useful for measuring request latency.
Measure execution time:
This is useful for monitoring:
  • API response time
  • Model inference time
  • Database operations

27. Labels

Labels provide additional dimensions for metrics.
Use:
The resulting metric can look like:

28. Monitoring an ML Application

For AI/ML applications, useful custom metrics include:
Example:
After a prediction:

29. Complete Monitoring Architecture


30. Complete Setup Commands

Terminal 1: FastAPI

Terminal 2: Prometheus

From the same directory:
Then:

Terminal 3: Grafana

Then:

31. Verification Checklist

FastAPI

Should return:

Metrics

Should contain:

Prometheus

Query:

Prometheus Targets

Expected:

Grafana

Prometheus data source:
Dashboard query:

32. Key Concepts


Final Flow

This gives the complete Day 47 Monitoring setup, including the Python application, Prometheus configuration, Docker commands for Prometheus and Grafana, PromQL queries, and dashboard creation.