PromQL (Prometheus Query Language) is a domain-specific query language that allows you to slice, aggregate, and calculate real-time trends over time-series data.
The 4 Core Metric Types
- Counter: A cumulative metric that can only increase or reset to zero on restart (e.g.
http_requests_total,errors_total). - Gauge: A value that goes up and down arbitrarily (e.g.
node_memory_MemAvailable_bytes,cpu_temperature). - Histogram: Samples observations in configurable buckets (e.g.
http_request_duration_seconds_bucket). - Summary: Similar to histogram, but calculates percentiles client-side.
Instant Vectors vs. Range Vectors
- Instant Vector: A set of time series containing a single sample for each time series, all at the same timestamp.
promqlhttp_requests_total{status="200"}
- Range Vector: A set of time series containing a range of samples over time.
promqlhttp_requests_total{status="200"}[5m]
[!IMPORTANT] You cannot directly graph a range vector. You must apply a rate or aggregation function (such as
rate(),avg_over_time(), orcount_over_time()) to convert a range vector into an instant vector for graphing!
rate() vs irate() vs increase()
When dealing with Counters, choosing the right function is crucial:
Example:
Calculating Percentiles with histogram_quantile()
Averages are dangerous in engineering. If 99 users experience 10ms latency and 1 user experiences 30,000ms latency, the average is ~310ms. You miss the critical outage experienced by that user.
Percentiles show real user experience:
- p50 (Median): 50% of users experience this latency or faster.
- p95: 95% of users experience this latency or faster.
- p99: 99% of users experience this latency or faster (catches tail latency).