Skip to content

Instantly share code, notes, and snippets.

@maxbrunet
Last active February 12, 2022 01:08
Show Gist options
  • Save maxbrunet/e96dee35f7e5fcd73a98929f4bc9c2bb to your computer and use it in GitHub Desktop.
Save maxbrunet/e96dee35f7e5fcd73a98929f4bc9c2bb to your computer and use it in GitHub Desktop.
Programmatically add a Grafana datasource template to a dashboard (jq / Jsonnet)

Templatize Grafana Dashboard Datasource

Easily add a $DS_PROMETHEUS datasource variable with its templating to a Grafana dashboard with jq or Jsonnet.

jq

./templatize_dashboard_datasource.jq dasboard.json >dashboard-new.json

Jsonnet

jsonnet - >dashboard-new.json <<EOF
local utils = import 'dashboard-utils.libsonnet';

(import 'dashboard.json') +
utils.withTemplatizedDatasource
EOF
{
// withPatchingPanels applies a patch to all panels.
withPatchingPanels(patch): {
panels:
if 'panels' in super then [
panel + patch +
// The schema changed in recent Grafana dashboards, some panels can be rows with panels
if 'panels' in panel then {
panels: [
subpanel + patch
for subpanel in panel.panels
],
} else {}
for panel in super.panels
],
// In older dashboards, we can have rows with panels
rows:
if 'rows' in super then [
row {
panels: [
panel + patch
for panel in row.panels
],
}
for row in super.rows
] else [],
},
// withDatasourceTemplate add a datasource variable/template.
withDatasourceTemplate: {
templating+: {
list: [{
current: {
text: 'Prometheus',
value: 'Prometheus',
},
hide: 0,
label: 'Datasource',
name: 'DS_PROMETHEUS',
options: [],
query: 'prometheus',
refresh: 1,
regex: '',
type: 'datasource',
}] + super.list,
},
},
// withTemplatizedDatasource replaces datasource in all panels and query-based templates by a variable.
withTemplatizedDatasource:
$.withDatasourceTemplate +
$.withPatchingPanels({
datasource: '$DS_PROMETHEUS',
}) {
templating+: {
list: [
if t.type == 'query' then t {
datasource: '$DS_PROMETHEUS',
} else t
for t in super.list
],
},
},
}
#!/bin/sh
# this next line is ignored by jq, which otherwise does not continue comments \
exec jq --exit-status --from-file "${0}" "${@}"
if has("panels") then
.panels[].datasource = "$DS_PROMETHEUS"
# The schema changed in recent Grafana dashboards, some panels can be rows with panels
| (.panels[] | select(.panels) | .panels[]).datasource = "$DS_PROMETHEUS"
else
.
end
# In older dashboards, we can have rows with panels
| if has("rows") then
.rows[].panels[].datasource = "$DS_PROMETHEUS"
else
.
end
| (.templating.list[] | select(.type == "query") | .datasource) = "$DS_PROMETHEUS"
# Add datasource variable/template if not there
| if [.templating.list[] | select(.type == "datasource" and .query == "prometheus")] | length < 1 then
.templating.list = [{
"current": {
"text": "Prometheus",
"value": "Prometheus",
},
"hide": 0,
"label": "Datasource",
"name": "DS_PROMETHEUS",
"options": [],
"query": "prometheus",
"refresh": 1,
"regex": "",
"type": "datasource"
}] + .templating.list
else
# Otherwise, ensure its name is DS_PROMEUTHEUS
(.templating.list[] | select(.type == "datasource" and .query == "prometheus")).name = "DS_PROMETHEUS"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment