乐闻世界logo
搜索文章和话题

What are the differences between Prometheus Recording Rules and Alerting Rules?

2月21日 15:41

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:

yaml
groups: - 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:

yaml
groups: - 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:

FeatureRecording RulesAlerting Rules
PurposePre-calculate query resultsTrigger alert notifications
StorageGenerate new time seriesDo not store new series
PerformanceImprove query performanceMay increase evaluation overhead
UsageDashboards, queriesMonitoring, alerting

Best Practices:

  1. Recording Rules:

    • Use meaningful naming conventions
    • Set reasonable evaluation intervals
    • Regularly review and clean up unused rules
    • Use by clause for grouping
  2. Alerting Rules:

    • Set reasonable for parameters to avoid false positives
    • Use tiered alerts (info, warning, critical)
    • Add clear description information
    • Use labels for grouping and routing
  3. Rule Management:

    • Use version control for rule files
    • Use promtool to check rule syntax
    • Test rules before deployment
    • Monitor rule evaluation performance

Validate Rules:

bash
promtool check rules /path/to/rules.yml
标签:Prometheus