Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
Signed-off-by: Ganesh Vernekar <[email protected]>
  • Loading branch information
codesome committed May 14, 2020
1 parent 2bbe4a8 commit caf78bd
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 43 deletions.
8 changes: 4 additions & 4 deletions cmd/thanos/receive.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func registerReceive(m map[string]setupFunc, app *kingpin.Application) {

objStoreConfig := regCommonObjStoreFlags(cmd, "", false)

retention := modelDuration(cmd.Flag("tsdb.retention", "How long to retain raw samples on local storage. 0d - disables this retention").Default("15d"))
retention := cmd.Flag("tsdb.retention", "How long to retain raw samples on local storage. 0d - disables this retention").Default("15d").Int64()

hashringsFile := cmd.Flag("receive.hashrings-file", "Path to file that contains the hashring configuration.").
PlaceHolder("<path>").String()
Expand Down Expand Up @@ -102,9 +102,9 @@ func registerReceive(m map[string]setupFunc, app *kingpin.Application) {
}

tsdbOpts := &tsdb.Options{
MinBlockDuration: *tsdbMinBlockDuration,
MaxBlockDuration: *tsdbMaxBlockDuration,
RetentionDuration: int64(*retention),
MinBlockDuration: *tsdbMinBlockDuration / int64(time.Millisecond),
MaxBlockDuration: *tsdbMaxBlockDuration / int64(time.Millisecond),
RetentionDuration: *retention / int64(time.Millisecond),
NoLockfile: true,
WALCompression: *walCompression,
}
Expand Down
23 changes: 17 additions & 6 deletions cmd/thanos/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ import (
tsdb_errors "github.com/prometheus/prometheus/tsdb/errors"
"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 @@ -60,8 +63,8 @@ func checkRulesFiles(logger log.Logger, files *[]string) error {
}

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

type ThanosRuleGroups struct {
Expand Down Expand Up @@ -89,12 +92,20 @@ func (g *ThanosRuleGroups) Validate() (errs []error) {
set[g.Name] = struct{}{}

for i, r := range g.Rules {
for _, node := range r.Validate() {
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.Value != "" {
ruleName = r.Alert.Value
if r.Alert != "" {
ruleName = r.Alert
} else {
ruleName = r.Record.Value
ruleName = r.Record
}
errs = append(errs, &rulefmt.Error{
Group: g.Name,
Expand Down
2 changes: 1 addition & 1 deletion pkg/query/api/v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ func (api *API) series(r *http.Request) (interface{}, []error, *ApiError) {
sets = append(sets, s)
}

set := storage.NewMergeSeriesSet(sets, nil)
set := storage.NewMergeSeriesSet(sets, storage.ChainedSeriesMerge)
for set.Next() {
metrics = append(metrics, set.At().Labels())
}
Expand Down
19 changes: 13 additions & 6 deletions pkg/query/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,14 +160,21 @@ func aggrsFromFunc(f string) []storepb.Aggr {
return []storepb.Aggr{storepb.Aggr_COUNT, storepb.Aggr_SUM}
}

func (q *querier) Select(_ bool, _ *storage.SelectHints, ms ...*labels.Matcher) (storage.SeriesSet, storage.Warnings, error) {
func (q *querier) Select(_ bool, hints *storage.SelectHints, ms ...*labels.Matcher) (storage.SeriesSet, storage.Warnings, error) {
if hints == nil {
hints = &storage.SelectHints{
Start: q.mint,
End: q.maxt,
}
}

matchers := make([]string, len(ms))
for i, m := range ms {
matchers[i] = m.String()
}
span, ctx := tracing.StartSpan(q.ctx, "querier_select", opentracing.Tags{
"minTime": q.mint,
"maxTime": q.maxt,
"minTime": hints.Start,
"maxTime": hints.End,
"matchers": "{" + strings.Join(matchers, ",") + "}",
})
defer span.Finish()
Expand All @@ -177,12 +184,12 @@ func (q *querier) Select(_ bool, _ *storage.SelectHints, ms ...*labels.Matcher)
return nil, nil, errors.Wrap(err, "convert matchers")
}

aggrs := aggrsFromFunc("")
aggrs := aggrsFromFunc(hints.Func)

resp := &seriesServer{ctx: ctx}
if err := q.proxy.Series(&storepb.SeriesRequest{
MinTime: q.mint,
MaxTime: q.maxt,
MinTime: hints.Start,
MaxTime: hints.End,
Matchers: sms,
MaxResolutionWindow: q.maxResolutionMillis,
Aggregates: aggrs,
Expand Down
32 changes: 23 additions & 9 deletions pkg/rule/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"time"

"github.com/pkg/errors"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/pkg/rulefmt"
"github.com/prometheus/prometheus/rules"
tsdberrors "github.com/prometheus/prometheus/tsdb/errors"
Expand Down Expand Up @@ -42,8 +43,18 @@ type RuleGroups struct {
Groups []RuleGroup `yaml:"groups"`
}

// ruleGroup is a list of sequentially evaluated recording and alerting rules.
// This is a modified version from Prometheus. In Prometheus they shifted to
// using yaml.Node as elements which gives problems in marshal/unmarshal.
// Hence this replaces the yaml.Node to strings.
type PromRuleGroup struct {
Name string `yaml:"name"`
Interval model.Duration `yaml:"interval,omitempty"`
Rules []rulefmt.Rule `yaml:"rules"`
}

type RuleGroup struct {
rulefmt.RuleGroup
PromRuleGroup
PartialResponseStrategy *storepb.PartialResponseStrategy
}

Expand Down Expand Up @@ -103,7 +114,7 @@ func (r *RuleGroup) UnmarshalYAML(unmarshal func(interface{}) error) error {
return errors.Wrap(err, errMsg)
}

rg := rulefmt.RuleGroup{}
rg := PromRuleGroup{}
if err := unmarshal(&rg); err != nil {
return errors.Wrap(err, "failed to unmarshal rulefmt.RuleGroup")
}
Expand All @@ -119,7 +130,7 @@ func (r *RuleGroup) UnmarshalYAML(unmarshal func(interface{}) error) error {
}

ps := storepb.PartialResponseStrategy(p)
r.RuleGroup = rg
r.PromRuleGroup = rg
r.PartialResponseStrategy = &ps
return nil
}
Expand All @@ -132,10 +143,10 @@ func (r RuleGroup) MarshalYAML() (interface{}, error) {
}

rs := struct {
RuleGroup rulefmt.RuleGroup `yaml:",inline"`
PartialResponseStrategy *string `yaml:"partial_response_strategy,omitempty"`
RuleGroup PromRuleGroup `yaml:",inline"`
PartialResponseStrategy *string `yaml:"partial_response_strategy,omitempty"`
}{
RuleGroup: r.RuleGroup,
RuleGroup: r.PromRuleGroup,
PartialResponseStrategy: ps,
}
return rs, nil
Expand Down Expand Up @@ -172,15 +183,18 @@ func (m *Manager) Update(evalInterval time.Duration, files []string) error {

// NOTE: This is very ugly, but we need to reparse it into tmp dir without the field to have to reuse
// rules.Manager. The problem is that it uses yaml.UnmarshalStrict for some reasons.
groupsByStrategy := map[storepb.PartialResponseStrategy]*rulefmt.RuleGroups{}
groupsByStrategy := map[storepb.PartialResponseStrategy]*RuleGroups{}
for _, rg := range rg.Groups {
if _, ok := groupsByStrategy[*rg.PartialResponseStrategy]; !ok {
groupsByStrategy[*rg.PartialResponseStrategy] = &rulefmt.RuleGroups{}
groupsByStrategy[*rg.PartialResponseStrategy] = &RuleGroups{}
}

groupsByStrategy[*rg.PartialResponseStrategy].Groups = append(
groupsByStrategy[*rg.PartialResponseStrategy].Groups,
rg.RuleGroup,
RuleGroup{
PromRuleGroup: rg.PromRuleGroup,
PartialResponseStrategy: rg.PartialResponseStrategy,
},
)
}

Expand Down
25 changes: 8 additions & 17 deletions pkg/rule/rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"github.com/thanos-io/thanos/pkg/store/storepb"
"github.com/thanos-io/thanos/pkg/testutil"
"gopkg.in/yaml.v2"
yamlv3 "gopkg.in/yaml.v3"
)

type nopAppendable struct{}
Expand Down Expand Up @@ -247,31 +246,23 @@ func TestRuleGroupMarshalYAML(t *testing.T) {
var input = RuleGroups{
Groups: []RuleGroup{
{
RuleGroup: rulefmt.RuleGroup{
PromRuleGroup: PromRuleGroup{
Name: "something1",
Rules: []rulefmt.RuleNode{
Rules: []rulefmt.Rule{
{
Alert: yamlv3.Node{
Value: "some",
},
Expr: yamlv3.Node{
Value: "up",
},
Alert: "some",
Expr: "up",
},
},
},
},
{
RuleGroup: rulefmt.RuleGroup{
PromRuleGroup: PromRuleGroup{
Name: "something2",
Rules: []rulefmt.RuleNode{
Rules: []rulefmt.Rule{
{
Alert: yamlv3.Node{
Value: "some",
},
Expr: yamlv3.Node{
Value: "rate(some_metric[1h:5m] offset 1d)",
},
Alert: "some",
Expr: "rate(some_metric[1h:5m] offset 1d)",
},
},
},
Expand Down

0 comments on commit caf78bd

Please sign in to comment.