Differences and usage of Prometheus Recording Rules and Alerting Rules:
Recording Rules:
- Pre-calculate and store common query results
- Improve query performance and reduce computational overhead
- Do not trigger alerts
Configuration Example:
yamlgroups: - name: api_recording_rules interval: 30s rules: - record: job:http_requests:rate5m expr: sum by (job) (rate(http_requests_total[5m])) - record: job:request_errors:rate5m expr: sum by (job) (rate(http_requests_total{status=~"5.."}[5m]))
Use Cases:
- Frequently queried complex expressions
- Calculations that aggregate multiple metrics
- Improve dashboard loading speed
- Reduce real-time query pressure
Alerting Rules:
- Monitor metrics and trigger alerts
- Support alert grouping, inhibition, and silencing
- Send notifications to Alertmanager
Configuration Example:
yamlgroups: - name: api_alerting_rules rules: - alert: HighErrorRate expr: job:request_errors:rate5m / job:http_requests:rate5m > 0.05 for: 5m labels: severity: critical annotations: summary: "High error rate on {{ $labels.job }}" description: "Error rate is {{ $value | humanizePercentage }}"
Key Differences:
| Feature | Recording Rules | Alerting Rules |
|---|---|---|
| Purpose | Pre-calculate query results | Trigger alert notifications |
| Storage | Generate new time series | Do not store new series |
| Performance | Improve query performance | May increase evaluation overhead |
| Usage | Dashboards, queries | Monitoring, alerting |
Best Practices:
-
Recording Rules:
- Use meaningful naming conventions
- Set reasonable evaluation intervals
- Regularly review and clean up unused rules
- Use
byclause for grouping
-
Alerting Rules:
- Set reasonable
forparameters to avoid false positives - Use tiered alerts (info, warning, critical)
- Add clear description information
- Use labels for grouping and routing
- Set reasonable
-
Rule Management:
- Use version control for rule files
- Use
promtoolto check rule syntax - Test rules before deployment
- Monitor rule evaluation performance
Validate Rules:
bashpromtool check rules /path/to/rules.yml