How do you compute an error ratio in PromQL?

BeginnerConceptPromQL & Recording Rules
Answer
sum(rate(http_requests_total{job="api",code=~"5.."}[5m]))
/
sum(rate(http_requests_total{job="api"}[5m]))

Both sides are aggregated to the same labels (none here), so the division matches. For per-service ratios, use sum by (service) on both sides. Multiply by 100 for percent only in the display, keep ratios 0–1 in rules.

All Monitoring & Observability questions Roadmap: Monitoring & alerting

More on PromQL & Recording Rules