AlgoViz

Prerequisites of a monitoring system

easy 28 min

A 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

Service 1CPU 45%, 120/sService 2CPU 45%, 120/sService 3CPU 45%, 120/sPrometheusSample storeGrafanaAlertmanagerOn call

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

Service 1CPU 45%PrometheusinactiveAlertmanagerquietOn call
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

    critical

    Every user makes new series until memory is full.

    Wrong
    requests.inc(user_id=uid)
    Right
    requests.inc(route="/orders/:id")
  • Sum before rate

    major

    The restart of one server hides in the sum.

    Wrong
    rate(sum(requests_total))
    Right
    sum(rate(requests_total[5m]))
  • Average of durations

    major

    Slow requests disappear.

    Wrong
    avg(duration)
    Right
    histogram_quantile(0.99,  sum by (le) (rate(duration_bucket[5m])))
  • A share as a gauge

    minor

    Shares of two servers cannot be joined without their counts.

    Wrong
    hit_ratio.set(hits / total)
    Right
    hits.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.