1. The required metrics #
Counting good and bad events.
Instrumentation #
A ratio SLI needs two counters: the total number of events and the bad (or good) ones. For an HTTP API, a counter per status code and a duration histogram are enough:
http_requests_total{service="checkout", route="/pay", code="200"}
http_requests_total{service="checkout", route="/pay", code="503"}
http_request_duration_seconds_bucket{service="checkout", le="0.1"}
http_request_duration_seconds_bucket{service="checkout", le="0.3"}
http_request_duration_seconds_count{service="checkout"}Good practice: Measure as close to the user as possible: load balancer or ingress metrics rather than application metrics, to also count requests that never reach the code.
2. SLI queries #
Availability and latency.
Availability #
# Taux d'erreur sur 5 minutes
sum(rate(http_requests_total{service="checkout", code=~"5.."}[5m]))
/
sum(rate(http_requests_total{service="checkout"}[5m]))Latency #
# Part des requêtes servies en plus de 300 ms (le seuil doit être un bucket existant)
1 - (
sum(rate(http_request_duration_seconds_bucket{service="checkout", le="0.3"}[5m]))
/
sum(rate(http_request_duration_seconds_count{service="checkout"}[5m]))
)Common mistake: The latency threshold must match a bucket boundary exactly (le="0.3"). Otherwise, add that bucket to the histogram or use Prometheus native histograms.3. Recording rules #
Precomputing for several windows.
Multi-window #
Burn rate alerts need the error rate over several windows (5 min, 30 min, 1 h, 2 h, 6 h, 1 d, 3 d). We precompute them:
groups:
- name: slo-checkout
rules:
- record: slo:sli_error:ratio_rate5m
expr: |
sum(rate(http_requests_total{service="checkout", code=~"5.."}[5m]))
/ sum(rate(http_requests_total{service="checkout"}[5m]))
labels: { slo: checkout-availability }
- record: slo:sli_error:ratio_rate1h
expr: |
sum(rate(http_requests_total{service="checkout", code=~"5.."}[1h]))
/ sum(rate(http_requests_total{service="checkout"}[1h]))
labels: { slo: checkout-availability }
# ... 30m, 2h, 6h, 1d, 3d
- record: slo:error_budget_remaining:ratio
expr: |
1 - (
sum_over_time(slo:sli_error:ratio_rate5m{slo="checkout-availability"}[30d])
/ count_over_time(slo:sli_error:ratio_rate5m{slo="checkout-availability"}[30d])
) / (1 - 0.999)Good to know: Writing these rules by hand is tedious and error-prone: this is exactly what Sloth and Pyrra generate from a description of a few lines.
