Tags: prometheus, grafana, monitoring, metrics, observability, tsdb, dashboard
Level: intermediate
TL;DR: Prometheus scrapes and stores time-series metrics using PromQL; Grafana visualizes those metrics via customizable dashboards and alerts.
R: I drop unnecessary labels at scrape time using metric_relabel_configs, avoid putting unique IDs (like user IDs or raw IPs) in labels, and use recording rules to pre-aggregate heavy queries.
rate() and irate() in PromQL. R: rate() calculates the per-second average over the entire time window, good for slow-moving or alerting metrics. irate() calculates the rate based on the last two data points, better for spiky graphs and zooming in.
R: I configure Prometheus as a Data Source in Grafana, usually via internal network routing or using basic auth / TLS if crossing network boundaries, often provisioned via code (YAML/Terraform).
R: It's a way to pre-compute frequently needed or computationally expensive expressions and save the result as a new set of time series, speeding up Grafana dashboards.
irate() for alerting rules; it's too sensitive to brief spikes and causes false positives (use rate() instead).rate() or increase() which handle counter resets automatically.# Example Prometheus scrape config for Node Exporter
scrape_configs:
- job_name: 'node'
scrape_interval: 15s
static_configs:
- targets: ['localhost:9100']
Snippet di codice
# Calculate HTTP 5xx error rate percentage
sum(rate(http_requests_total{status=~"5.."}[5m]))
/
sum(rate(http_requests_total[5m])) * 100
Snippet di codice
# Node exporter: CPU usage percentage
100 - (avg by (instance) (rate(node_cpu_seconds_total{mode="idle"}[5m])) * 100)
[ ] PromQL subqueries and offsets (e.g., [5m:1m], offset 1w).
[ ] Alertmanager inhibition rules vs routing tree.
[ ] Setting up Grafana provisioning (dashboards as JSON, datasources as YAML).