Computing SLIs from Request Logs
Before you write Prometheus rules, compute an SLI by hand from raw request logs once. It forces every decision into the open: which requests are valid, what "good" means, how latency is cut, and how the numbers change per user journey. Logs are also how you check that a metrics-based SLI is right.
A log line per request
Most load balancers and ingress controllers can write structured access logs. The examples use one JSON object per line with the fields an SLI needs:
{"ts": "2026-03-02T10:00:04Z", "route": "POST /checkout", "status": 201, "ms": 182, "ua": "Mozilla/5.0"}A first pass on twelve lines
import json
LOG = """\
{"ts": "2026-03-02T10:00:01Z", "route": "GET /products", "status": 200, "ms": 41, "ua": "Mozilla/5.0"}
{"ts": "2026-03-02T10:00:02Z", "route": "GET /healthz", "status": 200, "ms": 1, "ua": "kube-probe/1.37"}
{"ts": "2026-03-02T10:00:02Z", "route": "POST /checkout", "status": 201, "ms": 182, "ua": "Mozilla/5.0"}
{"ts": "2026-03-02T10:00:03Z", "route": "GET /products/999", "status": 404, "ms": 12, "ua": "Mozilla/5.0"}
{"ts": "2026-03-02T10:00:04Z", "route": "POST /checkout", "status": 503, "ms": 30012, "ua": "Mozilla/5.0"}
{"ts": "2026-03-02T10:00:05Z", "route": "GET /products", "status": 200, "ms": 388, "ua": "Mozilla/5.0"}
{"ts": "2026-03-02T10:00:05Z", "route": "GET /healthz", "status": 200, "ms": 1, "ua": "kube-probe/1.37"}
{"ts": "2026-03-02T10:00:06Z", "route": "POST /checkout", "status": 429, "ms": 3, "ua": "Mozilla/5.0"}
{"ts": "2026-03-02T10:00:07Z", "route": "GET /products", "status": 200, "ms": 95, "ua": "k6/1.4 (loadtest)"}
{"ts": "2026-03-02T10:00:08Z", "route": "POST /checkout", "status": 201, "ms": 240, "ua": "Mozilla/5.0"}
{"ts": "2026-03-02T10:00:09Z", "route": "GET /products", "status": 500, "ms": 77, "ua": "Mozilla/5.0"}
{"ts": "2026-03-02T10:00:09Z", "route": "GET /products", "status": 200, "ms": 120, "ua": "Mozilla/5.0"}
"""
def valid(r):
if r["route"] == "GET /healthz" or "loadtest" in r["ua"]:
return False # probes and our own load tests
if 400 <= r["status"] < 500 and r["status"] != 429:
return False # client errors, except our own throttling
return True
def available(r):
return r["status"] < 500 and r["status"] != 429
def fast(r, threshold_ms=300):
return available(r) and r["ms"] <= threshold_ms # a fast error is not a good event
rows = [json.loads(line) for line in LOG.splitlines()]
v = [r for r in rows if valid(r)]
print(f"{len(rows)} lines, {len(v)} valid")
print(f"availability: {sum(map(available, v))}/{len(v)} = {sum(map(available, v)) / len(v):.1%}")
print(f"latency<=300ms: {sum(map(fast, v))}/{len(v)} = {sum(map(fast, v)) / len(v):.1%}")
for r in v:
a, l = ("good" if available(r) else "bad "), ("good" if fast(r) else "bad")
print(f" {r['route']:<16} {r['status']} {r['ms']:>6} ms availability {a} latency {l}")12 lines, 8 valid
availability: 5/8 = 62.5%
latency<=300ms: 4/8 = 50.0%
GET /products 200 41 ms availability good latency good
POST /checkout 201 182 ms availability good latency good
POST /checkout 503 30012 ms availability bad latency bad
GET /products 200 388 ms availability good latency bad
POST /checkout 429 3 ms availability bad latency bad
POST /checkout 201 240 ms availability good latency good
GET /products 500 77 ms availability bad latency bad
GET /products 200 120 ms availability good latency good
Decisions this code makes, each worth arguing about:
- the 503 that took 30 s counts against both availability and latency;
- the 429 counts as unavailable, since it's our own rate limiter turning users away;
- the latency SLI counts only successful fast requests as good, so a fast error can't improve latency.
A day of traffic, per journey
Real SLIs are computed per user journey over a window. The next model generates a deterministic day of logs for two journeys (seeded random numbers), with a 20-minute incident at 14:00 where checkout's payment provider fails half the time and slows down, then reports the SLIs per journey for the day and per hour around the incident:
import random
from collections import defaultdict
rng = random.Random(2026)
def request(minute, route):
incident = route == "POST /checkout" and 14 * 60 <= minute < 14 * 60 + 20
if incident and rng.random() < 0.5:
return 503, round(rng.uniform(2000, 5000))
base = 120 if route == "POST /checkout" else 45
ms = round(rng.expovariate(1 / base)) + (900 if incident else 0)
status = 500 if rng.random() < 0.0005 else 200
return status, ms
totals = defaultdict(lambda: [0, 0, 0]) # route -> [valid, available, fast]
hourly = defaultdict(lambda: [0, 0, 0]) # (route, hour) -> same
for minute in range(24 * 60):
for route, per_min in (("GET /products", 40), ("POST /checkout", 6)):
for _ in range(per_min):
status, ms = request(minute, route)
ok = status < 500
fast = ok and ms <= (800 if route == "POST /checkout" else 300)
for bucket in (totals[route], hourly[(route, minute // 60)]):
bucket[0] += 1
bucket[1] += ok
bucket[2] += fast
print("whole day")
for route, (n, ok, fast) in totals.items():
print(f" {route:<15} requests {n:>6} availability {ok / n:.3%} latency {fast / n:.3%}")
print("checkout by hour")
for hour in (13, 14, 15):
n, ok, fast = hourly[("POST /checkout", hour)]
print(f" {hour:02}:00-{hour:02}:59 availability {ok / n:.2%} latency {fast / n:.2%}")whole day
GET /products requests 57600 availability 99.948% latency 99.809%
POST /checkout requests 8640 availability 99.236% latency 98.333%
checkout by hour
13:00-13:59 availability 100.00% latency 99.72%
14:00-14:59 availability 83.89% latency 66.39%
15:00-15:59 availability 100.00% latency 99.44%
The day-level availability for checkout is still above 99%, yet for twenty minutes half of all checkouts failed. Two lessons follow. Aggregate per journey: blended with the busy product page, the checkout incident nearly disappears. The window matters: a 24-hour SLI hides a sharp incident that a 1-hour SLI shows clearly, which is why burn-rate alerts look at several windows at once.
From logs to metrics
Computing SLIs from logs is precise but expensive at scale. In production you usually count good and valid events
as Prometheus counters at the load balancer or in the service (http_requests_total{route, code} and a latency
histogram), then compute the ratio with recording rules (SLO Recording Rules). Keep
the log version as a cross-check: run it on a sample day every quarter and compare.
Test yourself
Why does the latency SLI in the first model count a fast 500 as a bad event?
- 500s are always slow
- Otherwise an outage that fails fast would make latency look better
- Prometheus requires it
- It doesn't; it counts as good
Checkout half-failed for 20 minutes, yet its daily availability is still above 99%. What does that teach?
- The incident was unimportant
- Long windows hide short sharp incidents; look at shorter windows too
- The SLI is wrong
- Daily SLIs are always 99%
Why compute SLIs per user journey rather than for all routes together?
- It's faster
- High-volume, easy routes dilute failures on a critical low-volume route such as checkout
- Prometheus can't sum routes
- Journeys have more requests