Dashboards as Code
Provisioning data sources and dashboards from the repo
A dashboard that only exists in Grafana's database has the same problems as a server configured by hand: nobody knows who changed what, it can't be reviewed, and when the volume is lost, it's gone. Provisioning makes Grafana load data sources and dashboards from files at startup, so the repo is the source of truth.
How this stack does it
grafana/
├── provisioning/ # mounted at /etc/grafana/provisioning
│ ├── datasources/datasources.yml # Prometheus + Alertmanager
│ └── dashboards/dashboards.yml # "load JSON files from this folder"
└── dashboards/ # mounted at /var/lib/grafana/dashboards
├── Services/demo-app-red.json
├── Platform/stack-health.json
└── Databases/postgres-overview.jsonWipe Grafana completely with docker volume rm grafana_data, restart it,
and everything comes back identical. That's the test of a provisioned
setup.
Data source provisioning
apiVersion: 1
prune: true # delete data sources not in this file
datasources:
- name: Prometheus
type: prometheus
uid: prometheus # dashboards reference this, never the name
url: http://prometheus:9090
access: proxy
isDefault: true
editable: false
jsonData:
timeInterval: 15s
httpMethod: POSTThe fixed uid is the important bit. Dashboard JSON refers to its data
source as {"type": "prometheus", "uid": "prometheus"}. If Grafana
generated a random uid instead, every exported dashboard would break when
imported anywhere else.
Secrets don't belong in the file. Grafana expands environment variables,
so basicAuthPassword: $PROM_PASSWORD under secureJsonData reads the
value from the container's environment at startup.
Dashboard provisioning
apiVersion: 1
providers:
- name: learning
type: file
allowUiUpdates: true # can edit in the UI (changes are temporary)
disableDeletion: true # can't delete provisioned dashboards from the UI
updateIntervalSeconds: 30 # re-read files every 30s
options:
path: /var/lib/grafana/dashboards
foldersFromFilesStructure: true # Services/ -> "Services" folderGrafana re-reads the folder every 30 seconds, so editing a JSON file in the repo updates the live dashboard without a restart.
With allowUiUpdates: true, you can save changes in the UI, and
they'll appear to stick, until the JSON file changes on disk and
Grafana reloads it, silently discarding your edit. The workflow is
always: edit in the UI, export the JSON, commit it. Some teams set
allowUiUpdates: false so nobody is tempted.
The edit → export → commit loop
- Edit the dashboard in the UI until it's right.
- Dashboard settings → JSON Model, or Export → Export as JSON.
Leave "Export for sharing externally" off. That option replaces
data source references with
${DS_PROMETHEUS}input placeholders, which file provisioning can't fill in. - Paste it over the file in
grafana/dashboards/.... - Check the diff before committing. Look for
"datasource"entries with random uids, and for the"id"field, which should benullor absent so it doesn't clash across instances. The"uid"must stay fixed, because URLs and links use it. - Commit.
Generating dashboards
Hand-editing large JSON files gets painful. Teams with many dashboards generate them:
| Tool | Approach |
|---|---|
| Grafonnet (Jsonnet) | The long-standing standard; used by kube-prometheus mixins |
| Grafana Foundation SDK | Typed builders in Go, TypeScript, Python, Java |
Terraform grafana provider | Dashboards, folders, data sources, alerts, permissions as Terraform resources |
| Plain scripts | Templating JSON from Python. The dashboards in this repo were generated this way. |
Monitoring mixins bundle dashboards, recording rules, and alerts
for a piece of software in one Jsonnet package: node-mixin,
kubernetes-mixin, prometheus-mixin. They encode years of experience
about what to watch. For any widely used software, check for a mixin
before building your own.
Importing community dashboards
grafana.com hosts thousands of dashboards. Import one by ID (Dashboards → New → Import), for example "Node Exporter Full". They're a great way to discover which metrics an exporter has. But treat them as starting points: they're often huge, slow, and built for someone else's labels. Copy the three panels you need into your own dashboard rather than running a 100-panel import in production.
In production, run Grafana with an external database (Postgres or MySQL) instead of the default SQLite file, so you can run several Grafana replicas behind a load balancer. With everything provisioned, that database only holds users, sessions, and preferences, so losing it is an inconvenience, not a disaster.
Lab
- Export the node dashboard you built in Data Sources & Panels
to
grafana/dashboards/Platform/node.json. Set a fixed"uid": "node"and remove"id". - Wait 30 seconds. It appears in the Platform folder as provisioned.
- Edit the title in the JSON file and save. Watch Grafana pick it up.
- Prove it's reproducible:
docker compose -f compose_prod.yaml rm -sf grafana && docker volume rm grafana_data && docker compose -f compose_prod.yaml up -d grafana. Every dashboard and data source is back.