Aug 5, 2021

[Prometheus] PromQL Basics Notes

What is PromQL?

PromQL (Prometheus Query Language) is the query language for retrieving and aggregating data in Prometheus.

Data types

Prometheus supports four data types (see Expression language data types):

Instant vectorA set of time series at a single timestamp
Range vectorA set of time series over a time range
ScalarA single numeric value
StringA string value

Label matchers

Prometheus stores time series with labels. By specifying labels you can select specific series.

For example, to select only those with job="prometheus" for metric http_requests_total:

http_requests_total{job="prometheus"}

Available operators:

=equals
!=not equals
=~regex match
!~regex not match

Metric types

Prometheus metrics come in four types:

GaugeGauge
CounterCounter
HistogramHistogram
SummarySummary

For differences between Histogram and Summary, see HISTOGRAMS AND SUMMARIES. For latency calculation details, see this post (Japanese).

Aggregation

Use aggregation operators to aggregate vectors, e.g.:

  • sum
  • min
  • max
  • avg

Example: total HTTP requests

sum(http_requests_total)

Reference: Aggregation operators

Functions

PromQL provides many functions over time series. Functions accept either an instant vector or a range vector; the docs specify the type.

Examples:

  • absent(v instant-vector) checks for the absence of elements.
  • absent_over_time(v range-vector) checks absence over a range.
  • exp(v instant-vector) computes the exponential.

See the official docs for more: functions.

rate / irate

rate and irate compute the per‑second increase of counter metrics.

rate(v range-vector) Average increase rate for slowly changing counters.

irate(v range-vector) Instantaneous rate for fast‑changing counters. Use this when spikes make rate hard to observe.

Counter reset Counters may reset on restart. When using rate or irate, resets are handled automatically. When aggregating, build on rate~/~irate to avoid reset artifacts. Example with sum:

sum(rate(http_request_total[5m]))

overtime aggregation functions

Functions of the form <aggregation>_over_time(range-vector) aggregate over time and return an instant vector.

avg_over_time(range-vector)Average over the range
min_over_time(range-vector)Minimum over the range
max_over_time(range-vector)Maximum over the range
sum_over_time(range-vector)Sum over the range

References

Official Prometheus docs