RousselTMLEARNING

Observability

Measuring SLIs with Prometheus

Implement availability and latency SLIs with Prometheus: required metrics, ratio queries, histograms, multi-window recording rules and remaining budget.

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.

Related articles

Time-based vs event-based SLO: understanding, computing and choosing (with the Qonto example)

Differences between time-based SLOs (time slices, uptime) and event-based SLOs (good events over total): defi…

Chaos engineering: principles, tools and practice with SLOs

A complete guide to chaos engineering: principles, steady state and hypothesis, blast radius, link with SLOs …

SRE: SLI, SLO and SLA explained simply

The fundamentals of Site Reliability Engineering (SRE): the difference between SLI, SLO and SLA, choosing ind…

Keep learning

Support us

♥ SUPPORT US

Support RousselTM Learning

Courses, articles, labs and certificates stay free with no intrusive ads. A voluntary contribution, of any amount you choose, pays for hosting, lab environments and new content.

Contribute with PayPal Any amount · card or PayPal accountLearn more
QR code PayPal
Or scan this QR code with your phone

Thank you, every contribution counts!