|
1 | | -JSONNET_FMT := jsonnetfmt -n 2 --max-blank-lines 2 --string-style s --comment-style s |
| 1 | +BIN_DIR ?= $(shell pwd)/tmp/bin |
2 | 2 |
|
3 | | -all: fmt prometheus-alerts.yaml dashboards_out lint |
| 3 | +JSONNET_VENDOR=vendor |
| 4 | +GRAFANA_DASHBOARD_LINTER_BIN=$(BIN_DIR)/dashboard-linter |
| 5 | +JB_BIN=$(BIN_DIR)/jb |
| 6 | +JSONNET_BIN=$(BIN_DIR)/jsonnet |
| 7 | +JSONNETLINT_BIN=$(BIN_DIR)/jsonnet-lint |
| 8 | +JSONNETFMT_BIN=$(BIN_DIR)/jsonnetfmt |
| 9 | +MD_FILES = $(shell find . \( -type d -name '.vale' -o -type d -name 'vendor' \) -prune -o -type f -name "*.md" -print) |
| 10 | +MARKDOWNFMT_BIN=$(BIN_DIR)/markdownfmt |
| 11 | +VALE_BIN=$(BIN_DIR)/vale |
| 12 | +PROMTOOL_BIN=$(BIN_DIR)/promtool |
| 13 | +PINT_BIN=$(BIN_DIR)/pint |
| 14 | +TOOLING=$(JB_BIN) $(JSONNETLINT_BIN) $(JSONNET_BIN) $(JSONNETFMT_BIN) $(PROMTOOL_BIN) $(GRAFANA_DASHBOARD_LINTER_BIN) $(MARKDOWNFMT_BIN) $(VALE_BIN) $(PINT_BIN) |
| 15 | +JSONNETFMT_ARGS=-n 2 --max-blank-lines 2 --string-style s --comment-style s |
| 16 | +SRC_DIR ?=dashboards |
| 17 | +OUT_DIR ?=dashboards_out |
4 | 18 |
|
5 | | -fmt: |
6 | | - find . -name 'vendor' -prune -o -name '*.libsonnet' -print -o -name '*.jsonnet' -print | \ |
7 | | - xargs -n 1 -- $(JSONNET_FMT) -i |
| 19 | +.PHONY: all |
| 20 | +all: fmt generate lint test |
8 | 21 |
|
9 | | -prometheus-alerts.yaml: mixin.libsonnet config.libsonnet $(wildcard alerts/*) |
10 | | - jsonnet -S alerts.jsonnet > $@ |
| 22 | +.PHONY: generate |
| 23 | +generate: prometheus_alerts.yaml prometheus_rules.yaml $(OUT_DIR) |
11 | 24 |
|
12 | | -dashboards_out: mixin.libsonnet config.libsonnet $(wildcard dashboards/*) |
13 | | - @mkdir -p dashboards_out |
14 | | - jsonnet -J vendor -m dashboards_out dashboards.jsonnet |
| 25 | +$(JSONNET_VENDOR): $(JB_BIN) jsonnetfile.json |
| 26 | + $(JB_BIN) install |
15 | 27 |
|
16 | | -lint: prometheus-alerts.yaml |
17 | | - find . -name 'vendor' -prune -o -name '*.libsonnet' -print -o -name '*.jsonnet' -print | \ |
18 | | - while read f; do \ |
19 | | - $(JSONNET_FMT) "$$f" | diff -u "$$f" -; \ |
20 | | - done |
| 28 | +.PHONY: fmt |
| 29 | +fmt: jsonnet-fmt markdownfmt |
21 | 30 |
|
22 | | - promtool check rules prometheus-alerts.yaml |
| 31 | +.PHONY: jsonnet-fmt |
| 32 | +jsonnet-fmt: $(JSONNETFMT_BIN) |
| 33 | + @find . -name 'vendor' -prune -o -name '*.libsonnet' -print -o -name '*.jsonnet' -print | \ |
| 34 | + xargs -n 1 -- $(JSONNETFMT_BIN) $(JSONNETFMT_ARGS) -i |
23 | 35 |
|
24 | | -test: prometheus-alerts.yaml |
25 | | - promtool test rules tests.yaml |
| 36 | +.PHONY: markdownfmt |
| 37 | +markdownfmt: $(MARKDOWNFMT_BIN) |
| 38 | + @for file in $(MD_FILES); do $(MARKDOWNFMT_BIN) -w -gofmt $$file; done |
26 | 39 |
|
| 40 | +prometheus_alerts.yaml: $(JSONNET_BIN) mixin.libsonnet lib/alerts.jsonnet alerts/*.libsonnet |
| 41 | + @$(JSONNET_BIN) -J vendor -S lib/alerts.jsonnet > $@ |
| 42 | + |
| 43 | +prometheus_rules.yaml: $(JSONNET_BIN) mixin.libsonnet lib/rules.jsonnet rules/*.libsonnet |
| 44 | + @$(JSONNET_BIN) -J vendor -S lib/rules.jsonnet > $@ |
| 45 | + |
| 46 | +$(OUT_DIR): $(JSONNET_BIN) $(JSONNET_VENDOR) mixin.libsonnet lib/dashboards.jsonnet $(SRC_DIR)/*.libsonnet |
| 47 | + @mkdir -p $(OUT_DIR) |
| 48 | + @$(JSONNET_BIN) -J vendor -m $(OUT_DIR) lib/dashboards.jsonnet |
| 49 | + |
| 50 | +.PHONY: lint |
| 51 | +lint: jsonnet-lint alerts-lint dashboards-lint vale pint-lint |
| 52 | + |
| 53 | +.PHONY: jsonnet-lint |
| 54 | +jsonnet-lint: $(JSONNETLINT_BIN) $(JSONNET_VENDOR) |
| 55 | + @find . -name 'vendor' -prune -o -name '*.libsonnet' -print -o -name '*.jsonnet' -print | \ |
| 56 | + xargs -n 1 -- $(JSONNETLINT_BIN) -J vendor |
| 57 | + |
| 58 | +.PHONY: alerts-lint |
| 59 | +alerts-lint: $(PROMTOOL_BIN) prometheus_alerts.yaml prometheus_rules.yaml |
| 60 | + @$(PROMTOOL_BIN) check rules prometheus_rules.yaml |
| 61 | + @$(PROMTOOL_BIN) check rules prometheus_alerts.yaml |
| 62 | + |
| 63 | +$(OUT_DIR)/.lint: $(OUT_DIR) |
| 64 | + @cp .lint $@ |
| 65 | + |
| 66 | +.PHONY: dashboards-lint |
| 67 | +dashboards-lint: $(GRAFANA_DASHBOARD_LINTER_BIN) $(OUT_DIR)/.lint |
| 68 | + # Replace $$interval:$$resolution var with $$__rate_interval to make dashboard-linter happy. |
| 69 | + @sed -i -e 's/$$interval:$$resolution/$$__rate_interval/g' $(OUT_DIR)/*.json |
| 70 | + @find $(OUT_DIR) -name '*.json' ! -name 'celery-tasks.json' -print0 | xargs -n 1 -0 $(GRAFANA_DASHBOARD_LINTER_BIN) lint --strict |
| 71 | + |
| 72 | +.PHONY: vale |
| 73 | +vale: $(VALE_BIN) |
| 74 | + @$(VALE_BIN) sync && \ |
| 75 | + $(VALE_BIN) $(MD_FILES) |
| 76 | + |
| 77 | +.PHONY: pint-lint |
| 78 | +pint-lint: generate $(PINT_BIN) |
| 79 | + @# Pint will not exit with a non-zero status code if there are linting issues. |
| 80 | + @output=$$($(PINT_BIN) -n -o -l WARN lint prometheus_alerts.yaml prometheus_rules.yaml 2>&1); \ |
| 81 | + if [ -n "$$output" ]; then \ |
| 82 | + echo "\n$$output"; \ |
| 83 | + exit 1; \ |
| 84 | + fi |
| 85 | + |
| 86 | +.PHONY: clean |
27 | 87 | clean: |
28 | | - rm -rf dashboards_out prometheus-alerts.yaml |
| 88 | + # Remove all files and directories ignored by git. |
| 89 | + git clean -Xfd . |
| 90 | + |
| 91 | +.PHONY: test |
| 92 | +test: $(PROMTOOL_BIN) prometheus_alerts.yaml prometheus_rules.yaml |
| 93 | + @$(PROMTOOL_BIN) test rules tests/*.yaml |
| 94 | + |
| 95 | +$(BIN_DIR): |
| 96 | + mkdir -p $(BIN_DIR) |
| 97 | + |
| 98 | +$(TOOLING): $(BIN_DIR) |
| 99 | + @echo Installing tools from hack/tools.go |
| 100 | + @cd scripts && go list -e -mod=mod -tags tools -f '{{ range .Imports }}{{ printf "%s\n" .}}{{end}}' ./ | xargs -tI % go build -mod=mod -o $(BIN_DIR) % |
0 commit comments