Skip to content

Commit

Permalink
Fixed after rebase.
Browse files Browse the repository at this point in the history
Signed-off-by: Bartlomiej Plotka <bwplotka@gmail.com>
  • Loading branch information
bwplotka committed May 22, 2020
1 parent 50bc8dd commit fe0fac4
Show file tree
Hide file tree
Showing 8 changed files with 177 additions and 208 deletions.
87 changes: 2 additions & 85 deletions cmd/thanos/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,16 @@
package main

import (
"fmt"
"io/ioutil"

"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/oklog/run"
"github.com/opentracing/opentracing-go"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/prometheus/pkg/rulefmt"
tsdb_errors "github.com/prometheus/prometheus/tsdb/errors"
"github.com/thanos-io/thanos/pkg/rules"
"gopkg.in/alecthomas/kingpin.v2"
"gopkg.in/yaml.v2"
yamlv3 "gopkg.in/yaml.v3"

thanosrule "github.com/thanos-io/thanos/pkg/rule"
)

func registerTools(m map[string]setupFunc, app *kingpin.Application) {
Expand Down Expand Up @@ -62,64 +56,6 @@ func checkRulesFiles(logger log.Logger, files *[]string) error {
return nil
}

type ThanosRuleGroup struct {
PartialResponseStrategy string `yaml:"partial_response_strategy"`
thanosrule.PromRuleGroup `yaml:",inline"`
}

type ThanosRuleGroups struct {
Groups []ThanosRuleGroup `yaml:"groups"`
}

// Validate validates all rules in the rule groups.
// This is copied from Prometheus.
// TODO: Replace this with upstream implementation after https://github.com/prometheus/prometheus/issues/7128 is fixed.
func (g *ThanosRuleGroups) Validate() (errs []error) {
set := map[string]struct{}{}

for _, g := range g.Groups {
if g.Name == "" {
errs = append(errs, errors.New("Groupname should not be empty"))
}

if _, ok := set[g.Name]; ok {
errs = append(
errs,
fmt.Errorf("groupname: %q is repeated in the same file", g.Name),
)
}

set[g.Name] = struct{}{}

for i, r := range g.Rules {
ruleNode := rulefmt.RuleNode{
Record: yamlv3.Node{Value: r.Record},
Alert: yamlv3.Node{Value: r.Alert},
Expr: yamlv3.Node{Value: r.Expr},
For: r.For,
Labels: r.Labels,
Annotations: r.Annotations,
}
for _, node := range ruleNode.Validate() {
var ruleName string
if r.Alert != "" {
ruleName = r.Alert
} else {
ruleName = r.Record
}
errs = append(errs, &rulefmt.Error{
Group: g.Name,
Rule: i,
RuleName: ruleName,
Err: node,
})
}
}
}

return errs
}

func checkRules(logger log.Logger, filename string) (int, tsdb_errors.MultiError) {
level.Info(logger).Log("msg", "checking", "filename", filename)
checkErrors := tsdb_errors.MultiError{}
Expand All @@ -129,24 +65,5 @@ func checkRules(logger log.Logger, filename string) (int, tsdb_errors.MultiError
checkErrors.Add(err)
return 0, checkErrors
}

var rgs ThanosRuleGroups
if err := yaml.UnmarshalStrict(b, &rgs); err != nil {
checkErrors.Add(err)
return 0, checkErrors
}

if errs := rgs.Validate(); errs != nil {
for _, e := range errs {
checkErrors.Add(e)
}
return 0, checkErrors
}

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

return numRules, checkErrors
return rules.ValidateAndCount(b)
}
30 changes: 0 additions & 30 deletions pkg/query/iter.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,36 +100,6 @@ func (s *promSeriesSet) Err() error {
return s.set.Err()
}

func translateMatcher(m *labels.Matcher) (storepb.LabelMatcher, error) {
var t storepb.LabelMatcher_Type

switch m.Type {
case labels.MatchEqual:
t = storepb.LabelMatcher_EQ
case labels.MatchNotEqual:
t = storepb.LabelMatcher_NEQ
case labels.MatchRegexp:
t = storepb.LabelMatcher_RE
case labels.MatchNotRegexp:
t = storepb.LabelMatcher_NRE
default:
return storepb.LabelMatcher{}, errors.Errorf("unrecognized matcher type %d", m.Type)
}
return storepb.LabelMatcher{Type: t, Name: m.Name, Value: m.Value}, nil
}

func translateMatchers(ms ...*labels.Matcher) ([]storepb.LabelMatcher, error) {
res := make([]storepb.LabelMatcher, 0, len(ms))
for _, m := range ms {
r, err := translateMatcher(m)
if err != nil {
return nil, err
}
res = append(res, r)
}
return res, nil
}

// storeSeriesSet implements a storepb SeriesSet against a list of storepb.Series.
type storeSeriesSet struct {
// TODO(bwplotka): Don't buffer all, we have to buffer single series (to sort and dedup chunks), but nothing more.
Expand Down
2 changes: 1 addition & 1 deletion pkg/query/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ func (q *querier) Select(_ bool, hints *storage.SelectHints, ms ...*labels.Match
})
defer span.Finish()

sms, err := translateMatchers(ms...)
sms, err := storepb.TranslatePromMatchers(ms...)
if err != nil {
return nil, nil, errors.Wrap(err, "convert matchers")
}
Expand Down
Loading

0 comments on commit fe0fac4

Please sign in to comment.