prometheus-grafana
Set up monitoring with Prometheus and Grafana: metrics, exporters, alerting rules, dashboards, and SLO tracking. Use for observability.
Prometheus & Grafana
Monitoring systems with Prometheus metrics and Grafana dashboards.
Quick Start
docker run -p 9090:9090 -v prometheus.yml:/etc/prometheus/prometheus.yml -d prom/prometheus
docker run -p 3000:3000 -d grafana/grafana
# Grafana: localhost:3000 admin/adminWhen to Use
- Collecting time-series metrics from services
- Dashboards for latency, errors, and saturation
- Alerting on thresholds and SLO burn
- Kubernetes and infra monitoring
Best Practices
Instrumentation
- Expose `/metrics` in Prometheus format
- Use counters (increasing), gauges, histograms
- Label metrics moderately (avoid cardinality explosion)
- Use client libraries for the language
Scraping & Storage
- Set sane scrape intervals (15-60s)
- Use `service_discovery` or static targets
- Limit retention and use TSDB best practices
- Configure recording rules for hot queries
Alerting
- Define alerts with PromQL thresholds
- Use Alertmanager for routing and dedup
- Alert on symptoms (SLOs), not just metrics
- Set for, `pending`, and proper severity
Dashboards
- Build focused dashboards per service
- Use RED (Rate, Errors, Duration) and USE patterns
- Add panels with meaningful units
- Track SLOs with burn-rate panels
Dependencies
# Python client
pip install prometheus-client
# or Node:
npm i prom-clientExamples
# prometheus.yml
global:
scrape_interval: 15s
scrape_configs:
- job_name: app
static_configs:
- targets: ["app:8080"]
rule_files:
- alerts.yml# alerts.yml
groups:
- name: app
rules:
- alert: HighErrorRate
expr: rate(http_requests_total{status=~"5.."}[5m]) / rate(http_requests_total[5m]) > 0.05
for: 10m
labels: { severity: critical }
annotations: { summary: "Error rate above 5%" }from prometheus_client import Counter, Histogram, start_http_server
import random, time
REQUESTS = Counter("http_requests_total", "Total HTTP requests", ["status"])
LATENCY = Histogram("http_request_duration_seconds", "Request latency")
start_http_server(8080)
while True:
REQUESTS.labels(status="200").inc()
with LATENCY.time():
time.sleep(random.uniform(0.01, 0.2))# PromQL: error ratio per service
sum(rate(http_requests_total{status=~"5.."}[5m]))
/ sum(rate(http_requests_total[5m]))Step-by-Step
1. Instrument services to expose `/metrics`.
2. Configure Prometheus scraping and storage.
3. Add alerting rules and Alertmanager.
4. Connect Grafana as the data source.
5. Build dashboards with RED/USE patterns.
6. Track SLOs with burn-rate alerts.
7. Secure with auth and TLS.
8. Monitor the monitor: alert on Prometheus itself.
Validation
1. `/metrics` exposed and scraped successfully
2. Dashboards show correct metrics and units
3. Alerts fire and route correctly
4. Cardinality stays within limits
5. Retention matches your needs
Troubleshooting
- Missing metrics: check the scrape target and path.
- High cardinality: reduce dynamic label values.
- No data in Grafana: verify the data source and time range.
Prometheus & Grafana (Прометей и Графана)
Мониторинг систем через метрики Prometheus и дашборды Grafana.
Быстрый старт
docker run -p 9090:9090 -v prometheus.yml:/etc/prometheus/prometheus.yml -d prom/prometheus
docker run -p 3000:3000 -d grafana/grafana
# Grafana: localhost:3000 admin/adminКогда использовать
- Сбор time-series метрик с сервисов
- Дашборды задержек, ошибок и насыщения
- Алерты по порогам и SLO burn
- Мониторинг Kubernetes и инфраструктуры
Лучшие практики
Инструментация
- Отдавайте `/metrics` в формате Prometheus
- Counters (растущие), gauges, histograms
- Метки умеренно (без cardinality explosion)
- Клиентские библиотеки под язык
Scrape и хранение
- Разумные интервалы (15-60s)
- `service_discovery` или статические targets
- Ограничьте retention; используйте best practices TSDB
- Recording rules для горячих запросов
Алерты
- Правила с порогами PromQL
- Alertmanager для маршрутизации и дедупликации
- Алертьте на симптомы (SLO), а не только метрики
- `for`, pending и корректная серьёзность
Дашборды
- Сфокусированные дашборды на сервис
- Паттерны RED (Rate, Errors, Duration) и USE
- Панели с понятными единицами
- SLO-трекинг через burn-rate панели
Зависимости
# Python клиент
pip install prometheus-client
# или Node:
npm i prom-clientПримеры
# prometheus.yml
global:
scrape_interval: 15s
scrape_configs:
- job_name: app
static_configs:
- targets: ["app:8080"]
rule_files:
- alerts.yml# alerts.yml
groups:
- name: app
rules:
- alert: HighErrorRate
expr: rate(http_requests_total{status=~"5.."}[5m]) / rate(http_requests_total[5m]) > 0.05
for: 10m
labels: { severity: critical }
annotations: { summary: "Error rate above 5%" }from prometheus_client import Counter, Histogram, start_http_server
import random, time
REQUESTS = Counter("http_requests_total", "Total HTTP requests", ["status"])
LATENCY = Histogram("http_request_duration_seconds", "Request latency")
start_http_server(8080)
while True:
REQUESTS.labels(status="200").inc()
with LATENCY.time():
time.sleep(random.uniform(0.01, 0.2))# PromQL: доля ошибок по сервису
sum(rate(http_requests_total{status=~"5.."}[5m]))
/ sum(rate(http_requests_total[5m]))Пошаговое руководство
1. Инструментируйте сервисы: отдавайте `/metrics`.
2. Настройте scrape и хранение Prometheus.
3. Добавьте правила алертов и Alertmanager.
4. Подключите Grafana как источник данных.
5. Соберите дашборды по RED/USE паттернам.
6. Трекайте SLO через burn-rate алерты.
7. Обеспечьте auth и TLS.
8. Мониторьте сам монитор: алерты на сам Prometheus.
Валидация
1. `/metrics` отдаётся и скрейпится успешно
2. Дашборды показывают корректные метрики и единицы
3. Алерты срабатывают и маршрутизируются корректно
4. Кардинальность в пределах лимитов
5. Retention соответствует потребностям
Устранение неполадок
- Метрик нет: проверьте scrape target и путь.
- Высокая кардинальность: уменьшайте динамические значения меток.
- Нет данных в Grafana: проверьте источник данных и временной диапазон.