Skip to content
KALANBLON
Show contents

Chapter 5 of 7 · 20 min

Observing production

By the end of this chapter, you'll be able to

  • Tell logs, metrics and traces apart
  • Write structured logs that are easy to filter
  • Measure latency with percentiles rather than an average
  • Compute an error budget from a service level objective

Logs, metrics, traces

  • a log tells about a specific event: “order 4521 rejected, insufficient stock”. It answers the question “what happened?”;
  • a metric is a number measured over time: requests per second, error rate, memory used. It answers “how are things going, overall?” and lends itself to graphs and alerts;
  • a trace follows a request through all the services it crosses (site, API, database, payment), with the duration of each step. It answers “where did the time go?”.

Logs are better structured: one JSON line per event, with named fields, rather than a free sentence. You can then filter and count without fragile regular expressions:

import json

log = [
    '{"level": "info", "route": "/cart", "duration_ms": 120, "status": 200}',
    '{"level": "error", "route": "/payment", "duration_ms": 3010, "status": 504}',
    '{"level": "info", "route": "/payment", "duration_ms": 450, "status": 200}',
    '{"level": "error", "route": "/payment", "duration_ms": 3002, "status": 504}',
]
events = [json.loads(line) for line in log]
errors = [e for e in events if e["level"] == "error"]
print(len(errors), "errors, all on", {e["route"] for e in errors})
2 errors, all on {'/payment'}

Two 504 errors (Gateway Timeout) of about 3 seconds on payment: the payment service is not answering in time. A log must never contain a password, a card number or unnecessary personal data: it is read by many people and kept for a long time.

The average lies

For latency (response time), the average is misleading: a few very slow requests get lost in the crowd. We use percentiles: the p95 is the duration below which 95% of requests fall. In other words, 1 request in 20 is slower than the p95.

latencies = [110, 95, 130, 105, 2900, 120, 98, 115, 3100, 102,
             125, 99, 108, 117, 2950, 104, 112, 96, 121, 109]

def percentile(values, p):
    ordered = sorted(values)
    rank = max(0, round(p / 100 * len(ordered)) - 1)
    return ordered[rank]

print("average:", sum(latencies) / len(latencies), "ms")
print("median (p50):", percentile(latencies, 50), "ms")
print("p95:", percentile(latencies, 95), "ms")
average: 540.8 ms
median (p50): 110 ms
p95: 2950 ms

The median says most requests are fast (110 ms). The p95 reveals that a significant share of users wait almost 3 seconds. The average, 541 ms, describes nobody's experience. There are several ways to compute a percentile; monitoring tools choose their own, but the idea stays the same.

A useful alert points to a problem that affects users and requires action.

Step 5 of 5
Text description of the animation
  1. Each request records its duration and status.
  2. Durations accumulate in a metric, per route and per minute.
  3. Percentiles show what the average hides: a tail of very slow requests.
  4. The objective agreed with the product team is not met.
  5. A useful alert points to a problem that affects users and requires action.

Objectives and error budget

An SLO (Service Level Objective) is a numerical objective, for example: “99.9% of requests succeed, over 30 days”. Aiming for 100% is unrealistic and paralysing. The complement, here 0.1%, is the error budget: the amount of failure you allow yourself. As long as there is budget left, the team can ship and experiment; once it is used up, reliability takes priority.

slo = 0.999
requests = 12_000_000
failures = 7_800

budget = round(requests * (1 - slo))
print("error budget:", budget, "failed requests allowed")
print(f"used: {failures / budget:.0%}")
print(f"measured availability: {1 - failures / requests:.4%}")
error budget: 12000 failed requests allowed
used: 65%
measured availability: 99.9350%

Mini-lab: your own dashboardPython 3.13

Output

Click “Run”: Python will load in your browser (about 10 MB the first time).

Your code runs in your browser, isolated from the page. · Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor

Your turn

Worked exercise

Worked exercise: will the budget hold?

An API's SLO is 99.5% of successful requests over 30 days. After 10 days, it has received 4,000,000 requests, of which 9,000 failed. Assuming the same traffic every day, compute the error budget for the 30 days, the share already used, and in how many days the budget will be exhausted if the failure rate stays the same.

Hint 1
Total expected traffic over 30 days: 4,000,000 × 3.
Hint 2
Failures per day: 9,000 / 10. Remaining budget divided by that rate: days left.
Show the solution and common mistakes
slo = 0.995
requests_10_days = 4_000_000
failures_10_days = 9_000

budget = round(requests_10_days * 3 * (1 - slo))
used = failures_10_days / budget
rate = failures_10_days / 10
days_left = (budget - failures_10_days) / rate

print("budget over 30 days:", budget)
print(f"used after 10 days: {used:.1%}")
print(f"exhausted in {days_left:.1f} days, i.e. on day {10 + days_left:.1f}")
budget over 30 days: 60000
used after 10 days: 15.0%
exhausted in 56.7 days, i.e. on day 66.7

At the current rate, the budget will not run out before the end of the period: the team can keep shipping normally. 15% of the budget is used in a third of the period, which is slower than the “neutral” rate (33%).

Common mistakes:

  • Computing the budget over the 10 elapsed days only: you get 20,000, and a usage of 45% that looks wrongly alarming.
  • Confusing 99.5% success and a 0.5% budget with 0.05%: a factor of 10, a very common slip when handling “nines”.

Summary

  1. 1Logs, metrics and traces answer “what happened?”, “how are things going?” and “where does the time go?”.
  2. 2Structured logs (JSON) are easy to filter; they never contain secrets.
  3. 3Latency is tracked with percentiles (p50, p95, p99): the average hides slow requests.
  4. 4An SLO sets a numerical objective; its complement is the error budget you allow yourself.
  5. 5Only alert a person for a problem that affects users and requires action.

Your progress is saved on this device. Create a free account to keep your progress on all your devices.