Prometheus & Grafana: Beyond Basic Metrics
Writing custom exporters in Python, alerting on rates vs totals, and building dashboards that actually help during an incident.
Observability Engineering
CPU usage is a boring metric. It doesn't tell you if the user is happy. We need RED metrics: Rate, Errors, Duration.
1. Custom Exporters
Sometimes node_exporter isn't enough. You need business metrics.
Python Prometheus Client:
from prometheus_client import start_http_server, Gauge
import random
import time
REQUEST_TIME = Gauge('app_request_processing_seconds', 'Time spent processing request')
@REQUEST_TIME.time()
def process_request():
time.sleep(random.random())
2. PromQL Power Queries
Don't alert on static thresholds. Alert on burn rates. Predicting Disk Full in 4 hours:
predict_linear(node_filesystem_free_bytes[1h], 4 * 3600) < 0
3. Alerts that Actionable
Bad Alert: "High CPU on server-1". Good Alert: "High Error Rate (>5%) on Payment Service affecting Users".
Goal: Reduce Mean Time To Resolution (MTTR) by providing context in alerts.