PromQL Basics
PromQL (Prometheus Query Language) is the primary way to query and analyze metrics stored in Prometheus. Understanding PromQL is essential for the PCA exam.
Data Types
PromQL has four value types:
- Instant vector — a set of time series with a single sample per series at a given timestamp
- Range vector — a set of time series with a range of samples over time
- Scalar — a simple numeric floating-point value
- String — a simple string value (rarely used)
Selectors
Instant Vector Selectors
Select the current value of a metric:
http_requests_total
Filter with label matchers:
http_requests_total{method="GET", status="200"}
Label Matching Operators
| Operator | Description |
|---|---|
= |
Exact equality |
!= |
Not equal |
=~ |
Regex match |
!~ |
Negative regex match |
Example with regex:
http_requests_total{method=~"GET|POST"}
Range Vector Selectors
Select samples over a time range using square brackets:
http_requests_total[5m]
This returns all samples for the last 5 minutes. Range vectors are used as input to functions like rate().
Operators
Arithmetic Operators
Standard arithmetic operators work between scalars and instant vectors:
node_memory_MemTotal_bytes - node_memory_MemAvailable_bytes
Comparison Operators
Filter results based on comparisons:
http_requests_total > 100
Aggregation Operators
Aggregate across label dimensions:
sum(rate(http_requests_total[5m])) by (method)
Common aggregation operators:
sum— sum of valuesavg— average of valuesmin/max— minimum / maximumcount— count of elementstopk(k, ...)— top k elements by valuebottomk(k, ...)— bottom k elements by value
Functions
rate()
Calculates the per-second average rate of increase for a counter over a time range:
rate(http_requests_total[5m])
This is the most commonly used function for counters. Always use rate() before aggregating counters.
irate()
Instant rate — calculates rate using the last two data points:
irate(http_requests_total[5m])
More responsive to changes but noisier than rate().
increase()
Returns the total increase in a counter over a time range:
increase(http_requests_total[1h])
histogram_quantile()
Calculate quantiles from histogram metrics:
histogram_quantile(0.95, rate(http_request_duration_seconds_bucket[5m]))
Other Important Functions
abs()— absolute valueceil()/floor()— round up / downclamp_min()/clamp_max()— clamp valueslabel_replace()— modify labelspredict_linear()— linear predictiontime()— current Unix timestampvector()— scalar to vector conversion
Key Exam Tips
- rate() vs irate():
rate()gives a smoothed average,irate()uses only the last two points. Userate()for alerting andirate()for dashboards. - Counter resets:
rate()andincrease()handle counter resets automatically. - Staleness: Prometheus marks a time series stale 5 minutes after the last sample.
- Subqueries: You can nest instant vector queries inside range selectors:
rate(http_requests_total[5m])[1h:1m].