Skip to content

Commit

Permalink
Run same rule validation as Prometheus
Browse files Browse the repository at this point in the history
  • Loading branch information
prymitive committed Jun 16, 2022
1 parent 941dee2 commit 014cdb2
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 23 deletions.
6 changes: 3 additions & 3 deletions cmd/pint/tests/0006_rr_labels.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ 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
rules/0001.yml:8: incomplete rule, no alert or record key (yaml/parse)
- expr: sum(foo)
level=error msg="Failed to unmarshal file content" error="0:0: group \"foo\", rule 2, \"\": one of 'record' or 'alert' must be set" lines=1-10 path=rules/0001.yml
rules/0001.yml:1: 0:0: group "foo", rule 2, "": one of 'record' or 'alert' must be set (yaml/parse)
groups:

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

-- stderr.txt --
level=error msg="Failed to unmarshal file content" error="yaml: unmarshal errors:\n line 2: field alert not found in type rulefmt.RuleGroup\n line 3: field expr not found in type rulefmt.RuleGroup\n line 4: field for not found in type rulefmt.RuleGroup\n line 5: field labels not found in type rulefmt.RuleGroup\n line 8: field annotations not found in type rulefmt.RuleGroup" lines=1-9 path=rules/strict.yml
rules/strict.yml:2: field alert not found in type rulefmt.RuleGroup (yaml/parse)
- alert: Conntrack_Table_Almost_Full

level=info msg="Problems found" Fatal=1
level=fatal msg="Fatal error" error="problems found"
-- rules/strict.yml --
groups:
- alert: Conntrack_Table_Almost_Full
expr: ((node_nf_conntrack_entries / node_nf_conntrack_entries_limit) * 100) > 75
for: 5m
labels:
component: conntrack
priority: "3"
annotations:
summary: Conntrack table is at {{ $value|humanize }}%
5 changes: 5 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
the results. Previously it would try to run a single query for a whole week
and if that failed it would reduce time range until a query would succeed.

### Fixed

- Strict parsing mode didn't fully validate rule group files, this is now fixed
and pint runs the same set of checks as Prometheus.

## v0.21.1

### Fixed
Expand Down
30 changes: 15 additions & 15 deletions internal/discovery/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"strings"

"github.com/prometheus/prometheus/model/rulefmt"
"gopkg.in/yaml.v3"

"github.com/cloudflare/pint/internal/output"
"github.com/cloudflare/pint/internal/parser"
Expand Down Expand Up @@ -53,20 +52,21 @@ func readFile(path string, isStrict bool) (entries []Entry, err error) {
fileOwner, _ := parser.GetComment(string(content), FileOwnerComment)

if isStrict {
var r rulefmt.RuleGroups
if err = yaml.Unmarshal(content, &r); err != nil {
log.Error().
Err(err).
Str("path", path).
Str("lines", output.FormatLineRangeString(contentLines)).
Msg("Failed to unmarshal file content")
entries = append(entries, Entry{
Path: path,
PathError: err,
Owner: fileOwner.Value,
ModifiedLines: contentLines,
})
return entries, nil
if _, errs := rulefmt.Parse(content); len(errs) > 0 {
for _, err := range errs {
log.Error().
Err(err).
Str("path", path).
Str("lines", output.FormatLineRangeString(contentLines)).
Msg("Failed to unmarshal file content")
entries = append(entries, Entry{
Path: path,
PathError: err,
Owner: fileOwner.Value,
ModifiedLines: contentLines,
})
return entries, nil
}
}
}

Expand Down
6 changes: 2 additions & 4 deletions internal/discovery/glob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (

"github.com/prometheus/prometheus/model/rulefmt"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"

"github.com/cloudflare/pint/internal/discovery"
"github.com/cloudflare/pint/internal/parser"
Expand All @@ -33,8 +32,7 @@ func TestGlobPathFinder(t *testing.T) {
testRules, err := p.Parse([]byte(testRuleBody))
require.NoError(t, err)

var r rulefmt.RuleGroups
strictErr := yaml.Unmarshal([]byte(testRuleBody), &r)
_, strictErrs := rulefmt.Parse([]byte(testRuleBody))

testCases := []testCaseT{
{
Expand Down Expand Up @@ -92,7 +90,7 @@ func TestGlobPathFinder(t *testing.T) {
entries: []discovery.Entry{
{
Path: "bar.yml",
PathError: strictErr,
PathError: strictErrs[0],
ModifiedLines: []int{1, 2, 3, 4},
Owner: "bob",
},
Expand Down
2 changes: 1 addition & 1 deletion internal/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestParse(t *testing.T) {
shouldError: true,
},
{
content: []byte(string("- 0: 0\n 00000000: 000000\n 000000:00000000000: 00000000\n 00000000000:000000: 0000000000000000000000000000000000\n 000000: 0000000\n expr: |")),
content: []byte("- 0: 0\n 00000000: 000000\n 000000:00000000000: 00000000\n 00000000000:000000: 0000000000000000000000000000000000\n 000000: 0000000\n expr: |"),
output: []parser.Rule{
{Error: parser.ParseError{Err: fmt.Errorf("incomplete rule, no alert or record key"), Line: 6}},
},
Expand Down

0 comments on commit 014cdb2

Please sign in to comment.