Skip to Content
Mengty Learns
Platform tools

Prometheus & Grafana Cheatsheet

Essential PromQL queries, metric types (Counter, Gauge, Histogram), cardinality traps, relabeling order, and SLO burn-rate alerting.

referenceintermediateTested with prometheus 3.16.0 (docs), grafana 13.3.2 (docs)Updated Nov 27, 2026

TL;DR

Prometheus pulls /metrics endpoints and stores time series keyed by metric name plus label set. Use Counters with rate(), Histograms with histogram_quantile(), keep label cardinality bounded (never user IDs or raw paths), and always compute rate() before sum().

Metric types & PromQL

TypeBehaviorQuery pattern
CounterMonotonic, resets to 0 on restartrate(m[5m]) or increase(m[5m])
GaugeArbitrary up/down valueRaw value, avg_over_time(m[5m])
Histogram_bucket, _sum, _counthistogram_quantile(0.99, sum by (le) (rate(m_bucket[5m])))
SummaryClient-side quantilesCannot aggregate across instances
# Error ratio across all instances (rate BEFORE sum)
sum(rate(http_requests_total{status=~"5.."}[5m]))
  / sum(rate(http_requests_total[5m]))

# p99 request latency per service from a Histogram
histogram_quantile(
  0.99,
  sum by (le, service) (rate(http_request_duration_seconds_bucket[5m]))
)

# Detect missing targets or silent metric disappearance
up == 0
absent(up{job="payment-service"})
Tip:

Keep your rate() range window at least 4× the scrape interval (for a 15s scrape interval, use [1m] minimum or [5m] in alerts) so temporary scrape jitter never produces empty graphs.

Relabeling & cardinality control

# Prometheus scrape config snippet (not run)
scrape_configs:
  - job_name: kubernetes-pods
    kubernetes_sd_configs:
      - role: pod
    relabel_configs:           # runs BEFORE scrape (target discovery)
      - source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_scrape]
        action: keep
        regex: "true"
    metric_relabel_configs:    # runs AFTER scrape (drop noisy series)
      - source_labels: [__name__]
        action: drop
        regex: "go_gc_duration_seconds.*"

Gotchas

  • High-cardinality labels: Putting user_id, request_id, or unnormalized paths (/users/12345 instead of /users/:id) into labels multiplies time series exponentially and OOMs Prometheus.
  • Using irate() in alerts: irate() only inspects the last two samples; use rate() for alerting and dashboards so brief spikes do not page on noise.
  • Missing absent() alerts: If a batch job or exporter stops reporting completely, threshold alerts (error_rate > 0.05) never fire because the series is gone.

Production checklist

  • All HTTP/RPC latency metrics use Histograms with normalized route templates (/orders/:id).
  • Recording rules pre-compute expensive histogram_quantile and error-ratio expressions used by dashboards.
  • Alerts include a runbook_url annotation and page on symptom/SLO burn rate, not raw CPU spikes.
  • Grafana dashboards and datasources are provisioned as code (Git + Terraform/ConfigMaps), not hand-edited in the UI.

Check yourself

Check Yourself

1. You want to compute the p95 latency across 10 replicas of an API service. Which metric type and PromQL pattern works?

2. Which relabeling stage drops an unwanted high-cardinality metric name after Prometheus scrapes a target?

Sources