Prerequisites of a monitoring system
easy 28 minA monitoring system collects numbers from every program and machine, stores them with a time, answers questions about them and calls a person when something is wrong.
See it run
From a number in a program to a message on a phone: what is counted, how it is fetched, and what it costs.
Operations
The stack
Six steps with Prometheus, Alertmanager and Grafana.
Step 1 of 6
Services show numbers
Numbers wait to be fetched
Each has a page with counters, gauges and histograms: CPU 45%, 120 requests a second.
Step 1: Services show numbers
page = service.get("/metrics")store.append(now, parse(page))value = query("rate(http_requests_total[5m])")if rule(value) true for 10 minutes: alertmanager.send(alert)grafana.draw(value)- Tells you
- Is it working?
- Costs
- A fetch every 15 s
- Waiting for users to write
- minutes to hours
Rules run in Prometheus. Alertmanager decides who hears about it, and how often.
In the real world
The same prerequisites of a monitoring system doing real jobs. Pick one and step through it.
The rule: CPU above 80% for ten minutes.
Step 1 of 4
Inactive
- CPU
- 45%
Nothing to do
45%. The rule is false.
Step 1: Inactive
Common mistakes
The bugs interviewers watch for, with the fix beside each.
User id as a label
criticalEvery user makes new series until memory is full.
Wrongrequests.inc(user_id=uid)Rightrequests.inc(route="/orders/:id")Sum before rate
majorThe restart of one server hides in the sum.
Wrongrate(sum(requests_total))Rightsum(rate(requests_total[5m]))Average of durations
majorSlow requests disappear.
Wrongavg(duration)Righthistogram_quantile(0.99, sum by (le) (rate(duration_bucket[5m])))A share as a gauge
minorShares of two servers cannot be joined without their counts.
Wronghit_ratio.set(hits / total)Righthits.inc()misses.inc()
Interview tips
Do
- One naming rule, units in names.
- Labels with meaning and few values.
- Histograms for durations.
- Alerts on promises to users.
- A description for each metric.
Do not
- Labels without limit.
- Metrics nobody looks at.
- Alerts for every small change.
- Forgetting retention.
- A public metrics page.
METRICS
- Measure and Expose.
- Time series and Rules.
- Insights and Charts.
- SLOs: the targets.
Numbers
- 4 kinds of metric.
- 15 s is a common interval, 1 min the default.
- 1 to 2 bytes per sample.
- 100,000 series: 1.15 GB a day.