Skip to content

Commit

Permalink
Merge pull request cloudflare#141 from cloudflare/multi-match
Browse files Browse the repository at this point in the history
Allow multiple match/ignore blocks per rule
  • Loading branch information
prymitive authored Feb 4, 2022
2 parents c44a409 + 4976339 commit a34cb6e
Show file tree
Hide file tree
Showing 8 changed files with 518 additions and 273 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Changed

- Removed `lines` label from `pint_problem` metric exported when running `pint watch`.
- Multiple `match` and `ignore` blocks can now be specified per each `rule`.

## v0.8.2

Expand Down
48 changes: 30 additions & 18 deletions cmd/pint/tests/0025_config.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,24 @@ level=info msg="Loading configuration file" path=.pint.hcl
]
},
{
"match": {
"path": "foo/bar",
"kind": "alerting"
},
"ignore": {
"kind": "alerting",
"label": {
"key": "notify",
"value": "blackhole"
"match": [
{
"path": "foo/bar",
"kind": "alerting"
}
],
"ignore": [
{
"kind": "alerting",
"label": {
"key": "notify",
"value": "blackhole"
}
},
"command": "watch"
},
{
"command": "watch"
}
],
"annotation": [
{
"key": "summary",
Expand All @@ -77,9 +83,11 @@ level=info msg="Loading configuration file" path=.pint.hcl
}
},
{
"match": {
"kind": "recording"
},
"match": [
{
"kind": "recording"
}
],
"aggregate": [
{
"name": ".+",
Expand All @@ -90,9 +98,11 @@ level=info msg="Loading configuration file" path=.pint.hcl
]
},
{
"match": {
"kind": "recording"
},
"match": [
{
"kind": "recording"
}
],
"cost": {
"bytesPerSample": 4036
}
Expand Down Expand Up @@ -127,11 +137,13 @@ rule {
}
ignore {
kind = "alerting"
command = "watch"
label "notify" {
value = "blackhole"
}
}
ignore {
command = "watch"
}
alerts {
range = "7d"
step = "1m"
Expand Down
36 changes: 36 additions & 0 deletions cmd/pint/tests/0052_match_multiple.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
pint.ok --no-color -l debug lint rules
! stdout .
cmp stderr stderr.txt

-- stderr.txt --
level=info msg="Loading configuration file" path=.pint.hcl
level=info msg="File parsed" path=rules/0001.yml rules=2
level=debug msg="Found recording rule" lines=1-2 path=rules/0001.yml record=colo:recording
level=debug msg="Configured checks for rule" enabled=["promql/syntax","alerts/for","promql/comparison","alerts/template","promql/fragile","promql/aggregate(job:true)"] path=rules/0001.yml rule=colo:recording
level=debug msg="Found alerting rule" alert=colo:alerting lines=4-5 path=rules/0001.yml
level=debug msg="Configured checks for rule" enabled=["promql/syntax","alerts/for","promql/comparison","alerts/template","promql/fragile","promql/aggregate(job:true)"] path=rules/0001.yml rule=colo:alerting
rules/0001.yml:2: job label is required and should be preserved when aggregating "^.+$" rules, remove job from without() (promql/aggregate)
expr: sum(foo) without(job)

rules/0001.yml:5: job label is required and should be preserved when aggregating "^.+$" rules, remove job from without() (promql/aggregate)
expr: sum(bar) without(job) > 0

-- rules/0001.yml --
- record: "colo:recording"
expr: sum(foo) without(job)

- alert: "colo:alerting"
expr: sum(bar) without(job) > 0

-- .pint.hcl --
rule {
match {
kind = "recording"
}
match {
kind = "alerting"
}
aggregate ".+" {
keep = [ "job" ]
}
}
30 changes: 30 additions & 0 deletions cmd/pint/tests/0053_ignore_multiple.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
pint.ok --no-color -l debug lint rules
! stdout .
cmp stderr stderr.txt

-- stderr.txt --
level=info msg="Loading configuration file" path=.pint.hcl
level=info msg="File parsed" path=rules/0001.yml rules=2
level=debug msg="Found recording rule" lines=1-2 path=rules/0001.yml record=colo:recording
level=debug msg="Configured checks for rule" enabled=["promql/syntax","alerts/for","promql/comparison","alerts/template","promql/fragile"] path=rules/0001.yml rule=colo:recording
level=debug msg="Found alerting rule" alert=colo:alerting lines=4-5 path=rules/0001.yml
level=debug msg="Configured checks for rule" enabled=["promql/syntax","alerts/for","promql/comparison","alerts/template","promql/fragile"] path=rules/0001.yml rule=colo:alerting
-- rules/0001.yml --
- record: "colo:recording"
expr: sum(foo) without(job)

- alert: "colo:alerting"
expr: sum(bar) without(job) > 0

-- .pint.hcl --
rule {
ignore {
kind = "recording"
}
ignore {
kind = "alerting"
}
aggregate ".+" {
keep = [ "job" ]
}
}
27 changes: 25 additions & 2 deletions docs/CONFIGURATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ rule {
value = "(.*)"
}
}
match { ... }
match { ... }
ignore {
path = "(.+)"
name = "(.+)"
Expand All @@ -151,6 +153,8 @@ rule {
value = "(.*)"
}
}
ignore { ... }
ignore { ... }

[ check definition ]
...
Expand All @@ -172,7 +176,11 @@ rule {
- `ignore` - works exactly like `match` but does the opposite - any alerting or recording rule
matching all conditions defined on `ignore` will not be checked by this `rule` block.

Example:
Note: both `match` and `ignore` require all defined filters to be satisfied to work.
If multiple `match` and/or `ignore` rules are present any of then needs to match for the rule to
be matched / ignored.

Examples:

```JS
rule {
Expand All @@ -182,8 +190,23 @@ rule {
label "severity" {
value = "(warning|critical)"
}
[ check applied only to severity="critical" and severity="warning" alerts ]
}
ignore {
command = "watch"
}
[ check applied only to severity="critical" and severity="warning" alerts in "ci" or "lint" command is run ]
}
```

```JS
rule {
ignore {
command = "watch"
}
ignore {
command = "lint"
}
[ check applied unless "watch" or "lint" command is run ]
}
```

Expand Down
Loading

0 comments on commit a34cb6e

Please sign in to comment.