PromQL & Metrics Cheat Sheet
Quick reference guide for Prometheus & PromQL: Metric types, rate functions, selectors, and alert rules.
CI/CD & DevOps
prometheus
promql
metrics
PromQL (Prometheus Query Language) is a functional query language that lets you select and aggregate time series data in real-time.
Prometheus 4 Core Metric Types
Table
| Metric Type | Behavior | Common Example |
|---|---|---|
| Counter | Cumulative metric that only increases (resets to 0 on restart) | http_requests_total |
| Gauge | Value that can arbitrarily go up and down | node_memory_active_bytes, cpu_usage |
| Histogram | Samples observations (usually duration/size) into buckets | http_request_duration_seconds_bucket |
| Summary | Similar to Histogram, calculates configurable quantiles over time | rpc_duration_seconds{quantile="0.95"} |
Essential PromQL Functions & Patterns
promql
# 1. Per-second rate of increase for Counters over 5m window
rate(http_requests_total[5m])
# 2. Total rate summed by HTTP status code
sum by (status) (rate(http_requests_total[5m]))
# 3. 95th percentile HTTP request latency (Histogram)
histogram_quantile(0.95, sum(rate(http_request_duration_seconds_bucket[5m])) by (le))
# 4. Percentage of free memory (Gauges)
(node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes) * 100
# 5. Total count increase over past 1 hour
increase(errors_total[1h])
Prometheus Alert Rule Definition Example
yaml
groups:
- name: HighErrorRateAlerts
rules:
- alert: HighHttpErrorRate
expr: sum(rate(http_requests_total{status=~"5.."}[5m])) / sum(rate(http_requests_total[5m])) > 0.05
for: 5m
labels:
severity: critical
annotations:
summary: "High HTTP 5xx error rate detected on {{ $labels.instance }}"
description: "Error rate is currently {{ $value | humanizePercentage }}."
Common Pitfalls & Tips
[!WARNING] Never apply
rate()orincrease()functions to Gauge metrics!rate()expects monotonically increasing Counters and handles counter resets automatically.
[!TIP] Use label matching operators like
=~(regex match) and!~(regex non-match) to filter multiple services easily (e.g.job=~"api|web").