Skip to content

Commit 092ba24

Browse files
committed
Add grafana dashboard linter
This commit introduces a linter[1] to verify grafana dashboard including the promql expressions used in the dashboard. [1] https://github.com/grafana/dashboard-linter Signed-off-by: Arunprasad Rajkumar <[email protected]>
1 parent c76b937 commit 092ba24

File tree

5 files changed

+633
-111
lines changed

5 files changed

+633
-111
lines changed

.lint

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
exclusions:
2+
template-job-rule:
3+
panel-job-instance-rule:

Makefile

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
BIN_DIR ?= $(shell pwd)/tmp/bin
22

33
JSONNET_VENDOR=vendor
4+
GRAFANA_DASHBOARD_LINTER_BIN=$(BIN_DIR)/dashboard-linter
45
JB_BIN=$(BIN_DIR)/jb
56
JSONNET_BIN=$(BIN_DIR)/jsonnet
67
JSONNETLINT_BIN=$(BIN_DIR)/jsonnet-lint
78
JSONNETFMT_BIN=$(BIN_DIR)/jsonnetfmt
89
PROMTOOL_BIN=$(BIN_DIR)/promtool
9-
TOOLING=$(JB_BIN) $(JSONNETLINT_BIN) $(JSONNET_BIN) $(JSONNETFMT_BIN) $(PROMTOOL_BIN)
10+
TOOLING=$(JB_BIN) $(JSONNETLINT_BIN) $(JSONNET_BIN) $(JSONNETFMT_BIN) $(PROMTOOL_BIN) $(GRAFANA_DASHBOARD_LINTER_BIN)
1011
JSONNETFMT_ARGS=-n 2 --max-blank-lines 2 --string-style s --comment-style s
1112

1213
.PHONY: all
1314
all: fmt generate lint test
1415

1516
.PHONY: generate
16-
generate: prometheus_alerts.yaml prometheus_rules.yaml dashboards_out
17+
generate: prometheus_alerts.yaml prometheus_rules.yaml dashboards_out/.lint
1718

1819
$(JSONNET_VENDOR): $(JB_BIN) jsonnetfile.json
1920
$(JB_BIN) install
@@ -29,18 +30,26 @@ prometheus_alerts.yaml: $(JSONNET_BIN) mixin.libsonnet lib/alerts.jsonnet alerts
2930
prometheus_rules.yaml: $(JSONNET_BIN) mixin.libsonnet lib/rules.jsonnet rules/*.libsonnet
3031
@$(JSONNET_BIN) -J vendor -S lib/rules.jsonnet > $@
3132

32-
dashboards_out: $(JSONNET_BIN) $(JSONNET_VENDOR) mixin.libsonnet lib/dashboards.jsonnet dashboards/*.libsonnet
33+
dashboards_out/.lint: $(JSONNET_BIN) $(JSONNET_VENDOR) mixin.libsonnet lib/dashboards.jsonnet dashboards/*.libsonnet
3334
@mkdir -p dashboards_out
3435
@$(JSONNET_BIN) -J vendor -m dashboards_out lib/dashboards.jsonnet
36+
@cp .lint $@
3537

3638
.PHONY: lint
37-
lint: $(PROMTOOL_BIN) $(JSONNET_VENDOR) prometheus_alerts.yaml prometheus_rules.yaml
39+
lint: $(PROMTOOL_BIN) $(JSONNET_VENDOR) prometheus_alerts.yaml prometheus_rules.yaml dashboard_lint
3840
find . -name 'vendor' -prune -o -name '*.libsonnet' -print -o -name '*.jsonnet' -print | \
3941
xargs -n 1 -- $(JSONNETLINT_BIN) -J vendor
4042

4143
@$(PROMTOOL_BIN) check rules prometheus_rules.yaml
4244
@$(PROMTOOL_BIN) check rules prometheus_alerts.yaml
4345

46+
.PHONY: dashboard_lint
47+
dashboard_lint: dashboards_out/.lint
48+
# Replace $$interval:$$resolution var with $$__rate_interval to make dashboard-linter happy.
49+
@sed -i -e 's/$$interval:$$resolution/$$__rate_interval/g' dashboards_out/*.json
50+
@find dashboards_out -name '*.json' -print0 | xargs -n 1 -0 $(GRAFANA_DASHBOARD_LINTER_BIN) lint --strict
51+
52+
4453
.PHONY: clean
4554
clean:
4655
# Remove all files and directories ignored by git.

scripts/go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.14
44

55
require (
66
github.com/google/go-jsonnet v0.17.1-0.20210520122306-7373f5b60678
7+
github.com/grafana/dashboard-linter v0.0.0-20220121193616-222f7f5cfe30
78
github.com/jsonnet-bundler/jsonnet-bundler v0.4.0
8-
github.com/prometheus/prometheus v1.8.2-0.20210701133801-b0944590a1c9
9+
github.com/prometheus/prometheus v1.8.2-0.20211011171444-354d8d2ecfac
910
)

0 commit comments

Comments
 (0)