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 values
  • avg — average of values
  • min / max — minimum / maximum
  • count — count of elements
  • topk(k, ...) — top k elements by value
  • bottomk(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 value
  • ceil() / floor() — round up / down
  • clamp_min() / clamp_max() — clamp values
  • label_replace() — modify labels
  • predict_linear() — linear prediction
  • time() — current Unix timestamp
  • vector() — scalar to vector conversion

Key Exam Tips

  1. rate() vs irate(): rate() gives a smoothed average, irate() uses only the last two points. Use rate() for alerting and irate() for dashboards.
  2. Counter resets: rate() and increase() handle counter resets automatically.
  3. Staleness: Prometheus marks a time series stale 5 minutes after the last sample.
  4. Subqueries: You can nest instant vector queries inside range selectors: rate(http_requests_total[5m])[1h:1m].