Write Effective PromQL Queries for Grafana
Intermediatev1.0.0
Master PromQL for Grafana dashboards — rate calculations, histogram quantiles, label filtering, aggregations, and common patterns for monitoring services.
Content
Overview
PromQL (Prometheus Query Language) is the primary query language for Grafana dashboards backed by Prometheus. It enables powerful metric aggregation, filtering, and calculation for monitoring and alerting.
Why This Matters
- -Accurate metrics — proper PromQL gives correct rates, not misleading raw counters
- -Performance — efficient queries reduce Prometheus load
- -Alerting — PromQL powers Grafana alert rules
- -Debugging — slice and dice metrics to isolate issues
How It Works
Step 1: Request Rate (RED - Rate)
Step 2: Error Rate (RED - Errors)
Step 3: Latency (RED - Duration)
Step 4: Resource Utilization (USE)
Step 5: Advanced Patterns
Best Practices
- -Always use rate() on counters, never display raw counter values
- -Use sum by (label) for aggregation with label preservation
- -Use [5m] range for rate() in dashboards (smooth, readable)
- -Use [1m] range for rate() in alerts (faster detection)
- -Label filter early in queries to reduce Prometheus processing
- -Use recording rules for expensive queries used by multiple dashboards
Common Mistakes
- -Displaying raw counters without rate() (ever-increasing line)
- -Using irate() in dashboards (too volatile, use rate() instead)
- -Not filtering by job/service (aggregating unrelated metrics)
- -histogram_quantile without rate() inside (incorrect values)
- -Missing sum by (le) in histogram_quantile (wrong aggregation)
FAQ
Discussion
Loading comments...