Prometheus Rule Evaluation Error Fix
In this tutorial, you'll learn about Prometheus Rule Evaluation Error Fix. We cover key concepts, practical examples, and best practices.
Prometheus logs Error reading rules or Error evaluating rule — the rule file has a syntax error, references a missing metric, or uses an invalid PromQL expression.
The Problem
# WRONG — missing metric name in the rule
groups:
- name: example
rules:
- alert: HighCPUUsage
expr: avg(rate(cpu_seconds[5m])) > 0.8
Error evaluating rule "HighCPUUsage": found no matching metrics
The metric cpu_seconds doesn't exist. The correct metric name is node_cpu_seconds_total. Prometheus evaluates the expression, finds no data, and logs an error for every evaluation cycle.
Step-by-Step Fix
1. Validate rule syntax
# Check Prometheus config
promtool check rules /etc/prometheus/rules/*.yml
# Check config including rules
promtool check config /etc/prometheus/prometheus.yml
2. Fix the PromQL expression
groups:
- name: node_alerts
rules:
- alert: HighCPUUsage
expr: avg(rate(node_cpu_seconds_total{mode="user"}[5m])) > 0.8
for: 5m
labels:
severity: warning
annotations:
summary: "High CPU usage on {{ $labels.instance }}"
3. Handle missing metrics with absent()
rules:
- alert: InstanceDown
expr: up == 0
for: 1m
annotations:
summary: "Instance {{ $labels.instance }} is down"
- alert: NoMetrics
expr: absent(up)
for: 5m
annotations:
summary: "No metrics received at all"
4. Use recording rules for complex expressions
groups:
- name: node_recording
rules:
- record: node:cpu:rate5m
expr: rate(node_cpu_seconds_total[5m])
- name: node_alerts
rules:
- alert: HighCPUUsage
expr: avg(node:cpu:rate5m{mode="user"}) > 0.8
for: 5m
5. Catch evaluation errors in the API
curl http://localhost:9090/api/v1/rules
Expected output from promtool check rules:
Checking rules/node-rules.yml
SUCCESS: 5 rules found
Prevention Tips
- Run
promtool check rulesin CI before deploying rule changes - Use recording rules for complex PromQL to simplify alert rules
- Test expressions in the Prometheus UI's Graph tab before writing rules
- Validate metric names with
curl http://<a href="/devops/prometheus-grafana/">prometheus</a>:9090/api/v1/label/__name__/values - Add
absent()alerts to detect missing metrics
Common Mistakes with rule evaluation
- Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
- Using
returnto exit a function early instead of wrapping a pure value in the monad - Mixing let bindings with <- bindings in do notation, producing type errors
These mistakes appear frequently in real-world PROMETHEUS code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro