Skip to content

Commit

Permalink
Don't fail to parse YAML on template errors
Browse files Browse the repository at this point in the history
  • Loading branch information
prymitive committed Jul 21, 2022
1 parent 61689e0 commit 2c2deeb
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 3 deletions.
6 changes: 3 additions & 3 deletions cmd/pint/tests/0081_rulefmt.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ pint.error --no-color lint rules
cmp stderr stderr.txt

-- stderr.txt --
level=error msg="Failed to unmarshal file content" error="0:0: group \"foo\", rule 1, \"foo\": field 'expr' must be set in rule" lines=1-4 path=rules/strict.yml
rules/strict.yml:1: field 'expr' must be set in rule (yaml/parse)
groups:
level=info msg="File parsed" path=rules/strict.yml rules=1
rules/strict.yml:4: missing expr key (yaml/parse)
- record: foo

level=info msg="Problems found" Fatal=1
level=fatal msg="Fatal error" error="problems found"
Expand Down
44 changes: 44 additions & 0 deletions cmd/pint/tests/0086_rulefmt_ignored_errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
pint.error --no-color lint rules
! stdout .
cmp stderr stderr.txt

-- stderr.txt --
level=info msg="File parsed" path=rules/strict.yml rules=5
rules/strict.yml:4: incomplete rule, no alert or record key (yaml/parse)
- expr: MissingAlertOrRecord

rules/strict.yml:7: syntax error: no expression found in input (promql/syntax)
expr:

rules/strict.yml:10: syntax error: unknown function with name "sumz" (promql/syntax)
expr: sumz(0)

rules/strict.yml:15: template parse error: function "bogus" not defined (alerts/template)
dashboard: '{{ bogus }}'

rules/strict.yml:20: template parse error: function "bogus" not defined (alerts/template)
dashboard: '{{ bogus }}'

level=info msg="Problems found" Fatal=5
level=fatal msg="Fatal error" error="problems found"
-- rules/strict.yml --
groups:
- name: foo
rules:
- expr: MissingAlertOrRecord

- alert: MissingExpr
expr:

- record: BadPromQL
expr: sumz(0)

- alert: IgnoreTemplateErrors
expr: up == 0
annotations:
dashboard: '{{ bogus }}'

- alert: IgnoreTemplateErrors
expr: up == 0
labels:
dashboard: '{{ bogus }}'
5 changes: 5 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## v0.26.0

### Fixed

- Strict parsing mode shouldn't fail on template errors, those will be later
reported by `alerts/template` check.

### Added

- [promql/range_query](checks/promql/range_query.md) check.
Expand Down
2 changes: 2 additions & 0 deletions internal/discovery/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ const (

var ignoredErrors = []string{
"one of 'record' or 'alert' must be set",
"field 'expr' must be set in rule",
"could not parse expression: ",
"cannot unmarshal !!seq into rulefmt.ruleGroups",
": template: __",
}

func isStrictIgnored(err error) bool {
Expand Down

0 comments on commit 2c2deeb

Please sign in to comment.