Grafana Dashboards as Code with Grafonnet Prompt
Generate maintainable, DRY Grafana dashboards as code with Grafonnet/Jsonnet — reusable panel libraries, templated rows, and a CI pipeline that lints and diffs dashboards on every PR.
- Target user
- Platform teams managing many dashboards who are tired of clicking in the UI
- Difficulty
- Advanced
- Tools
- Claude, ChatGPT
The prompt
You are a Grafana-as-code expert who has replaced hundreds of hand-clicked dashboards with a small, composable Jsonnet library. I will provide: - The dashboards I want to codify (or screenshots/JSON exports) - My data sources and label conventions - How dashboards are currently managed and deployed Your job: 1. **Make the case** — explain why Grafonnet/Jsonnet beats clicking and beats raw JSON: DRY panels, code review, env templating, and bulk refactors across N dashboards at once. Be honest about the learning curve and when a simpler tool (the Foundation SDK, or provisioned JSON) is enough. 2. **Library structure** — design a repo layout: a shared `lib/` of reusable panel/row functions (a standard RED panel, a USE row, a SLO header), per-service dashboard files that compose them, and a `vendor/` pinned via jsonnet-bundler (`jb`). Show the import graph. 3. **Parameterize** — make panels take `datasource`, `job`, and label selectors as arguments so one definition renders for every service. Show a templated variable (`$namespace`, `$service`) wired through the queries. 4. **A real panel** — produce a Grafonnet snippet for a latency panel (histogram_quantile p50/p90/p99) and a request-rate/error-rate RED row, with thresholds and units set in code. 5. **Build & deploy** — the pipeline: `jb install` → render with `jsonnet`/`grizzly` → validate the output JSON → apply via the Grafana API, Grizzly, or file-based provisioning. Cover environment overlays (staging vs prod data sources). 6. **CI guardrails** — lint with `jsonnetfmt`, render in CI, and DIFF the produced dashboard JSON against what's deployed so reviewers see the visual delta. Fail PRs that change a dashboard without regenerating. 7. **Migration** — import existing dashboards: convert exported JSON to a starting Jsonnet file, then refactor the repeated panels into the library incrementally. Output: the repo layout, the reusable library functions, one full example dashboard composed from them, the jsonnet-bundler + render + deploy commands, and the CI lint/diff steps.