AlgoViz

Prometheus

medium 28 min

Prometheus is an open source monitoring server.

See it run

A scrape, a rate, a label explosion, an alert, and the way of a sample to disk.

Operations

One scrape

Find the targets, read their pages, write down whether they answered.

Step 1 of 5

Who is there?

Our systemGET /metricsDiscovery2 targetsPrometheusApp copy 1App copy 2

Discovery lists two copies of the app.

Step 1: Who is there?

def scrape_round():    for t in discovery.targets():        try:            page = http_get(t + "/metrics", timeout=10)            store(parse(page), labels=t.labels)            store("up", 1, labels=t.labels)        except Timeout:            store("up", 0, labels=t.labels)
How often
Every 15 s
What it costs
1 to 2 B a sample

A failed scrape is a measurement too: alert on it.

In the real world

The same prometheus doing real jobs. Pick one and step through it.

Two identical servers that do not know each other.

Step 1 of 3

Both work

Our systemTargetsPrometheus APrometheus BAlertmanagerremoves doublesOn call

1 message

Both scrape everything and both send the same alert. One message arrives.

Step 1: Both work

Common mistakes

The bugs interviewers watch for, with the fix beside each.

  • A label without a limit

    critical

    Each value is a new series in memory. Users, ids and full URLs never stop growing.

    Wrong
    REQUESTS.labels(user=user_id).inc()
    Right
    REQUESTS.labels(tier=user.tier).inc()log.info("request", user=user_id)
  • Sum before rate

    major

    A restart of one copy is hidden in the sum and breaks the result.

    Wrong
    rate(sum_of_requests[5m])
    Right
    sum(rate(http_requests_total[5m]))
  • Averaged percentiles

    major

    The mean of 20 p95 values is not the p95 of all requests.

    Wrong
    avg(latency{quantile="0.95"})
    Right
    histogram_quantile(0.95,  sum by (le) (rate(latency_bucket[5m])))
  • Everything through the Pushgateway

    major

    Old series stay for ever, up says nothing about your servers, and the gateway is one more single point.

    Wrong
    # 50 servers push every 10 s
    Right
    # 50 servers are scraped# one nightly job pushes its result
  • Window as long as the interval

    minor

    With fewer than two samples there is no rate, so charts have holes.

    Wrong
    # scrape every 60srate(x_total[1m])
    Right
    # scrape every 60srate(x_total[5m])

Interview tips

  • How to explain it

    • Targets show numbers.
    • Prometheus fetches and stores them.
    • Rules decide, Alertmanager tells people.
  • Where state lives

    • Newest samples: memory and the log.
    • Older ones: blocks on one disk.
    • Silences: Alertmanager.
  • Reach for it when

    • You need rates, errors and durations.
    • Targets come and go.
    • You can run one small server.
  • Before production

    • Two servers.
    • A sample_limit on every job.
    • Something outside that notices silence.