Prometheus and Grafana: Best Practices
Best practice here means the short list of habits that keep a monitoring setup useful instead of noisy. The list below is the whole lesson.
The PromQL cheat sheet: traffic
Requests per second is the traffic signal. Rate the counter, never graph it raw.
sum by (job) (rate(http_requests_total[5m]))The PromQL cheat sheet: errors
The error rate is failed requests over all requests. Five percent is a good first threshold.
sum by (job) (rate(http_requests_total{status=~"5.."}[5m]))
/ sum by (job) (rate(http_requests_total[5m]))The PromQL cheat sheet: latency
The p95 latency comes from the histogram buckets. Rate the buckets first, then take the quantile.
histogram_quantile(0.95, sum by (le) (rate(http_request_duration_seconds_bucket[5m])))The PromQL cheat sheet: saturation
The up metric is the fastest health check. One means the target answers, zero means it does not.
up{job="shopsentry"}Example
promtool check rules shopsentry-rules.ymlKey points
- Pin versions: run the same Prometheus, Grafana, and exporter versions everywhere.
- Label discipline: keep labels few, stable, and spelled the same in every service.
- Cardinality: the count of distinct series. Each new label value multiplies it.
- Cardinality budget: a rough ceiling on series, such as ten thousand per service to start.
- Never put a user id, an email, or a request id in a label. They explode the series count.
- Alert on symptoms, not causes. Page when users feel it, such as errors above 5 percent.
- A cause still gets a rule, but a quiet one, such as a warning or a dashboard panel.
- Test every alert with promtool before it ships, so an edit cannot break it silently.
- Destroy the lab when done, so no stray process keeps a port or a password on the machine.
In this example, promtool checks the capstone rule file and finds five rules: three recording rules and two alerting rules. The check runs before any rule ships, and it is the habit this lesson asks for. A rule file that passes here can still be wrong in logic, which is why the next lesson also tests the alerts with promtool test rules.
Note
A monitoring setup is judged by the alerts it does not send.
Try it Yourself
Edit the code, then press Run.
Running is simulated · Rust Playground
Exercise
Which of these belongs in a label on a metric series?