Lab 6 of 8 · Monitoring and observability
Add metrics, Prometheus and a Grafana dashboard
Count requests in your app, collect the numbers with Prometheus and graph them in Grafana, all with Docker Compose.
- Time
- 60–90 minutes
- Cost
- Free: everything runs in Docker on your computer.
What you learn
You cannot run what you cannot see. Metrics show how a system behaves over time, dashboards make them readable, and alerts wake someone up only when it matters.
- Metrics, logs and traces: the three signals
- Exposing Prometheus metrics from an app
- Querying with PromQL and building a Grafana dashboard
- What makes a good alert
In the AI era: AI apps need extra signals: response time, cost per request and how often answers fail checks. You add the same kind of counters for them that you add here.
Before you start
Finish lab 2. Docker Compose comes with Docker Desktop: check with
docker compose version.
Step by step
1 Add the metrics library
Add this line to the end of
requirements.txt, then install it.echo "prometheus-client==0.21.1" >> requirements.txt pip install -r requirements.txt2 Count requests in the app
Replace
app/main.pywith this version. Every request adds 1 to a counter labelled with its path, and/metrics/shows the numbers in Prometheus format.from fastapi import FastAPI from prometheus_client import Counter, make_asgi_app app = FastAPI(title="AI Status App") app.mount("/metrics", make_asgi_app()) REQUESTS = Counter("app_requests_total", "Requests handled by the app", ["path"]) @app.get("/health") def health() -> dict[str, str]: REQUESTS.labels(path="/health").inc() return {"status": "ok"} @app.get("/") def home() -> dict[str, str]: REQUESTS.labels(path="/").inc() return {"message": "Hello from my DevOps project"}3 Tell Prometheus where to look
Prometheus scrapes the app every 5 seconds. Inside Compose, the app is reachable by its service name,
app.global: scrape_interval: 5s scrape_configs: - job_name: ai-status-app metrics_path: /metrics/ static_configs: - targets: ["app:8000"]4 Run the app, Prometheus and Grafana together
Compose starts all three containers on one private network.
services: app: build: . ports: - "8000:8000" prometheus: image: prom/prometheus:v3.0.1 volumes: - ./monitoring/prometheus.yml:/etc/prometheus/prometheus.yml:ro ports: - "9090:9090" grafana: image: grafana/grafana:11.4.0 ports: - "3000:3000"5 Start it and send some traffic
The loop sends 50 requests so there is something to graph.
docker compose up -d --build for i in $(seq 1 50); do curl -s http://localhost:8000/health > /dev/null; done curl -s http://localhost:8000/metrics/ | grep app_requests_totalYou should see:A line like
app_requests_total{path="/health"} 50.0.6 Query Prometheus
Open http://localhost:9090, go to Status → Targets (the app should be UP), then run this query in the Query tab. It shows requests per second over the last minute.
rate(app_requests_total[1m])7 Build a Grafana dashboard
Open http://localhost:3000 and sign in with
admin/admin(choose a new password when asked; it only exists on your computer). Go to Connections → Data sources → Add data source → Prometheus, set the URL tohttp://prometheus:9090, and click Save & test. Then Dashboards → New → Add visualization, pick Prometheus, paste the query from the last step and save the dashboard as "AI Status App".You should see:A line graph that rises when you rerun the traffic loop.
8 Write down an alert rule
Good alerts describe a user problem, not a CPU number. Save this as a starting point; in real teams it is loaded into Prometheus Alertmanager or Grafana alerting.
groups: - name: ai-status-app rules: - alert: AppDown expr: up{job="ai-status-app"} == 0 for: 1m labels: severity: page annotations: summary: "AI Status App is not answering"9 Commit and stop everything
Take a screenshot of your dashboard and add it to the README: it is great proof for a recruiter.
git add app requirements.txt monitoring docker-compose.yml git commit -m "Add Prometheus metrics and Grafana monitoring" git push docker compose down
You are done when
Prometheus shows your app target as UP.
Your Grafana dashboard graphs
rate(app_requests_total[1m]).Your test from lab 1 still passes (
python -m pytest -q).
We check your public GitHub repository and give you a page to share with recruiters, and can add the project to your resume.