This lesson/article provides practical patterns you’ll use every day. It is not intended to be comprehensive Prometheus certification preparation.
Basic queries
The simplest PromQL expression is just the metric name. This returns all time series with that metric over the selected time range:Filtering with label selectors
To narrow results, use label selectors. Label selectors support exact matches, negative matches, and regular expressions:Metric types (quick reference)
Working with counters: rate() and range vectors
Counters are cumulative and always increase. To show a meaningful value, compute the rate of change over a lookback window usingrate():
- The expression
http_requests_total[5m]is a range vector. rate()returns an instant vector representing the per-second increase computed over the last 5 minutes for each time series.
Always use
rate() (or irate() when appropriate) for counters when you want a meaningful per-second rate. Plotting raw cumulative counters will produce unusable graphs.Aggregation
To aggregate across time series, use aggregation operators likesum, avg, min, max, etc. Aggregations are commonly wrapped around rate():
- Total request rate across all label dimensions:
- Request rate aggregated by
endpoint(keepsendpointlabel, drops others):
by(...), the aggregation removes all label dimensions and returns a single scalar per instant.
Arithmetic and SLIs
PromQL supports arithmetic on instant vectors. For example, compute a 2xx success rate percentage over the last 5 minutes:Histograms and percentiles
Histogram bucket series are cumulative counters representing counts of observations that fell into each bucket boundary. To compute per-bucket rates, applyrate() and then aggregate by the le (less-or-equal) label:

histogram_quantile() with the aggregated bucket rates:
histogram_quantile():
- It uses linear interpolation across bucket counts to approximate percentiles.
- Accuracy depends on your bucket boundaries and their density. You cannot recover exact values beyond the bucket granularity — only an approximation.
When designing buckets, choose boundaries that capture the SLO-relevant regions (e.g., many tight buckets around your target latency). This improves percentile accuracy where it matters.
Common PromQL functions and operators
Practical tips
- Use
rate()with a window appropriate for your traffic volume: high-volume services can use shorter windows (1m), low-volume may need longer windows (5m or 15m) to smooth spikes. - When aggregating across instances or jobs, explicitly control the label dimensions with
by(...)orwithout(...)to avoid accidental double-counting. - For alerting, prefer more conservative window sizes and consider
increase()for absolute counts over a time window.
Links and references
- Prometheus docs: https://prometheus.io/docs/
- PromQL reference: https://prometheus.io/docs/prometheus/latest/querying/basics/
- Grafana: https://grafana.com/