PromQL (Prometheus Query Language)
PromQL query follows three key steps
- Metric Selection: Choose a metric name to query.
- Label Filtering: Add label selectors to narrow your results.
- Time window selection: Use a time range to focus your query.
Example: rate(http_requests_total[5m])
In Prometheus’s expression language, an expression or sub-expression can evaluate to one of four types:
- Instant vector - a set of time series containing a single sample for each time series, all sharing the same timestamp
- Range vector - a set of time series containing a range of data points over time for each time series
- Scalar - a simple numeric floating point value
- String - a simple string value; currently unused/rarely used.
Prometheus metric types
-
Gauge: A gauge is a metric that represents a single numerical value that can arbitrarily go up and down.
Ex: avg_over_time(cpu_usage_percent[5m])
- Counter: A counter is a cumulative metric that represents a single monotonically increasing counter whose value can only increase or be reset to zero on restart.
- Use rate() or increase().
- rate() and increase() are designed for counters, not gauges.
Ex: rate(http_requests_total[5m])
-
Histogram: A histogram samples observations (usually things like request durations or response sizes) and counts them in configurable buckets. It also provides a sum of all observed values.
Ex: http_request_duration_seconds_bucket
- Summary: summary samples observations (usually things like request durations and response sizes). While it also provides a total count of observations and a sum of all observed values, it calculates configurable quantiles over a sliding time window.
Ex: response_time_seconds_99th_99thpercentile
Time based Functions
Functions like rate(), irate(), and increase() turn counter data into powerful indicators of system behavior. Use rate() for smooth trend visualization, irate() for real-time alerting, and increase() for measuring absolute change.
Counters tell you the gradual increase (Like distance travelled etc) and rate helps converting total trends (Speed movement etc)
PromQL’s time-based targeting functions: rate(), irate(), and increase().
- rate(): Calculates the per-second average rate of increase of the time series in the range vector.
- Ex: rate(http_requests_total{job=”api-server”}[5m]) per-second average rate of HTTP requests over the last 5 minutes, per time series
- irate(): (Also called instant rate) Alerting on sudden spikes. Calculates the per-second instant rate of increase of the time series in the range vector. This is based on the last two data points.
- Ex: irate(http_requests_total{job=”api-server”}[5m]) returns the per-second rate of HTTP requests looking up to 5 minutes back for the two most recent data points, per time series.
- increase(): Calculates the increase in the time series in the range vector.
- Ex: increase(http_requests_total{job=”api-server”}[5m]) returns the number of HTTP requests as measured over the last 5 minutes, per time series.
Aggregation
Combines many individual time series into a single, unified reading using functions like sum().
Ex: sum(), avg(), count() etc
Grouping
Group time series by targetting the system.
Ex: by(), without(),