🎓 A1Learner
🔍
0 of 50 lessons completed

Grafana: Dashboards as Code

A dashboard as code is a dashboard stored as a file, not only as a saved setting inside Grafana. The file lives in Git, so the dashboard gets versions, reviews, and rollbacks like any other code.

A provisioning file for dashboards

This file tells Grafana to load every JSON dashboard in the folder.

apiVersion: 1
providers:
  - name: shopsentry
    type: file
    options:
      path: /etc/grafana/provisioning/dashboards

Export the dashboard as JSON

The export call returns the dashboard JSON exactly as Grafana stores it.

curl -u admin:admin123 http://127.0.0.1:13104/api/dashboards/uid/shopsentry

Commit the exported file

The exported JSON goes into Git with the rest of the lab files.

git add dashboards/shopsentry.json
git commit -m "Add ShopSentry dashboard as code"

Example

curl -s -u admin:admin123 http://127.0.0.1:13104/api/dashboards/uid/shopsentry | head -c 300

Key points

  • Provisioning is a set of files that tells Grafana which dashboards and data sources to load at start.
  • A provisioning file names a folder of dashboard JSON files for Grafana to import.
  • Dashboard JSON is the full text of a dashboard: its panels, queries, variables, and layout.
  • Export a dashboard from the UI to get its JSON, then save that file in Git.
  • Git Sync is the Grafana 13 feature that keeps a dashboard folder in step with a Git repository.
  • With Git Sync on, a commit that changes the JSON updates the dashboard in Grafana.

In this example, the export call returns the real ShopSentry dashboard JSON from the running Grafana. The JSON holds the four golden-signal panels and the $instance and $route variables built in the earlier lessons. That file is what goes into Git.

Note

The JSON holds no metric data, only the dashboard design. The data still comes from Prometheus.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Rust Playground

Exercise

What does dashboard JSON hold?