PromQL (Prometheus Query Language)¶
π PromQL Basics - Query language fundamentals π PromQL Operators - Operators reference π PromQL Functions - Functions reference
Data Types¶
| Type | Description | Example |
|---|---|---|
| Instant Vector | Set of time series, each with one value at a given time | http_requests_total{status="200"} |
| Range Vector | Set of time series, each with a range of values over time | http_requests_total[5m] |
| Scalar | Simple numeric floating-point value | 42 or 3.14 |
| String | Simple string value (rarely used) | "hello" |
Key Distinction: - Instant vectors return the most recent value for each time series - Range vectors return all values within a time window - Many functions require range vectors (e.g., rate, increase, avg_over_time) - Grafana panels typically display instant vector results
Selectors and Matchers¶
Label Matchers¶
| Matcher | Description | Example |
|---|---|---|
= | Exact equality | {job="api"} |
!= | Not equal | {status!="200"} |
=~ | Regex match | {method=~"GET\|POST"} |
!~ | Negative regex | {instance!~"test.*"} |
Time Selection¶
- Range:
metric[5m]- last 5 minutes of data - Offset:
metric offset 1h- data from 1 hour ago - At:
metric @ 1609459200- data at specific Unix timestamp - Duration units:
s(seconds),m(minutes),h(hours),d(days),w(weeks),y(years)
Rate Functions (For Counters)¶
rate()¶
- Per-second average rate of increase over a time range
- Handles counter resets automatically
- Best for: alerting rules, slow-moving counters, recording rules
rate(http_requests_total[5m])- average requests per second over 5 minutes
irate()¶
- Per-second instant rate using the last two data points in the range
- More responsive to spikes but noisier
- Best for: volatile, fast-moving counters on dashboards
irate(http_requests_total[5m])- instant rate (uses only last 2 points from 5m window)
increase()¶
- Total increase over a time range
- Equivalent to
rate() * seconds_in_range - Handles counter resets automatically
increase(http_requests_total[1h])- total requests in the last hour
When to Use Each¶
| Function | Smoothness | Responsiveness | Best For |
|---|---|---|---|
rate() | Smooth | Averaged | Alerting, recording rules |
irate() | Noisy | Instant | Dashboards showing spikes |
increase() | Smooth | Averaged | Total counts over time periods |
Important: All three require range vectors and are designed for counters. Using them on gauges produces incorrect results.
Aggregation Operators¶
Basic Aggregations¶
| Operator | Description | Example |
|---|---|---|
sum | Sum of all values | sum(rate(http_requests_total[5m])) |
avg | Average value | avg(node_cpu_seconds_total) |
min | Minimum value | min(node_memory_MemAvailable_bytes) |
max | Maximum value | max(node_cpu_seconds_total) |
count | Number of time series | count(up) |
stddev | Standard deviation | stddev(rate(http_requests_total[5m])) |
stdvar | Standard variance | stdvar(rate(http_requests_total[5m])) |
Grouping Aggregations¶
| Operator | Description | Example |
|---|---|---|
topk | Top K elements by value | topk(5, rate(http_requests_total[5m])) |
bottomk | Bottom K elements by value | bottomk(3, node_memory_MemFree_bytes) |
count_values | Count series by value | count_values("version", build_info) |
quantile | Calculate quantile | quantile(0.95, rate(http_requests_total[5m])) |
by and without Clauses¶
by (label)- Keep only specified labels in the resultwithout (label)- Remove specified labels from the resultsum by (method)(rate(http_requests_total[5m]))- sum per HTTP methodsum without (instance)(rate(http_requests_total[5m]))- sum across all instances
Binary Operators¶
Arithmetic¶
+(addition),-(subtraction),*(multiplication)/(division),%(modulo),^(exponentiation)
Comparison¶
==,!=,>,<,>=,<=- With
boolmodifier: returns 0 or 1 instead of filtering http_requests_total > 100- returns only series with value > 100http_requests_total > bool 100- returns 1 for true, 0 for false
Logical (set operations)¶
and- intersectionor- unionunless- complement (left side minus right side matches)
Vector Matching¶
- One-to-one: Default, matches series with identical labels
on(label)- Match only on specified labelsignoring(label)- Match ignoring specified labels- Many-to-one/one-to-many:
group_left(labels)orgroup_right(labels)
Key Functions¶
Histogram Functions¶
histogram_quantile(q, v)- Calculate the q-th quantile from histogram buckets- Must preserve the
lelabel:histogram_quantile(0.95, sum by (le)(rate(metric_bucket[5m]))) lemeans "less than or equal" - defines bucket boundaries
Gauge Functions¶
changes(v[t])- Number of times a gauge changed valuedelta(v[t])- Difference between first and last value in rangederiv(v[t])- Per-second derivative using linear regressionpredict_linear(v[t], s)- Predict value in s seconds using linear regressionavg_over_time(v[t])- Average value over time rangemin_over_time(v[t]),max_over_time(v[t])- Min/max over time range
Utility Functions¶
absent(v)- Returns 1 if the vector has no elements (useful for "is this metric missing?" alerts)absent_over_time(v[t])- Returns 1 if no samples exist in the rangelabel_replace(v, dst, replacement, src, regex)- Modify labelslabel_join(v, dst, separator, src1, src2, ...)- Join label valuessort(v),sort_desc(v)- Sort resultstime()- Current Unix timestampvector(s)- Convert scalar to vector
Recording Rules¶
Pre-compute expensive or frequently used queries and store results as new time series.
groups:
- name: http_rules
interval: 30s
rules:
- record: job:http_requests:rate5m
expr: sum by (job)(rate(http_requests_total[5m]))
- record: job:http_errors:rate5m
expr: sum by (job)(rate(http_requests_total{status=~"5.."}[5m]))
- record: job:http_error_ratio:rate5m
expr: job:http_errors:rate5m / job:http_requests:rate5m
Naming Convention: - level:metric:operations format - level = aggregation level (job, instance, etc.) - metric = original metric name - operations = list of operations applied (rate5m, sum, etc.)
Benefits: - Faster dashboard loading (pre-computed results) - Enables alerting on complex expressions - Reduces query-time load on Prometheus - Consistent results across dashboards and alerts
Common Query Patterns¶
Error Rate Percentage¶
sum(rate(http_requests_total{status=~"5.."}[5m]))
/
sum(rate(http_requests_total[5m]))
* 100
P95 Latency¶
histogram_quantile(0.95, sum by (le)(rate(http_request_duration_seconds_bucket[5m])))
Top 5 Pods by CPU¶
topk(5, sum by (pod)(rate(container_cpu_usage_seconds_total[5m])))
Disk Full Prediction (24 hours)¶
predict_linear(node_filesystem_avail_bytes[6h], 24*3600) < 0
Missing Metric Alert¶
absent(up{job="my-service"})