SLO Alerts & Dashboards
Metrics only help at 3 a.m. if something turns them into a page for the right problem, and a dashboard shows the on-call engineer where to look. This lesson writes linkly's service level objective, the recording and alert rules that enforce it, tests for those rules, and the dashboard. The SRE course derives the burn-rate maths; the Observability course covers rule syntax and Alertmanager.
The SLO
| SLI | the proportion of requests to linkly that don't fail with a 5xx, measured by http_requests_total at the service |
| SLO | 99.9% over 30 days (an error budget of 0.1%: about 43 minutes of full outage a month) |
| Latency objective | p99 of redirects (GET /{code}) below 250 ms; a ticket, not a page |
| Not covered | requests that never reach linkly (Gateway or DNS failures): measure those with a synthetic probe from outside |
Redirects are what users notice, but POST /links failing matters too, so the availability SLI covers every route. Probes count as traffic here; if you think they distort the ratio, exclude route=~"/healthz|/readyz|/metrics" in the recording rules and say so in the SLO document.
The rules, tested
The session writes the rule file (recording rules first, then three alerts) and a unit-test file with synthetic series, runs promtool check and promtool test, and wraps the rules into a Prometheus Operator PrometheusRule validated against its CRD schema:
mkdir -p monitoring && cd monitoring
cat > linkly-rules.yml <<'YAML'
groups:
- name: linkly-recording
rules:
- record: job:http_requests:rate5m
expr: sum by (job) (rate(http_requests_total{job="linkly"}[5m]))
- record: job:slo_errors_per_request:ratio_rate5m
expr: |
sum by (job) (rate(http_requests_total{job="linkly",code=~"5.."}[5m]))
/ sum by (job) (rate(http_requests_total{job="linkly"}[5m]))
- record: job:slo_errors_per_request:ratio_rate1h
expr: |
sum by (job) (rate(http_requests_total{job="linkly",code=~"5.."}[1h]))
/ sum by (job) (rate(http_requests_total{job="linkly"}[1h]))
- record: job_route:http_request_duration_seconds:p99_5m
expr: |
histogram_quantile(0.99,
sum by (job, route, le) (rate(http_request_duration_seconds_bucket{job="linkly"}[5m])))
- record: job:linkly_cache_hits:ratio_rate5m
expr: |
sum by (job) (rate(linkly_cache_requests_total{job="linkly",result="hit"}[5m]))
/ sum by (job) (rate(linkly_cache_requests_total{job="linkly"}[5m]))
- name: linkly-alerts
rules:
- alert: LinklyErrorBudgetBurn
expr: |
job:slo_errors_per_request:ratio_rate1h > (14.4 * 0.001)
and job:slo_errors_per_request:ratio_rate5m > (14.4 * 0.001)
labels: {severity: page}
annotations:
summary: "linkly is burning its 99.9% availability budget 14x too fast"
runbook_url: https://github.com/acme/linkly/blob/main/docs/runbook.md#error-budget-burn
- alert: LinklySlowRedirects
expr: job_route:http_request_duration_seconds:p99_5m{route="/{code}"} > 0.25
for: 10m
labels: {severity: ticket}
annotations:
summary: "p99 redirect latency is {{ $value | humanizeDuration }}"
- alert: LinklyCacheDegraded
expr: sum by (job) (rate(linkly_cache_requests_total{job="linkly",result="error"}[5m])) > 0
for: 5m
labels: {severity: ticket}
annotations:
summary: "Redis errors: redirects are served from PostgreSQL"
YAML
cat > linkly-rules.test.yml <<'YAML'
rule_files: [linkly-rules.yml]
evaluation_interval: 1m
tests:
- name: a burst of 5xx pages, a single error does not
interval: 1m
input_series:
- series: 'http_requests_total{job="linkly",route="/{code}",code="302"}'
values: '0+600x90'
- series: 'http_requests_total{job="linkly",route="/{code}",code="500"}'
values: '0x60 1 1 1 1 1 1 1 1 1 1 61+60x20'
promql_expr_test:
- expr: job:http_requests:rate5m
eval_time: 30m
exp_samples: [{labels: 'job:http_requests:rate5m{job="linkly"}', value: 10}]
alert_rule_test:
- eval_time: 70m
alertname: LinklyErrorBudgetBurn
exp_alerts: []
- eval_time: 85m
alertname: LinklyErrorBudgetBurn
exp_alerts:
- exp_labels: {job: linkly, severity: page}
exp_annotations:
summary: "linkly is burning its 99.9% availability budget 14x too fast"
runbook_url: https://github.com/acme/linkly/blob/main/docs/runbook.md#error-budget-burn
- name: Redis down for a while
interval: 1m
input_series:
- series: 'linkly_cache_requests_total{job="linkly",result="hit"}'
values: '0+540x30'
- series: 'linkly_cache_requests_total{job="linkly",result="error"}'
values: '0x10 0+300x20'
promql_expr_test:
- expr: job:linkly_cache_hits:ratio_rate5m
eval_time: 25m
exp_samples: [{labels: 'job:linkly_cache_hits:ratio_rate5m{job="linkly"}', value: 0.64285714285714285}]
alert_rule_test:
- eval_time: 14m
alertname: LinklyCacheDegraded
exp_alerts: []
- eval_time: 18m
alertname: LinklyCacheDegraded
exp_alerts:
- exp_labels: {job: linkly, severity: ticket}
exp_annotations: {summary: "Redis errors: redirects are served from PostgreSQL"}
YAML
promtool check rules linkly-rules.yml
promtool test rules linkly-rules.test.yml
yq '{"apiVersion": "monitoring.coreos.com/v1", "kind": "PrometheusRule",
"metadata": {"name": "linkly", "labels": {"release": "monitoring"}}, "spec": .}' linkly-rules.yml > prometheusrule.yaml
kubeconform -strict -summary prometheusrule.yaml
echo "== the same tests against a copy with a one-letter typo in a matcher"
sed 's/job="linkly",code=~"5.."}\[5m\]/job="linky",code=~"5.."}[5m]/' linkly-rules.yml > typo.yml
sed 's/rule_files: \[linkly-rules.yml\]/rule_files: [typo.yml]/' linkly-rules.test.yml > typo.test.yml
promtool check rules typo.yml | grep SUCCESS
promtool test rules typo.test.yml; echo "exit code: $?"Checking linkly-rules.yml
SUCCESS: 8 rules found
SUCCESS
Summary: 1 resource found in 1 file - Valid: 1, Invalid: 0, Errors: 0, Skipped: 0
== the same tests against a copy with a one-letter typo in a matcher
SUCCESS: 8 rules found
FAILED:
name: a burst of 5xx pages, a single error does not,
alertname: LinklyErrorBudgetBurn, time: 1h25m,
exp:[
0:
Labels:{alertname="LinklyErrorBudgetBurn", job="linkly", severity="page"}
Annotations:{runbook_url="https://github.com/acme/linkly/blob/main/docs/runbook.md#error-budget-burn", summary="linkly is burning its 99.9% availability budget 14x too fast"}
],
got:[]
exit code: 1
The alerts
| Alert | Fires when | Severity | Why this severity |
|---|---|---|---|
LinklyErrorBudgetBurn |
the 5xx ratio is above 14.4 × 0.1% over both the last hour and the last 5 minutes | page | at that rate 2% of the monthly budget is gone in an hour; the 5-minute window makes it reset quickly once fixed |
LinklySlowRedirects |
p99 of /{code} above 250 ms for 10 minutes |
ticket | slow is bad, but not an emergency |
LinklyCacheDegraded |
any Redis errors for 5 minutes | ticket | redirects still work (from PostgreSQL); fix it in working hours |
This is the single fast-burn page of the multi-window approach; the Multiwindow Alerts lesson adds the slower 6h/30m page and the 3-day ticket for budgets burning slowly. Every page has a runbook_url: an alert without instructions is a puzzle for whoever is on call.
The tests
- A burst of 5xx pages, a single error does not. Steady traffic of 600 requests a minute, one error a minute from minute 60 (0.17%, below the 1.44% threshold: silence at 70m), then 60 errors a minute from minute 71. At 85m both windows are over the threshold, and the test asserts the exact labels and annotations of the page. The same test also checks a recording rule with
promql_expr_test: 600 requests a minute is10per second. - Redis down for a while. Cache errors start at minute 10. The alert has
for: 5m, so the test asserts silence at 14m and a ticket at 18m; the hit ratio recording rule reads540 / (540 + 300) ≈ 0.643during the outage.
The last part of the session shows why this matters. A one-letter typo in a label matcher (job="linky") is still valid PromQL, so promtool check rules reports SUCCESS, and in production the alert would silently never fire. The unit test catches it: at 1h25m it expected the page and got nothing, and promtool exits non-zero, which fails the pipeline. CI runs promtool test rules next to go test (The CI Workflow).
The PrometheusRule carries the label release: monitoring because kube-prometheus-stack's Prometheus, by default, loads only rules labelled with its Helm release name: the same selector trap as for ServiceMonitors in Local Track. Commit prometheusrule.yaml into the chart (behind a value) or into linkly-deploy, so Argo CD applies it with the app.
Routing the alerts
Alertmanager decides who hears about what. A minimal route for linkly sends pages to the on-call rotation and tickets to the team's channel:
# Alertmanager configuration (kube-prometheus-stack: alertmanager.config in the chart's values)
route:
receiver: team-linkly-chat
group_by: [alertname, namespace]
routes:
- matchers: ['severity="page"']
receiver: oncall-pager
group_wait: 30s
repeat_interval: 1h
- matchers: ['severity="ticket"']
receiver: team-linkly-chat
repeat_interval: 12h
receivers:
- name: oncall-pager
pagerduty_configs:
- routing_key_file: /etc/alertmanager/secrets/pagerduty/routing-key
- name: team-linkly-chat
slack_configs:
- api_url_file: /etc/alertmanager/secrets/slack/webhook-url
channel: "#linkly"Secrets are read from files mounted from Kubernetes Secrets, never written into the configuration (Alertmanager).
The dashboard
One dashboard, top to bottom in the order an on-call engineer asks questions: Are users affected? Since when? Which part? Is it us or a dependency? Kept as code, provisioned by the Grafana sidecar from a ConfigMap labelled grafana_dashboard: "1" (Dashboards as Code):
| Row | Panel | Query |
|---|---|---|
| Users | availability over the last hour, against the 99.9% objective | 1 - job:slo_errors_per_request:ratio_rate1h{job="linkly"} with a threshold line at 0.999 |
| Users | request rate by route | sum by (route) (rate(http_requests_total{job="linkly", namespace="$env"}[$__rate_interval])) |
| Users | p50 / p99 latency of redirects | histogram_quantile(0.99, sum by (le) (rate(http_request_duration_seconds_bucket{job="linkly", route="/{code}", namespace="$env"}[$__rate_interval]))) |
| Errors | 5xx by route | sum by (route) (rate(http_requests_total{job="linkly", code=~"5..", namespace="$env"}[$__rate_interval])) |
| Dependencies | cache hit ratio and errors | job:linkly_cache_hits:ratio_rate5m, sum by (result) (rate(linkly_cache_requests_total{namespace="$env"}[$__rate_interval])) |
| Dependencies | PostgreSQL | CloudNativePG's own dashboard (its exporter is built in): link, don't duplicate |
| Rollouts | pods per version | count by (version) (linkly_build_info{namespace="$env"}) |
| Saturation | CPU and memory vs requests and limits | container_cpu_usage_seconds_total, container_memory_working_set_bytes for container="linkly" |
| Logs | error lines | Loki: {namespace="$env", app="linkly"} | json | level="ERROR" |
$env is a dashboard variable (label_values(linkly_build_info, namespace)), so one dashboard serves staging and production (Dashboard Variables). The "pods per version" panel turns every rollout into a visible staircase, and lines up with Argo CD sync events if you add them as annotations.
Test yourself
Why does the page require the error ratio to be high over both 1 hour and 5 minutes?
- Prometheus needs two windows
- The long window proves the burn is significant; the short window makes the alert stop soon after the problem is fixed
- To halve the alert volume
- The 5-minute window is for latency
Why is LinklyCacheDegraded a ticket and not a page?
- Redis errors can't be measured
- Redirects keep working from PostgreSQL, so users are not failing; it needs fixing, not waking someone up
- Tickets are cheaper
- Redis has its own alerts
What would happen without promtool test rules if the alert said job="linky"?
- Prometheus would refuse the rule
- The alert would load fine and never fire
- It would fire constantly
- promtool check rules would catch it