1---
2name: prometheus
3description: Prometheus monitoring patterns, cardinality management, alerting best practices, and PromQL traps.
4metadata:
5 source: https://skillmd.com/skills/modbender/prometheus
6 category: infrastructure
7 skills: ["prometheus", "monitoring", "alerting", "metrics", "observability"]
8---
9
10## Cardinality Explosions
11
12- Every unique label combination creates a new time series — `user_id` as label kills Prometheus
13- Avoid high-cardinality labels: user IDs, email addresses, request IDs, timestamps, UUIDs
14- Check cardinality: `prometheus_tsdb_head_series` metric — above 1M series needs attention
15- Use histograms for latency, not per-request labels — buckets are fixed cardinality
16- Relabeling can drop dangerous labels before ingestion: `labeldrop` in scrape config
17
18## Histogram vs Summary
19
20- Histograms: use for SLOs, aggregatable across instances, buckets defined upfront
21- Summaries: use when you need exact percentiles, cannot aggregate across instances
22- Histogram bucket boundaries must be defined before data arrives — wrong buckets = wrong percentiles
23- Default buckets (.005, .01, .025, .05, .1, .25, .5, 1, 2.5, 5, 10) assume HTTP latency — adjust for your use case
24
25## Rate and Increase
26
27- `rate()` requires range selector at least 4x scrape interval — `rate(metric[1m])` with 30s scrape misses data
28- `rate()` is per-second, `increase()` is total over range — don't confuse them
29- Counter resets on restart — `rate()` handles this, raw delta doesn't
30- `irate()` uses only last two samples — too spiky for alerting, use `rate()` for alerts
31
32## Alerting Mistakes
33
34- Alert on symptoms, not causes — "high latency" not "high CPU"
35- `for` clause prevents flapping: `for: 5m` means condition must hold 5 minutes before firing
36- Missing `for` clause = fires immediately on first match = noisy
37- Alerts need `runbook_url` label — on-call needs to know what to do, not just that something's wrong
38- Test alerts with `promtool check rules` — syntax errors discovered at 3am are bad
39
40## PromQL Traps
41
42- `and` is intersection by labels, not boolean AND — results must have matching label sets
43- `or` fills in missing series, doesn't do boolean OR on values
44- `{}` without metric name is expensive — scans all metrics
45- `offset` goes back in time: `metric offset 1h` is value from 1 hour ago
46- Comparison operators filter series: `http_requests > 100` drops series below 100, doesn't return boolean
47
48## Scrape Configuration
49
50- `honor_labels: true` trusts source labels — use only when source is authoritative (e.g., Pushgateway)
51- `scrape_timeout` must be less than `scrape_interval` — otherwise overlapping scrapes
52- Static configs don't reload without restart — use file_sd or service discovery for dynamic targets
53- TLS verification disabled (`insecure_skip_verify`) should be temporary, never permanent
54
55## Pushgateway Pitfalls
56
57- Pushgateway is for batch jobs, not services — services should expose /metrics
58- Metrics persist until deleted — stale metrics from dead jobs confuse dashboards
59- Add job and instance labels to distinguish sources — default grouping hides failures
60- Delete metrics when job completes: `curl -X DELETE http://pushgateway/metrics/job/myjob`
61
62## Recording Rules
63
64- Pre-compute expensive queries: `record: job:request_duration_seconds:rate5m`
65- Naming convention: `level:metric:operations` — helps identify what rules produce
66- Recording rules update every evaluation interval — not instant, plan for slight delay
67- Reduce cardinality with recording rules: aggregate away labels you don't need for alerting
68
69## Federation and Remote Write
70
71- Federation for pulling from other Prometheus — use sparingly, adds latency
72- Remote write for long-term storage — Prometheus local storage is not durable
73- Remote write can buffer during outages — but buffer is finite, data loss on extended outages
74- Prometheus is not highly available by default — run two instances scraping same targets
75
76## Common Operational Issues
77
78- TSDB corruption on unclean shutdown — use `--storage.tsdb.wal-compression` and monitor disk space
79- Memory grows with series count — each series costs ~3KB RAM
80- Compaction pauses during high load — leave 40% disk headroom
81- Scrape targets stuck "Unknown" — check network, firewall, target actually exposing /metrics
82
83## Label Best Practices
84
85- Use labels for dimensions you'll filter/aggregate by — environment, service, instance
86- Keep label values low-cardinality — tens or hundreds, not thousands
87- Consistent naming: `snake_case`, prefix with domain: `http_requests_total`, `node_cpu_seconds_total`
88- `le` label is reserved for histogram buckets — don't use for other purposes