GDG has now introduced a new supporting tool that works in conjunction with GDG. It is currently dependent on the GDG
configuration
since it will operate on the currently selected context. You can confirm what the current context is by
running gdg tools ctx show
Most of the config isn’t that interesting, except the output_path will be used to determine where the newly generated
dashboards will be. Make sure you have a valid configuration before continuing.
What does gdg-generate do?
There are use cases where an almost identical dashboard is needed except we need to replace certain parts of it.
For example, parts of a query need to be different, a different title, brand it to specific customer with a different
logo, or footer. All of these are difficult to control from grafana itself and even in the best case scenario it’s not
great user experience. This allows you to configure and generate a new dashboard with any set of variables and
dictionaries that you will seed to the tool.
Configuration
The configuration that drives this application is templates.yml. You can see an example below.
entities:dashboards:- template_name:"template_example"##Matches the name to a file under ouput_path/templates/*.go.tmploutput:## The section below defines one or multiple destination and the associated configuration## that goes with it.- folder:"General"## Name of the new folder where the template will be createdorganization_name:"Main Org."dashboard_name:""## Optional, defaults to template_name.jsontemplate_data:## Template Data the dictionary of datasets that can be used in the template,# it's basically your 'seed data'. Everything is contains is absolutely arbitrary# and can have any structure as long as it's valid yamlTitle:"Bob Loves Candy"## Dashboard Titlteenabledlight:false## Boolean check to enable/disable behaviorlightsources:## some arbitrary list we get to play with- sun- moon- lightbulb- office lights
One caveat. The “Keys” will all be lowercased due to how the data is being read in. Meaning, even though
Title is specified, the template will see the value under “title” instead.
Available Functions
Additionally, there a few functions exposed and available to you that allows you to modify
| Function Name | Example | Input | Output |
|------------------|-----------------------------------------|----------------------|--------------------------|
| ToSlug | {{ .title \| ToSlug }} | Bob Candy | bob-candy |
| QuotedStringJoin | {{ .lightsources \| QuotedStringJoin }} | [sun,moon,lightbulb] | "sun","moon","lightbulb" |
There is also a large collection of functions that have been imported from sprig
and are available for use.
Example Templating Snippets
Data Injection
{"annotations":{"list":[{"$$hashKey":"{{ .title | lower | ToSlug}}",// Inserting data and piping it to two different functions. In this case, ToLower is redundant, but it serves as a chained example.
"builtIn":1,"datasource":"Grafana","enable":true,"hide":true,"iconColor":"rgba(0, 211, 255, 1)","name":"Annotations Alerts","type":"dashboard"}]}}
Iterating and conditionals.
{"link_text":[{{if.enabledlight}}// conditional to check if to insert or not
{{range$v:=.lightsources}}// Iterating through list
{{$v}}// Inserting value
{{end}}{{end}}]}
As part of the installation you will have access to gdg-generate.
Note
–config, –template-config, and -t are optional parameters. gdg-generate will fallback on defaults if
none are specified. If -t is not provided, all templates will be processed