Grafana Cheat Sheet
Reference for Grafana dashboard JSON, data source provisioning, panel queries, and alerting configuration for visualizing metrics.
Data Source Provisioning
YAML file provisioning a Prometheus data source on startup.
apiVersion: 1datasources: - name: Prometheus type: prometheus access: proxy url: http://prometheus:9090 isDefault: true editable: false
Dashboard Provisioning
Auto-load dashboard JSON files from a directory.
apiVersion: 1providers: - name: 'default' orgId: 1 folder: '' type: file options: path: /var/lib/grafana/dashboards
Key Concepts
Core Grafana building blocks.
- data source- Connection to a backend (Prometheus, Loki, InfluxDB, MySQL) that panels query
- panel- A single visualization (graph, table, gauge, stat) bound to a query
- dashboard variable- Templated input (e.g. $instance) letting users filter panels dynamically
- alert rule- Condition evaluated on a query result that triggers notifications
- contact point- Destination (Slack, email, PagerDuty) that receives firing alerts
- folder- Organizational grouping of dashboards with shared permissions
grafana-cli & HTTP API
Managing plugins and dashboards via CLI/API.
grafana-cli plugins install grafana-piechart-panelgrafana-cli plugins ls# Create/update a dashboard via HTTP APIcurl -X POST http://admin:admin@localhost:3000/api/dashboards/db \ -H "Content-Type: application/json" \ -d @dashboard.json
Unified Alerting Rule (Provisioned YAML)
A file-provisioned alert rule group evaluating a Prometheus expression through a threshold expression.
apiVersion: 1groups: - orgId: 1 name: platform-alerts folder: SRE interval: 1m rules: - uid: high-error-rate title: High error rate condition: C data: - refId: A datasourceUid: prometheus model: expr: sum(rate(http_requests_total{status=~"5.."}[5m])) / sum(rate(http_requests_total[5m])) - refId: C datasourceUid: __expr__ model: type: threshold expression: A conditions: - evaluator: type: gt params: [0.05] for: 5m labels: severity: critical annotations: summary: Error rate above 5% for 5 minutes
Chained Template Variables
Dashboard JSON defining a namespace variable that filters a dependent pod variable via a regex.
{ "templating": { "list": [ { "name": "namespace", "type": "query", "datasource": "Prometheus", "query": "label_values(kube_pod_info, namespace)", "refresh": 2, "multi": true, "includeAll": true }, { "name": "pod", "type": "query", "datasource": "Prometheus", "query": "label_values(kube_pod_info{namespace=~\"$namespace\"}, pod)", "regex": "/^(?!.*-canary).*/", "refresh": 2 } ] }}
Panel Query Transformations
Chained transformations that join two queries by field and hide the raw time column before rendering.
{ "transformations": [ { "id": "joinByField", "options": { "byField": "instance", "mode": "outer" } }, { "id": "reduce", "options": { "reducers": ["last"] } }, { "id": "organize", "options": { "excludeByName": { "Time": true } } } ]}
Advanced Concepts
Terminology beyond basic dashboards and panels.
- recording rule- A Prometheus-side (or Grafana-managed) precomputed query stored as a new time series to speed up expensive dashboard queries
- notification policy- Tree of label matchers routing firing alerts to specific contact points, with grouping and mute timings
- exemplar- A trace ID attached to a histogram bucket sample, enabling jump-from-metric-to-trace navigation
- mixin- A reusable, versioned bundle of dashboards + alert rules (often written in Jsonnet) shared across teams
- LogQL- Loki's query language combining a log stream selector with line filters and metric aggregations
- RBAC role- Fine-grained permission (fixed or custom) assignable to teams, separate from the coarser Viewer/Editor/Admin org roles
- flapping detection- Unified alerting suppresses repeated pending/firing transitions to avoid notification storms on unstable queries
Grafana as Code (Terraform)
Provisioning a data source, folder, and alert rule group with the Grafana Terraform provider.
resource "grafana_data_source" "prometheus" { type = "prometheus" name = "prometheus" url = "http://prometheus:9090"}resource "grafana_folder" "sre" { title = "SRE"}resource "grafana_rule_group" "alerts" { name = "platform-alerts" folder_uid = grafana_folder.sre.uid interval_seconds = 60 rule { name = "high-error-rate" condition = "C" for = "5m" data { ref_id = "A" datasource_uid = grafana_data_source.prometheus.uid model = jsonencode({ expr = "sum(rate(http_requests_total{status=~\"5..\"}[5m]))" }) } }}
Store dashboards as JSON in version control and load them via the file provisioner rather than editing in the UI — this makes dashboard changes reviewable and reproducible across environments.