grafanadashboard

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 7, 2026 License: MIT Imports: 2 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

grafana-dashboard-go

A typed Grafana dashboard model (panels / targets / thresholds) rendered, borealis-themed, to Grafana dashboard JSON — so dashboards are types, not hand-edited 1000-line JSON blobs that drift.

What

A typed Dashboard of Panels (each with Targets and Thresholds) rendered by one pure verb:

jsonBytes, err := d.RenderJSON(grafanadashboard.Tundra())

RenderJSON is a pure function of the Dashboard and the Theme: the same input yields byte-identical JSON every call (deterministic field order, panels auto-laid-out on the 24-column grid in declaration order, threshold colours resolved from the theme).

Why

Grafana dashboards live as JSON nobody hand-edits safely: copy-pasted panels keep stale datasources, thresholds disagree across panels, the palette is whatever the last editor clicked. Authoring the dashboard as a typed value and rendering the JSON mechanically — with a single fleet-consistent theme — removes the whole class of drift.

Theme (borealis-aligned)

Thresholds and panel colours reference semantic Roles (RoleSuccess/RoleWarning/RoleDanger/…), never raw hex. Tundra() and Nord() return the Nord Aurora palette the borealis design system binds to those roles, so every fleet dashboard reads the same.

Install

go get github.com/pleme-io/grafana-dashboard-go

Usage

d, err := grafanadashboard.New("service-red",
    grafanadashboard.WithTitle("Service RED"),
    grafanadashboard.WithDatasource("prometheus"),
    grafanadashboard.WithPanel(grafanadashboard.Panel{
        Title:   "Request rate",
        Type:    grafanadashboard.TimeSeries,
        Unit:    "reqps",
        Targets: []grafanadashboard.Target{{Expr: "rate(http_requests_total[5m])"}},
    }),
    grafanadashboard.WithPanel(grafanadashboard.Panel{
        Title: "Error ratio",
        Type:  grafanadashboard.Gauge,
        Targets: []grafanadashboard.Target{{Expr: "..."}},
        Thresholds: []grafanadashboard.Threshold{
            {Value: 0, Role: grafanadashboard.RoleSuccess},
            {Value: 0.01, Role: grafanadashboard.RoleWarning},
            {Value: 0.05, Role: grafanadashboard.RoleDanger},
        },
    }))
if err != nil { return errs.Exit(err) } // typed code-carrying error (dashboard_invalid)

j, _ := d.RenderJSON(grafanadashboard.Tundra())
os.WriteFile("service-red.json", j, 0o644)

Configuration

None — a pure library. Callers that read the dashboard shape from config use shikumi-go and pass the fields via the functional options; a FromConfig bridge over a shikumi-loaded Dashboard-shaped struct is the natural extension for config-driven dashboards.

Release

Pull-model (Go modules): an annotated vX.Y.Z tag is the release; pkg.go.dev indexes it. See the GSDS module delivery FSM.

Documentation

Overview

Package grafanadashboard is the fleet's typed Grafana dashboard model — a dashboard authored as a typed Dashboard of [Panel]s (with [Target]s and [Threshold]s) and rendered, borealis-themed, to Grafana dashboard JSON. So dashboards are types, not hand-edited JSON blobs that drift.

Why

Grafana dashboards live as 1000-line JSON files that nobody hand-edits safely: a copy-pasted panel keeps a stale datasource, thresholds disagree across panels, the colour palette is whatever the last editor clicked. The cure is to author the dashboard as a typed value and render the JSON mechanically with a consistent (borealis/Tundra Aurora) theme.

Shape (Law 1, §3.5)

d, err := grafanadashboard.New("service-red",
    grafanadashboard.WithTitle("Service RED"),
    grafanadashboard.WithPanel(grafanadashboard.Panel{
        Title:   "Request rate",
        Type:    grafanadashboard.TimeSeries,
        Targets: []grafanadashboard.Target{{Expr: "rate(http_requests_total[5m])"}},
        Thresholds: []grafanadashboard.Threshold{
            {Value: 0, Role: grafanadashboard.RoleSuccess},
            {Value: 100, Role: grafanadashboard.RoleWarning},
        },
    }))
jsonBytes, err := d.RenderJSON(grafanadashboard.Tundra())

RenderJSON is a pure function of the Dashboard + Theme: the same input yields byte-identical JSON every time (deterministic field order, panel grid laid out in declaration order, threshold colours resolved from the theme).

Theme (borealis-aligned)

Tundra and Nord return the same Aurora-family palette the borealis design system binds to semantic roles. Thresholds and panel colours reference semantic [Role]s, not raw hex, so every fleet dashboard reads the same.

Weight (Law 6)

Pure standard library (encoding/json) plus errors-go for typed, code-carrying errors. The theme palette is baked in as typed tokens; it does not import the heavy borealis charm stack — a dashboard renderer needs the hex tokens, not a terminal renderer.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Dashboard

type Dashboard struct {
	// UID is the stable dashboard UID. Required (seeds the JSON `uid`).
	UID string
	// Title is the human dashboard title. Defaults to the UID.
	Title string
	// Tags are dashboard tags, kept in declaration order.
	Tags []string
	// Datasource is the default datasource name applied to targets that do not
	// set their own. Empty leaves Grafana's default.
	Datasource string
	// Panels are the dashboard's panels, laid out in declaration order.
	Panels []Panel
}

Dashboard is the one typed declaration of a Grafana dashboard. Construct with New and functional options; the constructor applies defaults and validates invariants in one place.

func New

func New(uid string, opts ...Option) (*Dashboard, error)

New builds and validates a Dashboard. It returns a typed, code-carrying error (code "dashboard_invalid") when an invariant is violated.

func (*Dashboard) RenderJSON

func (d *Dashboard) RenderJSON(t Theme) ([]byte, error)

RenderJSON renders the dashboard to Grafana dashboard JSON, themed by t. It is a pure function of (Dashboard, Theme): the same input yields byte-identical JSON every call. Panels are laid out left-to-right, wrapping at the 24-column grid boundary, in declaration order.

type Option

type Option func(*Dashboard)

Option configures a Dashboard during New.

func WithDatasource

func WithDatasource(ds string) Option

WithDatasource sets the default datasource.

func WithPanel

func WithPanel(p Panel) Option

WithPanel appends a panel.

func WithTag

func WithTag(tag string) Option

WithTag appends a dashboard tag.

func WithTitle

func WithTitle(title string) Option

WithTitle sets the dashboard title.

type Panel

type Panel struct {
	// Title is the panel heading.
	Title string
	// Type selects the panel renderer. Defaults to [TimeSeries].
	Type PanelType
	// Unit is the value unit (Grafana unit id, e.g. "reqps", "s", "percent").
	Unit string
	// Width is the panel grid width in 24-column units (default 12).
	Width int
	// Height is the panel grid height in row units (default 8).
	Height int
	// Targets are the panel's queries.
	Targets []Target
	// Thresholds are the value bands; their colours resolve from the theme Role.
	Thresholds []Threshold
}

Panel is one dashboard panel.

type PanelType

type PanelType string

PanelType is the Grafana panel renderer kind.

const (
	// TimeSeries is the line/area time-series panel.
	TimeSeries PanelType = "timeseries"
	// Stat is the single-number stat panel.
	Stat PanelType = "stat"
	// Gauge is the gauge panel.
	Gauge PanelType = "gauge"
	// Table is the table panel.
	Table PanelType = "table"
)

type Role

type Role int

Role is a semantic colour intent, mirroring the borealis design system's Role vocabulary. A consumer maps a domain state (a threshold band, a panel emphasis) onto a Role; the Theme decides the actual hex, so every fleet dashboard reads the same and colours are never hand-authored (the ishou / borealis "consume tokens, never hex" rule).

const (
	// RoleNeutral is inactive / baseline (Polar Night surface tone).
	RoleNeutral Role = iota
	// RolePrimary is the headline / primary-metric colour (Frost).
	RolePrimary
	// RoleSuccess is a healthy band (Aurora green).
	RoleSuccess
	// RoleInfo is informational (Frost cyan).
	RoleInfo
	// RoleWarning is a degraded band (Aurora yellow).
	RoleWarning
	// RoleDanger is a breached band (Aurora red).
	RoleDanger
)

type Target

type Target struct {
	// Expr is the query expression (e.g. a PromQL string).
	Expr string
	// Legend is the legend format. Optional.
	Legend string
	// Datasource overrides the dashboard default for this target. Optional.
	Datasource string
}

Target is one panel query.

type Theme

type Theme struct {
	// Name identifies the theme in the rendered dashboard metadata.
	Name string
	// contains filtered or unexported fields
}

Theme is the resolved token bundle: a Role → hex map plus the Grafana base colour names this theme prefers. It is pure data so a dashboard renders the same regardless of where the theme came from.

func Nord

func Nord() Theme

Nord is an alias of Tundra under the upstream palette name — same tokens, provided so a consumer can name the source palette explicitly.

func Tundra

func Tundra() Theme

Tundra is the pleme-io fleet-default Grafana theme: the Nord Aurora family bound to semantic roles, matching the borealis Tundra theme. These are the canonical Nord hex tokens (nordtheme.com) — the same palette ishou binds.

func (Theme) Color

func (t Theme) Color(r Role) string

Color resolves a Role to its hex string under this theme. An unknown Role falls back to the neutral surface tone so rendering never panics.

type Threshold

type Threshold struct {
	// Value is the band's lower bound. The first threshold conventionally has
	// Value 0 and is the base band.
	Value float64
	// Role is the semantic colour intent for this band.
	Role Role
}

Threshold is one value band. The colour is not a hex string but a semantic Role resolved from the Theme at render time — so dashboards never carry hand-authored colours.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址