Learn Labs
Grafana

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.json

Wipe 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: POST

The 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" folder

Grafana re-reads the folder every 30 seconds, so editing a JSON file in the repo updates the live dashboard without a restart.

The edit → export → commit loop

  1. Edit the dashboard in the UI until it's right.
  2. 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.
  3. Paste it over the file in grafana/dashboards/....
  4. Check the diff before committing. Look for "datasource" entries with random uids, and for the "id" field, which should be null or absent so it doesn't clash across instances. The "uid" must stay fixed, because URLs and links use it.
  5. Commit.

Generating dashboards

Hand-editing large JSON files gets painful. Teams with many dashboards generate them:

ToolApproach
Grafonnet (Jsonnet)The long-standing standard; used by kube-prometheus mixins
Grafana Foundation SDKTyped builders in Go, TypeScript, Python, Java
Terraform grafana providerDashboards, folders, data sources, alerts, permissions as Terraform resources
Plain scriptsTemplating 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.

Lab

  1. Export the node dashboard you built in Data Sources & Panels to grafana/dashboards/Platform/node.json. Set a fixed "uid": "node" and remove "id".
  2. Wait 30 seconds. It appears in the Platform folder as provisioned.
  3. Edit the title in the JSON file and save. Watch Grafana pick it up.
  4. 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.

On this page