Prometheus & Grafana Cheatsheet
Essential PromQL queries, metric types (Counter, Gauge, Histogram), cardinality traps, relabeling order, and SLO burn-rate alerting.
TL;DR
Prometheus pulls /metrics endpoints and stores time series keyed by metric name plus label set. Use Counters with rate(), Histograms with histogram_quantile(), keep label cardinality bounded (never user IDs or raw paths), and always compute rate() before sum().
Metric types & PromQL
| Type | Behavior | Query pattern |
|---|---|---|
| Counter | Monotonic, resets to 0 on restart | rate(m[5m]) or increase(m[5m]) |
| Gauge | Arbitrary up/down value | Raw value, avg_over_time(m[5m]) |
| Histogram | _bucket, _sum, _count | histogram_quantile(0.99, sum by (le) (rate(m_bucket[5m]))) |
| Summary | Client-side quantiles | Cannot aggregate across instances |
# Error ratio across all instances (rate BEFORE sum)
sum(rate(http_requests_total{status=~"5.."}[5m]))
/ sum(rate(http_requests_total[5m]))
# p99 request latency per service from a Histogram
histogram_quantile(
0.99,
sum by (le, service) (rate(http_request_duration_seconds_bucket[5m]))
)
# Detect missing targets or silent metric disappearance
up == 0
absent(up{job="payment-service"})Keep your rate() range window at least 4× the scrape interval (for a 15s scrape interval, use [1m] minimum or [5m] in alerts) so temporary scrape jitter never produces empty graphs.
Relabeling & cardinality control
# Prometheus scrape config snippet (not run)
scrape_configs:
- job_name: kubernetes-pods
kubernetes_sd_configs:
- role: pod
relabel_configs: # runs BEFORE scrape (target discovery)
- source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_scrape]
action: keep
regex: "true"
metric_relabel_configs: # runs AFTER scrape (drop noisy series)
- source_labels: [__name__]
action: drop
regex: "go_gc_duration_seconds.*"Gotchas
- High-cardinality labels: Putting
user_id,request_id, or unnormalized paths (/users/12345instead of/users/:id) into labels multiplies time series exponentially and OOMs Prometheus. - Using
irate()in alerts:irate()only inspects the last two samples; userate()for alerting and dashboards so brief spikes do not page on noise. - Missing
absent()alerts: If a batch job or exporter stops reporting completely, threshold alerts (error_rate > 0.05) never fire because the series is gone.
Production checklist
- All HTTP/RPC latency metrics use Histograms with normalized route templates (
/orders/:id). - Recording rules pre-compute expensive
histogram_quantileand error-ratio expressions used by dashboards. - Alerts include a
runbook_urlannotation and page on symptom/SLO burn rate, not raw CPU spikes. - Grafana dashboards and datasources are provisioned as code (Git + Terraform/ConfigMaps), not hand-edited in the UI.
Check yourself
Check Yourself
1. You want to compute the p95 latency across 10 replicas of an API service. Which metric type and PromQL pattern works?
2. Which relabeling stage drops an unwanted high-cardinality metric name after Prometheus scrapes a target?
Sources
Related Notes
VPN & Private Access: WireGuard, Split-Horizon DNS & Zero Trust
Why network reachability is not authorization—how to combine WireGuard or IPsec VPNs, split-horizon DNS, bastions, and per-app OIDC without creating a flat internal trust zone.
ELK Logging: Pipelines, ILM & Log Hygiene
Centralize structured JSON logs with Filebeat/Fluent Bit, Elasticsearch data streams, Index Lifecycle Management (ILM), KQL queries, and PII redaction.