How do you write an availability SLI recording rule in Prometheus?

IntermediateConceptSLIs, SLOs & SLAs
Answer

Record the error ratio (bad / total) per window, so alerts and dashboards reuse it cheaply:

groups:
  - name: checkout-sli
    rules:
      - record: slo:sli_error:ratio_rate5m
        labels:
          slo: checkout-availability
        expr: |
          sum(rate(http_requests_total{job="checkout",code=~"5.."}[5m]))
          /
          sum(rate(http_requests_total{job="checkout"}[5m]))

Repeat for 30m, 1h, 2h, 6h, 1d and 3d windows. The naming convention level:metric:operations makes it obvious what the series is.

All SRE & Reliability questions Roadmap: SRE & incident response

More on SLIs, SLOs & SLAs