Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: dynamic matcher+template checks #181

Open
wants to merge 28 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
bd48754
:sparkles: required matchers can be provided via args + dynamic check…
alex5517 Jul 10, 2024
ff625b5
Change var naming
alex5517 Jul 10, 2024
9527c05
update docs
alex5517 Jul 10, 2024
5d25007
update docs add new
alex5517 Jul 10, 2024
73f5a85
Revert deletion of job/instance rules + remove matchers arg in favor …
alex5517 Jul 11, 2024
a3f79d8
:rewind: :hankey: - Fix chmod...
alex5517 Jul 12, 2024
dcfc870
:lipstick: - Add way of indicating a rules stability + Change printin…
alex5517 Jul 12, 2024
09de038
Light cleanup work + improved testing
alex5517 Jul 12, 2024
4ed59dc
Add/Update docs
alex5517 Jul 12, 2024
b08d388
:construction: Add test for Grafana variables :construction:
alex5517 Jul 12, 2024
721da14
merge conflict
alex5517 Sep 2, 2024
845df8a
Add legacy config example
alex5517 Sep 2, 2024
99ff76a
Go mod tidy
alex5517 Sep 2, 2024
7076e64
Use const for stability
alex5517 Sep 2, 2024
f71ba50
revert usage of const in test
alex5517 Sep 2, 2024
287dffd
:construction: initial rework of variable expansion to support revers…
alex5517 Sep 9, 2024
15d2e81
remove misleading comment
alex5517 Sep 9, 2024
d2cc9d8
:adhesive_bandage: - support different uses of variables
alex5517 Sep 9, 2024
c4bb4fc
fix failing test
alex5517 Sep 9, 2024
98ee145
Reverse variable expansion for: target-required-matchers-rule
alex5517 Sep 9, 2024
54aeb32
Do not omitempty dashboard editable
alex5517 Sep 10, 2024
ae10cf2
Support templating variables having a variable as value (expand recur…
alex5517 Sep 10, 2024
99d1d7c
:adhesive_bandage: Modfiy regexp matching to only match string that p…
alex5517 Sep 11, 2024
17956a8
:adhesive_bandage: Use a combination of slices and maps to ensure out…
alex5517 Sep 11, 2024
2594e21
:white_check_mark: Update tests to match new variable expansion metho…
alex5517 Sep 11, 2024
09e53e8
Support cases where variable is matchers
alex5517 Nov 7, 2024
c2e6171
Add hacky check and override configured match type if needed
alex5517 Nov 7, 2024
89661c5
mType should also be used when autofix
alex5517 Nov 7, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ intermediate-docs:
@go run ./main.go -h > ./docs/_intermediate/help.txt
@go run ./main.go completion -h > ./docs/_intermediate/completion.txt
@go run ./main.go lint -h > ./docs/_intermediate/lint.txt
@go run ./main.go rules > ./docs/_intermediate/rules.txt
@go run ./main.go rules --experimental > ./docs/_intermediate/rules.txt
@echo "Can't automate everything, please replace the #Rules section of index.md with the contents of ./docs/_intermediate/rules.txt"

embedmd:
Expand Down
42 changes: 28 additions & 14 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@ Usage:

Flags:
-c, --config string path to a configuration file
--experimental enable experimental rules
--fix automatically fix problems if possible
-h, --help help for lint
--stdin read from stdin
--strict fail upon linting error or warning
--verbose show more information about linting
```
Expand All @@ -62,20 +64,32 @@ Flags:

The linter implements the following rules:

* [template-datasource-rule](./rules/template-datasource-rule.md) - Checks that the dashboard has a templated datasource.
* [template-job-rule](./rules/template-job-rule.md) - Checks that the dashboard has a templated job.
* [template-instance-rule](./rules/template-instance-rule.md) - Checks that the dashboard has a templated instance.
rgeyer marked this conversation as resolved.
Show resolved Hide resolved
* [template-label-promql-rule](./rules/template-label-promql-rule.md) - Checks that the dashboard templated labels have proper PromQL expressions.
* [template-on-time-change-reload-rule](./rules/template-on-time-change-reload-rule.md) - Checks that the dashboard template variables are configured to reload on time change.
* [panel-datasource-rule](./rules/panel-datasource-rule.md) - Checks that each panel uses the templated datasource.
* [panel-title-description-rule](./rules/panel-title-description-rule.md) - Checks that each panel has a title and description.
* [panel-units-rule](./rules/panel-units-rule.md) - Checks that each panel uses has valid units defined.
* `panel-no-targets-rule` - Checks that each panel has at least one target.
* [target-promql-rule](./rules/target-promql-rule.md) - Checks that each target uses a valid PromQL query.
* [target-rate-interval-rule](./rules/target-rate-interval-rule.md) - Checks that each target uses $__rate_interval.
* [target-job-rule](./rules/target-job-rule.md) - Checks that every PromQL query has a job matcher.
* [target-instance-rule](./rules/target-instance-rule.md) - Checks that every PromQL query has a instance matcher.
* `target-counter-agg-rule` - Checks that any counter metric (ending in _total) is aggregated with rate, irate, or increase.
* [template-datasource-rule](./rules/template-datasource-rule) - ``stable`` - Checks that the dashboard has a templated datasource.
* [template-job-rule](./rules/template-job-rule) - `stable` - Checks that the dashboard has a templated job.
* [template-instance-rule](./rules/template-instance-rule) - `stable` - Checks that the dashboard has a templated instance.
* [template-label-promql-rule](./rules/template-label-promql-rule) - `stable` - Checks that the dashboard templated labels have proper PromQL expressions.
* [template-on-time-change-reload-rule](./rules/template-on-time-change-reload-rule) - `stable` - Checks that the dashboard template variables are configured to reload on time change.
* [panel-datasource-rule](./rules/panel-datasource-rule) - `stable` - Checks that each panel uses the templated datasource.
* [panel-title-description-rule](./rules/panel-title-description-rule) - `stable` - Checks that each panel has a title and description.
* [panel-units-rule](./rules/panel-units-rule) - `stable` - Checks that each panel uses has valid units defined.
* [panel-no-targets-rule](./rules/panel-no-targets-rule) - `stable` - Checks that each panel has at least one target.
* [target-promql-rule](./rules/target-promql-rule) - `stable` - Checks that each target uses a valid PromQL query.
* [target-rate-interval-rule](./rules/target-rate-interval-rule) - `stable` - Checks that each target uses $__rate_interval.
* [target-job-rule](./rules/target-job-rule) - `stable` - Checks that every PromQL query has a job matcher.
* [target-instance-rule](./rules/target-instance-rule) - `stable` - Checks that every PromQL query has a instance matcher.
* [target-counter-agg-rule](./rules/target-counter-agg-rule) - `stable` - Checks that any counter metric (ending in _total) is aggregated with rate, irate, or increase.
* [uneditable-dashboard-rule](./rules/uneditable-dashboard-rule) - `stable` - Checks that the dashboard is not editable.
* [target-required-matchers-rule](./rules/target-required-matchers-rule) - `experimental` - Checks that target PromQL query has the required matchers
* [template-required-variables-rule](./rules/template-required-variables-rule) - `experimental` - Checks that the dashboard has a template variable for required variables or matchers that use variables

## Rule stability
- **Stable** rules have gone through testing and been widely adopted.

- **Experimental** rules are for new and experimental features.
These rules are not enabled by default, but can be enabled by providing the `experimental` flag.
Allowing early adopters to gain confidence with new features.

- **Deprecated** rules may be removed or replaced when they are marked as deprecated.

## Related Rules

Expand Down
20 changes: 20 additions & 0 deletions docs/rules/target-required-matchers-rule.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# target-required-matchers-rule
Checks that each PromQL query has a the matchers specified in rule settings. This rule is experimental and is designed to work with Prometheus datasources.

## Rule Settings

```yaml
settings:
target-required-matchers-rule:
matchers:
- cluster=~"$cluster"
- someLabel="someValue"
```
Legacy config example for job and instance
```yaml
settings:
target-required-matchers-rule:
matchers:
- job
- instance
```
31 changes: 31 additions & 0 deletions docs/rules/template-required-variables-rule.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# template-required-variables-rule
Checks that each dashboard has a templated variable based on provided rule settings and detected variable usage for the target-required-matchers-rule.

# Best Practice
The rule ensures all of the following conditions.

* The dashboard template exists.
* The dashboard template is named `xxx`.
* The dashboard template is labeled `xxx`.
* The dashboard template uses a templated datasource, specifically named `$datasource`.
* The dashboard template uses a Prometheus query to find available matching instances.
* The dashboard template is multi select
* The dashboard template has an allValue of `.+`

## Rule Settings

```yaml
settings:
template-required-variables-rule:
variables:
- cluster
- namespace
```
Legacy config example for job and instance
```yaml
settings:
template-required-variables-rule:
variables:
- job
- instance
```
9 changes: 9 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ require (
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/aws/aws-sdk-go v1.53.16 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/jpillora/backoff v1.0.0 // indirect
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f // indirect
github.com/prometheus/common/sigv4 v0.1.0 // indirect
)

require (
cloud.google.com/go v0.114.0 // indirect
cloud.google.com/go/auth v0.5.1 // indirect
Expand Down Expand Up @@ -46,6 +54,7 @@ require (
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/prometheus/alertmanager v0.27.0
github.com/prometheus/client_golang v1.19.1 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.54.0 // indirect
Expand Down
Loading