Exercises — Module 10: Monitoring, Logging & Observability
Environment: Docker Compose for the core labs (fast, free); your kind cluster for the Kubernetes edition. You'll instrument YOUR app from Modules 5–7 — monitoring someone else's demo teaches half as much. Keep Grafana open on a second screen all module; the goal is that graphs become how you see your systems.
Part A — Warm-Up Drills
Drill 1 — First scrape. Compose file: prometheus + node_exporter. Open Prometheus UI (:9090) → Status→Targets: both UP? Query up — read every label. Stop node_exporter; watch up flip to 0 within a scrape interval.
✓ You can explain how the pull model detected death with no extra code.
Drill 2 — PromQL finger exercises (in the Prometheus UI, 20 min daily until fluent).
node_cpu_seconds_total— raw counter; why is the graph a ramp?rate(node_cpu_seconds_total{mode="idle"}[1m])— why is rate() mandatory for counters?node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes— gauge math.100 - (avg by(instance)(rate(node_cpu_seconds_total{mode="idle"}[1m])) * 100)— build CPU% and explain each layer. ✓ You can build a query inside-out and narrate it.
Drill 3 — Instrument your app (the highest-value 30 minutes). Add prometheus-client (or equivalent) to your Flask app: a request Counter (labels: path, status) and a duration Histogram; expose /metrics. Curl /metrics and READ the raw text format — find your counter, the histogram buckets, _sum and _count.
✓ You understand the exposition format before any dashboard abstracts it.
Drill 4 — Load + watch. Scrape your app; run ab -n 5000 -c 20; in Prometheus graph rate(http_requests_total[1m]) and histogram_quantile(0.95, sum(rate(http_request_duration_seconds_bucket[1m])) by (le)) live during the load.
✓ You watched p95 move under load — latency is now a graph, not a vibe.
Drill 5 — Log line discipline. Convert your app to JSON logs (one line per request: ts, level, path, status, duration_ms). Curl a few times, docker logs, and pipe through jq to filter status>=500.
✓ Structured logs queryable with jq — Loki will feel natural now.
Part B — Guided Labs
Lab 1 — The Full Local Stack (2 h)
Compose: your app + postgres + prometheus + node_exporter + grafana + loki + promtail.
- Wire promtail to Docker logs (or a mounted log file); Loki as a Grafana datasource; Prometheus too.
- Grafana: import dashboard 1860 (Node Exporter Full) — spend 15 minutes just reading it: find load, memory pressure, disk I/O, network.
- Build YOUR service dashboard from scratch: panels for request rate, error %, p50/p95/p99, plus CPU/mem of the host. Save the JSON into your Git repo.
✓
docker compose upfrom cold gives you working dashboards — reproducible observability.
Lab 2 — Alert Pipeline End-to-End (90 min)
- Add alertmanager to the stack; route to a Slack/Discord webhook.
- Rules: (a)
up == 0for 1m, severity=page; (b) error-rate > 5% for 2m; (c) disk predict_linear < 0 in 4h, severity=warn. - Fire each ON PURPOSE: stop the app (a); add a
/boomendpoint returning 500 and hammer it (b); for (c), reason through why it's hard to trigger quickly and test the expression manually instead. - Time alert (a): from
docker stopto Slack ping. Explain every stage of the delay (scrape interval +for:+ group_wait). ✓ You can draw the metric→rule→alertmanager→webhook pipeline with real latencies on each arrow. - Silence an alert in the Alertmanager UI during "maintenance"; watch it suppress; expire it.
Lab 3 — SLOs on Your Own App (60 min)
- Write two SLOs for your app: availability (non-5xx ratio) 99.5%/30d; latency (p95 < 300ms) 99%/30d.
- Express each SLI as a PromQL recording rule (
record:in a rules file); confirm the new series exist. - Compute your current error budget spend after deliberately serving some 500s. How many bad requests until the monthly budget dies at your traffic rate? Do the arithmetic on paper. ✓ SLO/error budget stops being vocabulary and becomes a number you calculated.
Lab 4 — Loki Deep Dive (60 min)
- LogQL reps in Grafana Explore: all app logs; only errors (
|= "error"and| json | status >= 500— note the difference); rate of errors as a graph;line_formatto prettify. - Metric-from-logs: create an alert on
sum(rate({app="api"} | json | status >= 500 [5m])) > 1— fire it via/boom. - Second source: point promtail at a host file (
/var/log/syslogor an nginx log) with different labels; query both sources in one Explore session. ✓ One query bar over containers AND host files — centralized logging achieved. - The correlation move: put a request_id in both an app log line and a response header; find one request's log by id in under 15 seconds.
Lab 5 — Kubernetes Edition (2 h)
- On kind:
helm installkube-prometheus-stack (note everything it brings: operator, CRDs, node-exporter DaemonSet, kubelet scraping, Grafana with dashboards preloaded) and loki-stack (promtail DaemonSet). - Tour the preloaded dashboards: find your namespace's pods, their CPU vs requests/limits, cluster capacity.
- Deploy your app (Module 7 Helm chart) with a ServiceMonitor so the operator scrapes it automatically — verify the target appears in Prometheus with zero prometheus.yml editing. Explain what the CRD+operator did for you.
- Rerun the HPA load test from Module 7 — but this time watch it on Grafana: request rate up → CPU up → replicas up → p95 stabilizes. Screenshot the four-panel story. ✓ That screenshot is your observability portfolio piece: autoscaling explained by data.
Lab 6 — Incident Simulation: the Full Loop (90 min) — the module's boss fight
Set up, then have a friend (or a dice roll) pick ONE fault while you're not looking:
- kill the DB / fill its disk / deploy the
/boomimage as "v2" / drop an index and hammer a slow query / set app memory limit too low. Run the incident like it's real:
- Alert fires → acknowledge (note the time).
- Dashboard first: what changed and WHEN? Correlate with deploys/events.
- Logs second: narrow with LogQL to the failing component.
- Mitigate (rollback/restart/scale) → watch recovery on the dashboard → note time-to-recovery.
- Write a half-page blameless postmortem: timeline, root cause, what monitoring caught/missed, one action item — and IMPLEMENT the action item (a new alert/panel). ✓ Repeat with a second fault. The postmortems go in your portfolio repo; the ritual goes in your muscle memory.
Part C — Challenges
C1 — Blackbox probing. Add blackbox_exporter; probe your app's public URL and one external site (HTTP 2xx + latency); alert on probe failure. Now you monitor from the USER's side, not just inside.
C2 — cAdvisor per-container metrics. Add cAdvisor to the compose stack; build a panel showing memory per container; find your app's baseline vs under load; set a sensible memory limit from data, not guesswork.
C3 — Postgres exporter. Add postgres_exporter; graph connections, transactions/s, cache hit ratio; alert on connections > 80% of max_connections (Module 12 foreshadowing).
C4 — The dashboard-as-code loop. Export your service dashboard JSON, provision it via Grafana's provisioning directory in compose, delete it in the UI, restart — it returns. Observability config now lives in Git like everything else.
C5 — Alert quality audit. Review every alert you've built against three questions: is it a symptom or a cause? is it actionable at 3 AM? does it link a runbook? Rewrite one that fails, delete one that's noise, and write a runbook stub for your top alert.
C6 — OTel taste (stretch). Add OpenTelemetry auto-instrumentation + Tempo (or Jaeger) to the stack; make your app call a second tiny service; find one trace showing both spans; click from trace → logs via a shared trace_id.
Part D — Break & Fix Scenarios
D1 — The silent monitor. Prometheus itself is down (stop it). How long until you notice, and what told you? Add the fix: a dead-man's-switch style always-firing alert (or an external probe on Prometheus) so silence itself alerts.
D2 — Cardinality bomb (contained!). Add a label with unbounded values (e.g., full request path with IDs, or user_id) to your counter; hammer varied URLs; watch series count explode (prometheus_tsdb_head_series). Revert, and write the labeling rule you'll follow forever (labels = low-cardinality dimensions only).
D3 — The lying dashboard. Build a latency panel using avg() while p99 is awful (make /slow hit 5% of requests with a 2s sleep). Show average looking fine while users suffer; replace with percentiles. Keep both screenshots — it's the best "why percentiles" artifact you'll ever have.
D4 — Alert fatigue week. Set thresholds too tight (error > 0.1%, CPU > 50%) and live with the webhook noise for a day. Count the pings, note when you started ignoring them, then tune with for: durations and sane thresholds. Write the two-sentence lesson.
D5 — Log flood. Set the app to debug-log every request body under load; watch disk/Loki ingest climb; fix with log levels + promtail drop stages, and note the cost dimension of logging.
Part E — Interview Rehearsal (out loud)
- Metrics vs logs vs traces — and the order you use them in an incident (tell Lab 6's story).
- Define SLI/SLO/SLA and error budget with YOUR app's numbers from Lab 3.
- Why percentiles over averages? (Show the D3 story.)
- How does Prometheus find out a target died? Counter vs gauge vs histogram?
- Write, on the whiteboard, the PromQL for error ratio and p95.
- What makes a good alert? What's alert fatigue and how did you fix it? (D4.)
- How would you monitor a new microservice from scratch — first four things you'd add?
- What is Loki's design trade-off vs Elasticsearch?
Done when
the Lab 5 four-panel screenshot exists, two Lab 6 postmortems are written, D3's average-vs-p99 evidence is saved, and you can whiteboard E5 from memory.
