PromQL
PromQL (Prometheus Query Language) is the functional query language used to select, filter, aggregate, and calculate over metrics stored in Prometheus. It powers Grafana panels, alert rules, and ad hoc exploration in the Prometheus UI.
Core concepts
- Instant vector: one value per series at a point in time, such as
http_requests_total. - Range vector: a window of samples per series, such as
http_requests_total[5m]. Functions likerate()consume it. - Scalar: a single number.
and labels"] --> R["Range selector
[5m]"] R --> F["rate or increase"] F --> A["Aggregate
sum by pod"] classDef step fill:#DBEAFE,color:#1E293B classDef out fill:#D1FAE5,color:#1E293B class M,R,F step class A out
Building a query step by step
http_requests_totalhttp_requests_total{namespace="production", service="payment-api"}rate(http_requests_total{namespace="production"}[5m])sum(rate(http_requests_total{namespace="production"}[5m]))sum by (pod) (rate(http_requests_total{namespace="production"}[5m]))Label matchers: = exact, != not equal, =~ regex, !~ regex not match.
Functions you will use most
| Function | Use |
|---|---|
| rate() | Per-second rate of a counter |
| increase() | Total increase over a window |
| irate() | Instant rate from the last two samples |
| avg_over_time() | Average of a gauge over a window |
| topk() | Top N series |
| histogram_quantile() | Percentiles from histograms |
Production queries
# Error ratio per servicesum by (service) (rate(http_requests_total{status=~"5..", namespace="production"}[5m])) / sum by (service) (rate(http_requests_total{namespace="production"}[5m])) # p99 latency per servicehistogram_quantile(0.99, sum by (service, le) (rate(http_request_duration_seconds_bucket{namespace="production"}[5m]))) # Restarts in the last hourincrease(kube_pod_container_status_restarts_total{namespace="production"}[1h]) > 0 # Memory working set vs limitcontainer_memory_working_set_bytes{namespace="production"} / on(pod, container) kube_pod_container_resource_limits{resource="memory", namespace="production"}Common mistakes
- Querying a counter directly. Use
rate()to get throughput. - Using
irate()in alert rules. It is noisy, so preferrate(). - Summing without
by (...), which discards the label you need to find the culprit. - Using a range shorter than about four times the scrape interval.
Common MistakeAlerting on
container_memory_usage_bytes. It includes reclaimable cache. Usecontainer_memory_working_set_bytes, which is closer to what the kubelet uses for OOM and eviction decisions.
TipAlways filter by
namespacefirst. Without it, results mix all namespaces and thresholds become meaningless.
RememberPair alerts with a
for:duration so a single spike does not page anyone.
Frequently Asked Questions
How is PromQL different from SQL?
It is not relational. It works on time series identified by a metric name and labels, and has purpose-built functions such as rate and histogram_quantile for counters and histograms.
Why does rate() give nonsense on some metrics?
rate() expects a monotonically increasing counter and handles resets. On a gauge, which rises and falls, it produces meaningless values. Use avg_over_time or deriv for gauges.
Where do I test queries safely?
In the Prometheus UI Graph tab, or in Grafana's Explore view, before putting them into dashboards or alert rules.