At midnight your phone rings: notes-api's error rate is 7.8%. You open the laptop half awake, and the graph is already back to normal. It was only the containers restarting during a deploy. A week later your phone rings again. This time the database really is down, but for a moment you think it is the same blip (a brief spike).
What went wrong here is the alert rule, not the monitoring. Alerting is a set of rules that watch metrics and call a person when there is a problem that needs action. On-call is the duty roster: who gets called, when, and what they do. Picture the smoke alarm in a house. A good one does not sound every time the toast burns a little, but it always sounds when the smoke stays. For a real fire, the fire brigade has a duty roster and written steps for handling it. In this picture, the smoke alarm is the alert, the toast that burns a little is a blip, the smoke that stays is a real problem, the duty roster is the on-call rotation, and the written steps are the runbook. Monitoring without alerts is just a dashboard nobody looks at at 3 am.
In the Metrics & Monitoring lesson, Prometheus started collecting notes-api's metrics every 15 seconds, and the error rate (the percent of requests that fail) can be calculated from those metrics. Now we add alert rules that use them.
Anatomy of an alert rule
Prometheus alert rules are written in a YAML file. Prometheus evaluates them (checks their condition) every minute:
groups:
- name: notes-api
rules:
- alert: HighErrorRate
expr: |
sum(rate(http_requests_total{job="notes-api",status_code=~"5.."}[1m]))
/ sum(rate(http_requests_total{job="notes-api"}[1m])) * 100 > 5
for: 5m
labels:
severity: critical
annotations:
summary: "notes-api error rate above 5%"
runbook_url: "https://wiki.example.com/runbooks/notes-api-error-rate"
expr: 5xx requests (failed because of a server error) per second divided by all requests per second, times 100, so a percentage.status_code=~"5.."picks the statuses that start with 5, andsumadds up all routes. The rule is active while the result is above 5.for: 5m: the condition must hold for 5 minutes before the alert is really sent.labels.severity: how serious it is, used to decide where the alert goes.annotations: text for humans, including a link to the runbook (a page with the steps for handling the problem, covered below).
Prometheus only decides whether the alert is firing (sounding) or not. Sending the notification is the job of Alertmanager, a separate program that groups alerts, drops duplicates, and forwards them to the phone of the person on call or to Slack.
One rule, one value
The heart of every evaluation is comparing the current value with the threshold (the limit), using the rule's operator. This evaluateAlert function does that:
function evaluateAlert(rule, currentValue) {
let triggered = false;
if (rule.operator === ">") triggered = currentValue > rule.threshold;
if (rule.operator === "<") triggered = currentValue < rule.threshold;
if (rule.operator === ">=") triggered = currentValue >= rule.threshold;
if (rule.operator === "<=") triggered = currentValue <= rule.threshold;
return { triggered, severity: rule.severity, metric: rule.metric, currentValue, threshold: rule.threshold };
}
Three example rules and their results:
| Rule | Current value | triggered |
|---|---|---|
error_rate > 0.05, critical |
0.08 |
true (8% is above 5%) |
cpu >= 90, warning |
85 |
false |
disk < 10 (free disk %), critical |
5 |
true |
- The operator arrives as text (
">"), so each text is mapped to the matching JavaScript comparison. <is for metrics that are dangerous when too small, such as free disk space.- The returned object is built with the key order
triggered,severity,metric,currentValue,threshold. In the exercise that order is checked too.
Threshold and for:
An alert has three states:
- inactive: the condition is false (not met).
- pending: the condition is true, but has not held for the
for:duration yet. - firing: the condition has been true for the
for:duration. A notification is sent. When the condition turns false again, the alert is resolved (finished).
If the condition turns false while the alert is still pending, the alert is dropped without a notification. These are two series (error rate values, one per minute) from the Lab, with a 5% threshold and for: 5m:
| Series | Above 5% | pending | firing | Notifications |
|---|---|---|---|---|
| deploy blip | 10:08 to 10:09 | 10:08 to 10:09 | never | 0 |
| incident | 10:14 to 10:23 | 10:14 to 10:18 | 10:19 to 10:23 | 10:19 firing, 10:24 resolved |
A 2-minute blip never holds for 5 minutes, so nobody gets a call. But there is a price, shown in the second row: a real incident only calls someone 5 minutes after the error rate crosses the threshold. for: makes alerts calmer, but as a result they come later. For a problem every user feels, a for: of 2 to 5 minutes is enough.
Flapping
The Lab's third series goes up and down around 5%: 5.4, then 4.7, then 5.3, then 4.6. With for: 0m, the alert fires, then resolves, then fires again: it fires 12 times in 30 minutes. That is called flapping: one problem, a dozen phone calls. Ways to calm it:
- A long enough
for:, so a brief spike does not count. keep_firing_for: 5m: the alert stays firing for 5 minutes after the condition turns false, so a brief dip is not taken as the end.- A threshold at the level you actually care about. If a 4.5% error rate is a problem too, the threshold is 4, not 5.
In the Lab, for: 2m plus keep_firing_for: 5m gives one notification at 10:13, and the alert stays firing while the problem lasts.
Symptoms, not causes
Phone people for symptoms users feel: a high error rate, a slow p99, a site that will not load. Not for possible causes. Take a CPU at 90%: if every request stays fast and succeeds, that CPU hurts nobody. Causes belong on dashboards and in tickets (tasks for working hours), and are used when investigating a symptom.
Every alert must be actionable (something you can act on): there is something to do the moment it fires. Alerts that keep firing but never need action cause alert fatigue (tiredness from too many alerts): people stop reacting, and the important alerts get ignored too. The cure: only actionable alerts, the right threshold and for:, and clear severities (levels of seriousness).
Severity: page or ticket
Severity decides where an alert goes and how fast it must be handled:
| Severity | Example in notes-api | Where it goes |
|---|---|---|
critical |
error rate above 5% for 5 minutes, site down | page: phone the on-call, any hour |
warning |
TLS certificate expires in 14 days, disk at 80% | ticket in Slack #notes-alerts, handled in working hours |
Alertmanager reads the severity label and forwards the alert:
route:
receiver: slack-ticket # default: a ticket in Slack
routes:
- matchers: ['severity="critical"']
receiver: pagerduty-oncall # critical: phone the on-call
Many teams also use incident levels SEV1 to SEV4: SEV1 means the service is down for every user, SEV4 means a cosmetic issue (only how something looks).
Runbooks and on-call
A runbook is the page an alert links to. It holds what the alert means, which dashboard to open, the first steps (for example "check the last deploy, roll back if errors started after it"), and whom to contact. With a runbook, someone woken at 3 am does not have to start without a plan.
An on-call rotation shares the duty. For example, each week one person is primary (the first to be phoned) and one is secondary (the backup), taking turns. Services such as PagerDuty, incident.io, Jira Service Management or Grafana Cloud IRM hold that schedule and the escalation policy: if the primary does not acknowledge (say "I have it") within 5 minutes, the alert goes up to the secondary. Two other names are no longer sold or developed: Opsgenie stopped being sold in June 2025 and shuts down on 5 April 2027, and Grafana OnCall OSS was archived in March 2026. When you are called, the order is:
- acknowledge: say that you are handling it;
- assess the impact;
- mitigate first, which means reduce the damage (roll back, switch off a feature flag);
- only then look for the root cause.
Writing the postmortem (the report after an incident) is covered in the Incident Response & Postmortems lesson.
⚠️ Alerts that wake people up for nothing
- Alerts without
for:. Every brief spike, including restarts during a deploy, wakes someone up. - Paging on causes. High CPU, memory or connection counts with no symptom users feel are a ticket.
- Alerts that are not actionable. If the right response when it fires is "just ignore it", delete the alert.
- In the exercise: swapped operators or a different key order.
">="must use>=, not>. One of the checks: CPU 85 with the rule>= 90must betriggered: false. The result is{ triggered, severity, metric, currentValue, threshold }in exactly that order.
TL;DR: Alerting calls a person when there is a problem that needs action, and on-call is the duty roster. An alert rule compares a metric with a threshold (operators >, <, >=, <=) every time it is checked. for: keeps the alert pending (waiting) until the condition has held for that long, so a 2-minute blip (a brief spike) wakes nobody. The price is that a real incident only calls someone once the for: duration has passed. A value moving up and down around the threshold causes flapping (the alert sounds and stops again and again); calm it with for:, keep_firing_for, or a threshold at the level you actually care about. Page (phone) on symptoms users feel (error rate, p99), make tickets (tasks for working hours) for causes and anything that can wait, and delete alerts that are not actionable (need no action), so alert fatigue (people ignoring too many alerts) does not start. Severity (the level of seriousness) critical becomes a phone call to the on-call, warning a ticket, routed by Alertmanager. Every alert links a runbook (a page of handling steps), and an on-call rotation has a primary (the first to be phoned), a secondary (the backup), and escalation when nobody acknowledges (says they are handling it).
⚡ An alert rule: threshold and for:
Write:
- alert: HighErrorRate
expr: |
sum(rate(http_requests_total{job="notes-api",status_code=~"5.."}[1m]))
/ sum(rate(http_requests_total{job="notes-api"}[1m])) * 100 > 5
for: 5m
labels:
severity: critical
Meaning: Alerting tells the team when there's a problem that needs action. This Prometheus rule is active while notes-api's error rate (the percent of requests that fail with a 5xx status) is above 5%. Because of for: 5m, the alert is only sent once the condition has held for 5 minutes. Alertmanager sends the notification.
⚡ Pending first, then firing
Guess first: the error rate goes above 5% for 2 minutes during a deploy, then back to normal. With for: 5m, how many notifications are sent?
Zero. The alert is pending (waiting) for 2 minutes, then dropped; it never fires (sounds). The price: a real incident also only calls someone 5 minutes after it starts.
Meaning: An error rate moving up and down around the threshold (the limit, here 5%) makes the alert switch between firing and resolved (finished). That is called flapping: one problem, a dozen phone calls. Calm it with for:, keep_firing_for: 5m (the alert stays firing for 5 minutes after the condition turns false), or a threshold at the level you actually care about.
⚡ Symptoms, not causes
Meaning: Phone people for symptoms users feel: a high error rate, a slow p99 (the duration 99% of requests do not exceed), a site that won't load. A CPU at 90% while every request stays fast is a possible cause, not a symptom, so a ticket (a task for working hours) is enough, not a call. Alerts that aren't actionable cause alert fatigue: people stop reacting, and the important alerts get ignored too.
⚡ critical becomes a call, warning a ticket
Write:
route:
receiver: slack-ticket # default: a ticket in Slack
routes:
- matchers: ['severity="critical"']
receiver: pagerduty-oncall # critical: phone the on-call
Meaning: Alertmanager reads the severity label and forwards the alert: critical goes to the phone of the person on call, everything else becomes a ticket in Slack. Every alert links a runbook (a page of handling steps). An on-call rotation has a primary (the first to be phoned), a secondary (the backup), and escalation to the secondary when the primary does not acknowledge (say they are handling it).