4.5.22 · D3Software Engineering

Worked examples — Logging and monitoring — structured logging, metrics, alerting

4,583 words21 min readBack to topic

Two symbols we lean on all page — defined once, here


The scenario matrix

Every worked example below is tagged with the cell of this matrix it covers. Together they touch every cell.

# Case class What makes it tricky Covered by
C1 Counter → rate, clean ordinary slope of a monotone line Ex 1
C2 Counter reset / wrap-around value drops → naive subtraction goes negative Ex 2
C3 Percentile, ordinary pick the rank, read the value Ex 3
C4 Percentile, degenerate + interpolation rank rounding, tail dominates, even- median Ex 4
C5 Error budget → count & time multiply the fraction Ex 5
C6 Budget burn rate (limiting: how fast is "too fast") rate of spending vs allowed Ex 6
C7 Alert with for duration (noisy input, blip vs real) sustained-condition logic Ex 7
C8 Average vs percentile trap (the "looks fine" case) mean hides the tail, p100 = max Ex 8
C9 Real-world word problem (structured-log field design) choosing what to emit Ex 9
C10 Exam twist (multi-window burn-rate alert) combine burn rate + windows Ex 10

Ex 1 — Counter → rate, clean (C1)


Ex 2 — Counter reset AND wrap-around (C2)

Two distinct ways a counter's value can drop, both of which break naive subtraction.


Ex 3 — Percentile, ordinary (C3)

Read the figure below to see what "value at a rank" means: the bar chart plots the 20 sorted latencies (black) with each bar's height = its latency and its position = its rank. The two red bars are exactly the ranks the formula selected — rank 10 (p50) and rank 18 (p90). The picture makes the definition concrete: a percentile is not a computed average, it is literally one bar you point at after sorting.

Figure — Logging and monitoring — structured logging, metrics, alerting

Ex 4 — Percentile: degenerate inputs AND interpolation (C4)

Four sub-cases: all-equal, tiny- tail, even- median interpolation, and why they differ.


Ex 5 — Error budget → count and time (C5)


Ex 6 — Budget burn rate, limiting behaviour (C6)


Ex 7 — Alert with a for duration, noisy input (C7)

Read the figure below to see the for logic: the black step line is the error rate minute by minute; the dashed line is the 2% threshold. The red shaded band marks the continuous breaching run (minutes 4–8) that finally satisfies for 5m, and the red dot marks the exact minute the page fires. Notice the lone spike at minute 2 pokes above the threshold but sits alone — the picture shows why it never accumulates a 5-minute run and so is correctly ignored.

Figure — Logging and monitoring — structured logging, metrics, alerting

Ex 8 — Average vs percentile, the "looks fine" trap (C8)


Ex 9 — Real-world word problem: designing log fields (C9)


Ex 10 — Exam twist: multi-window burn-rate alert (C10)


Recall Rapid self-test

Two counter reads 900 then 300 over 10 s (64-bit, alive) — reported rate? ::: Treat as reset (a decrease); rate = 300/10 = 30 req/s (never negative). A 32-bit counter reads its max 4,294,967,295 then 4 over 5 s — increments? ::: Wrap-around: (M − old) + 1 + new = 0 + 1 + 4 = 5 increments, rate 1 req/s. p95 of 10 samples where the max is 800 ms? ::: 800 ms — because , the last rank; too few samples to trust. Median of [10,20,30,40,50,60] by nearest-rank vs interpolation? ::: Nearest-rank 30; interpolated 35 (average of 30 and 40). 99.95% SLO over 7 days with 20M requests — allowed failures? ::: 10,000 failures (≈ 5.04 min/week). Error rate 0.25% against a 0.05% budget — burn rate? ::: 5× (exhausts a 7-day budget in 1.4 days). Alert "> 2% for 5 min", one-minute blip at minute 2 — does it page? ::: No; a single-scrape spike resets the for run. What is p100? ::: The maximum (rank ); is the minimum.