Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
26 changes: 25 additions & 1 deletion pkg/rules/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package rules

import (
"context"
"os"
"sort"
"sync"
"text/template"
Expand All @@ -14,7 +15,7 @@ import (
"github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/promql/parser"
"github.com/prometheus/prometheus/storage"

"github.com/thanos-io/thanos/pkg/errutil"
"github.com/thanos-io/thanos/pkg/rules/rulespb"
"github.com/thanos-io/thanos/pkg/tracing"
)
Expand Down Expand Up @@ -248,3 +249,26 @@ func (srv *rulesServer) Send(res *rulespb.RulesResponse) error {
func (srv *rulesServer) Context() context.Context {
return srv.ctx
}

func CheckRulesFiles(filePaths []string) error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont see that this is called anywhere; why is it needed?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just like the issue I mentioned above, we need a method to replace rulefmt.ParseFile in prometheus since thanos change the alert rule file and and rulefmt.ParseFile cannot chech the changed file.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any way we could use it in the code-base itself? I'm afraid that this logic will get stale.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, the key func is ValidateAndCount(), which is used by thanos CLI tool,too.

var failed errutil.MultiError

for _, path := range filePaths {
f, er := os.Open(path)
if er != nil {
failed.Add(er)
continue
}
defer func() { _ = f.Close() }()

_, errs := ValidateAndCount(f)
if errs.Err() != nil {
for _, e := range errs {
failed.Add(e)
}
continue
}
}

return failed.Err()
}
21 changes: 21 additions & 0 deletions pkg/rules/rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1390,3 +1390,24 @@ func TestFilterRules(t *testing.T) {
})
}
}

func TestCheckRulesFiles(t *testing.T) {

validFiles := []string{
"../../cmd/thanos/testdata/rules-files/valid.yaml",
}

invalidFiles := [][]string{
{"../../cmd/thanos/testdata/rules-files/non-existing-file.yaml"},
{"../../cmd/thanos/testdata/rules-files/invalid-yaml-format.yaml"},
{"../../cmd/thanos/testdata/rules-files/invalid-rules-data.yaml"},
{"../../cmd/thanos/testdata/rules-files/invalid-unknown-field.yaml"},
}

testutil.Ok(t, CheckRulesFiles(validFiles))

for _, fn := range invalidFiles {
testutil.NotOk(t, CheckRulesFiles(fn), "expected err for file %s", fn)
}

}