Skip to content

Commit

Permalink
feat: added thanos check rules command
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Chodur <m.chodur@seznam.cz>
  • Loading branch information
FUSAKLA committed Apr 30, 2019
1 parent 8498d91 commit 909adbb
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 0 deletions.
113 changes: 113 additions & 0 deletions cmd/thanos/check.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package main

import (
"fmt"
"io/ioutil"
"os"

"github.com/go-kit/kit/log"
thanosrule "github.com/improbable-eng/thanos/pkg/rule"
"github.com/oklog/run"
"github.com/opentracing/opentracing-go"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/prometheus/pkg/rulefmt"
"gopkg.in/alecthomas/kingpin.v2"
"gopkg.in/yaml.v2"
)

func registerChecks(m map[string]setupFunc, app *kingpin.Application, name string) {
cmd := app.Command(name, "Linting tools for Thanos")
registerRulesCheck(m, cmd, name)
return
}

func registerRulesCheck(m map[string]setupFunc, root *kingpin.CmdClause, name string) {
checkRulesCmd := root.Command("rules", "Check if the rule files are valid or not.")
ruleFiles := checkRulesCmd.Arg(
"rule-files",
"The rule files to check.",
).Required().ExistingFiles()

m[name+" rules"] = func(g *run.Group, logger log.Logger, reg *prometheus.Registry, _ opentracing.Tracer, _ bool) error {
// Dummy actor to immediately kill the group after the run function returns.
g.Add(func() error { return nil }, func(error) {})
return checkRulesFiles(ruleFiles)
}
}

func checkRulesFiles(files *[]string) error {
var failed error = nil

for _, f := range *files {
if n, errs := checkRules(f); errs != nil {
fmt.Fprintln(os.Stderr, " FAILED:")
for _, e := range errs {
fmt.Fprintln(os.Stderr, e.Error())
}
failed = errs[0]
} else {
fmt.Printf(" SUCCESS: %d rules found\n", n)
}
fmt.Println()
}
if failed != nil {
return failed
}
return nil
}

func checkRules(filename string) (int, []error) {
fmt.Println("Checking", filename)

b, err := ioutil.ReadFile(filename)
if err != nil {
return 0, []error{err}
}

var rgs thanosrule.RuleGroups
if err := yaml.Unmarshal(b, &rgs); err != nil {
return 0, []error{err}
}

// We need to convert Thanos rules to Prometheus rules so we can use their validation
promRgs := thanosRuleGroupsToPromRuleGroups(rgs)
if errs := promRgs.Validate(); errs != nil {
return 0, errs
}

numRules := 0
for _, rg := range rgs.Groups {
numRules += len(rg.Rules)
}

return numRules, nil
}

func thanosRuleGroupsToPromRuleGroups(ruleGroups thanosrule.RuleGroups) rulefmt.RuleGroups {
promRuleGroups := rulefmt.RuleGroups{Groups: []rulefmt.RuleGroup{}}
for _, g := range ruleGroups.Groups {
group := rulefmt.RuleGroup{
Name: g.Name,
Interval: g.Interval,
Rules: []rulefmt.Rule{},
}
for _, r := range g.Rules {
group.Rules = append(
group.Rules,
rulefmt.Rule{
Record: r.Record,
Alert: r.Alert,
Expr: r.Expr,
For: r.For,
Labels: r.Labels,
Annotations: r.Annotations,
},
)
}
promRuleGroups.Groups = append(
promRuleGroups.Groups,
group,
)
}
return promRuleGroups
}
1 change: 1 addition & 0 deletions cmd/thanos/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func main() {
registerBucket(cmds, app, "bucket")
registerDownsample(cmds, app, "downsample")
registerReceive(cmds, app, "receive")
registerChecks(cmds, app, "check")

cmd, err := app.Parse(os.Args[1:])
if err != nil {
Expand Down

0 comments on commit 909adbb

Please sign in to comment.