Advanced
Searching inLabs
How to search
  • Search a task or a tool (lvm, selinux, @podman); filter by level and by book or course.
  • With an empty search box, a filter alone lists the labs — for example all hard labs of one course.
  • "two words" finds the exact phrase.
  • Plain words find names, texts and examples.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search
Try:

Lab 13.1Engineering practice, Chapter 13

From logs to an SLO: availability, latency, error budget and a burn-rate alert

Level
Medium
45 min
Machines
any Linux or macOS shell with awk and Python 3
Status
Not started

Goal

Turn raw access logs into the numbers a reliability review asks for. You generate a day of web traffic with a short outage, measure availability and the 95th / 99th latency percentile, set an SLO, calculate how much of the error budget was burned, and write a burn-rate alert rule with a runbook link instead of a noisy "5xx > 0" alert.

Before you start

  • Make the folder ~/slo-lab. Generate a log of 100,000 requests (one day) with a 20-minute outage at 14:00:
    gen.py
    import random
    random.seed(7)
    with open("access.log", "w") as f:
        for i in range(100_000):
            sec = int(i * 86400 / 100_000)
            hh, mm, ss = sec // 3600, sec % 3600 // 60, sec % 60
            outage = 14 * 3600 <= sec < 14 * 3600 + 1200
            status = 503 if outage and random.random() < 0.6 else (500 if random.random() < 0.002 else 200)
            ms = int(random.lognormvariate(4.6, 0.5)) + (900 if outage else 0)
            f.write(f"2026-11-14T{hh:02d}:{mm:02d}:{ss:02d} GET /api/orders {status} {ms}\n")
    python3 gen.py && wc -l access.log && head -2 access.log

Tasks

  1. Count all requests and the bad ones (status 5xx). Calculate availability in percent.
  2. Calculate the p95 and p99 latency for the whole day and for the outage window only.
  3. Define two SLOs for this API: availability 99.5 % over 30 days, and 99 % of requests faster than 500 ms.
  4. Error budget: how many bad requests does 99.5 % allow for this day? What share of today's budget was burned?
  5. Find the worst 5-minute window and its error rate. What is the burn rate (error rate ÷ allowed error rate)?
  6. Write a Prometheus alert that fires on a fast burn (burn rate above 14.4 over 1 hour and 5 minutes), with severity: page and a runbook_url.
  7. Write down which two of the five alert anti-patterns from the chapter this alert avoids.

Check your work

awk '{n++} $4>=500{b++} END{printf "requests=%d bad=%d availability=%.3f%%\n", n, b, 100*(n-b)/n}' access.log
cat slo.md
grep -c "runbook_url" burn-alert.yml
  • 100,000 requests and about 1,000 bad ones; availability about 99.0 % — below the SLO for this day.
  • The daily p95 is about 240 ms (fine), but the daily p99 is near 1 second: the 20-minute outage alone breaks the latency SLO. During the outage p95 and p99 are above 1,100 ms.
  • The worst 5-minute window (14:15) has an error rate near 62 %, a burn rate of about 124.
  • burn-alert.yml has one alert with two windows and a runbook link.

Hints

Show the hints
  • awk: column 4 is the status, column 5 the time in ms.
  • Percentiles: sort -n the times and take the line at 95 % / 99 % of the count.
  • Allowed error rate = 1 − 0.995 = 0.005. Budget for the day = requests × 0.005.
  • 5-minute window: the first 15 characters of the time stamp (2026-11-14T14:0) group by 10 minutes; for 5 minutes use substr($1,12,2) hour and int(substr($1,15,2)/5).
  • Google SRE multi-window rule: page when burn rate > 14.4 for 1 h and for 5 min (2 % of a 30-day budget in one hour).

Solution

Show the solution
  1. Availability.
    awk '{n++} $4>=500{b++} END{printf "requests=%d bad=%d availability=%.3f%%\n", n, b, 100*(n-b)/n}' access.log
  2. Percentiles (whole day, then the outage window 14:00–14:19).
    awk '{print $5}' access.log | sort -n | awk '{a[NR]=$1} END{print "p95="a[int(NR*0.95)]" ms p99="a[int(NR*0.99)]" ms"}'
    awk '$1 ~ /T14:[01]/ {print $5}' access.log | sort -n | awk '{a[NR]=$1} END{print "outage p95="a[int(NR*0.95)]" ms p99="a[int(NR*0.99)]" ms"}'
  3. Error budget and the worst window.
    awk '{n++} $4>=500{b++} END{allow=n*0.005; printf "budget=%d used=%d burned=%.0f%% of today\n", allow, b, 100*b/allow}' access.log
    awk '{w=substr($1,12,2)":"sprintf("%02d",int(substr($1,15,2)/5)*5); n[w]++} $4>=500{b[w]++} END{for(w in n) printf "%s %.3f\n", w, b[w]/n[w]}' access.log | sort -k2 -nr | head -1

    Burn rate of the worst window = its error rate ÷ 0.005, here 0.618 ÷ 0.005 ≈ 124: at that speed the whole 30-day budget is gone in under 6 hours. The day used about 1,025 bad requests of a budget of 500: 205 %.

  4. The alert.
    burn-alert.yml
    groups:
      - name: slo-orders-api
        rules:
          - alert: OrdersApiErrorBudgetFastBurn
            expr: |
              (sum(rate(http_requests_total{job="orders",code=~"5.."}[1h])) / sum(rate(http_requests_total{job="orders"}[1h]))) > (14.4 * 0.005)
              and
              (sum(rate(http_requests_total{job="orders",code=~"5.."}[5m])) / sum(rate(http_requests_total{job="orders"}[5m]))) > (14.4 * 0.005)
            labels:
              severity: page
            annotations:
              summary: "Orders API burns its 30-day error budget 14x too fast"
              runbook_url: "https://wiki.example.com/runbooks/orders-api-errors"
  5. The SLO note.
    slo.md
    SLO orders API (30 days): availability 99.5 %, latency 99 % < 500 ms.
    Today: 99.0 % available, p99 974 ms -> 205 % of the daily error budget burned (outage 14:00-14:20).
    Alert: multi-window burn rate (1h + 5m) instead of "any 5xx": it pages on real user impact,
    not on single errors (anti-pattern: alert on causes, not symptoms), and every page has a runbook
    link (anti-pattern: alerts nobody knows how to handle).

My notes

No notes yet.

Lessons for this lab

Commands in this lab