The idea
Monitoring means collecting numbers about a running service, called metrics, every few seconds. A tool stores them over time, draws graphs and wakes a person when a number crosses a line. Here Prometheus watches a shop's web app.
Collect: count and scrape
The app keeps its own numbers. Lines 1 to 3 use the prometheus_client library to make a counter: a metric that only goes up, and resets to zero when the app restarts. Its label, code, splits it by HTTP status, such as 200 or 500.
Line 4 starts a small web server on port 8000. Its /metrics page lists every metric as plain text, one line per label value. Each failed request runs line 5, which adds one: the 500 line now reads 120.
Prometheus fetches the numbers itself. Every 15 seconds it requests /metrics and reads the reply; this is called a scrape. If a scrape fails, Prometheus sets a metric called up to 0 for that app, which can raise an alert too.
Store and query
Each scrape saves one sample, a time and a value. The samples for one metric name with one set of labels form a time series. After three scrapes, the 500 series holds 120, 123 and 126.
A counter's total means little; how fast it climbs matters. From 123 to 126 in 15 seconds is 3 ÷ 15 = 0.2 errors per second. PromQL, Prometheus's query language, works this out with rate().
Alert a person
A new release breaks checkout. Line 5 now runs far more often, and the next two scrapes read 171 and 216: up 45 every 15 seconds, or 3 errors per second, fifteen times the normal rate.
An alerting rule checks rate > 1 per second every 15 seconds. It is true now, so the alert turns pending. Its for: 1m waits a whole minute; the rate is still high, so it fires. The wait ignores short blips.
Prometheus sends the firing alert to Alertmanager. It groups related alerts, drops duplicates and routes them, here by paging the on-call engineer's phone. A person now looks, and can roll the bad release back.
What to watch
Google's SRE book names four golden signals to watch first. Latency, how long requests take, is usually a histogram. Traffic and errors are counters, as here. Saturation, how full the system is, is a gauge: a number that goes up and down.
What happens
;; step 1/10 · watch
service → counts things in its code (metrics)
monitor → collects them every few seconds
→ stores them over time
→ graphs them, checks rules
person ← woken only when a rule trips
The figure divides one 15-second rise by 15 seconds; that is what irate() returns. rate() averages over its whole window, extrapolates to the window edges and handles counter resets, so on real data it gives about the same number, not exactly it. Alerts should use rate(). The sample values are an invented example. Real services usually alert on the share of requests that fail (errors divided by all requests), not on a raw count, so the threshold still makes sense as traffic grows.