Skip to content

Commit

Permalink
Rule: Support ruleGroup limit (thanos-io#4837)
Browse files Browse the repository at this point in the history
Signed-off-by: Jimmie Han <hanjinming@outlook.com>
  • Loading branch information
hanjm committed Nov 17, 2021
1 parent b0b853b commit 2738f1c
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 63 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -27,6 +27,7 @@ We use *breaking :warning:* to mark changes that are not backward compatible (re
- [#4847](https://github.com/thanos-io/thanos/pull/4847) Query: add `--alert.query-url` which is used in the user interface for rules/alerts pages. By default the HTTP listen address is used for this URL.
- [#4856](https://github.com/thanos-io/thanos/pull/4856) Mixin: Add Query Frontend Grafana dashboard.
- [#4874](https://github.com/thanos-io/thanos/pull/4874) Query: Add `--endpoint-strict` flag to statically configure Thanos API server endpoints. It is similar to `--store-strict` but supports passing any Thanos gRPC APIs: StoreAPI, MetadataAPI, RulesAPI, TargetsAPI and ExemplarsAPI.
- [#4868](https://github.com/thanos-io/thanos/pull/4868) Rule: Support ruleGroup limit introduced by Prometheus v2.31.0.

### Fixed

Expand Down
1 change: 1 addition & 0 deletions pkg/rules/manager.go
Expand Up @@ -44,6 +44,7 @@ func (g Group) toProto() *rulespb.RuleGroup {
Name: g.Name(),
File: g.OriginalFile,
Interval: g.Interval().Seconds(),
Limit: int64(g.Limit()),
PartialResponseStrategy: g.PartialResponseStrategy,
// UTC needed due to https://github.com/gogo/protobuf/issues/519.
LastEvaluation: g.GetLastEvaluation().UTC(),
Expand Down
57 changes: 56 additions & 1 deletion pkg/rules/manager_test.go
Expand Up @@ -293,6 +293,7 @@ func TestConfigRuleAdapterUnmarshalMarshalYAML(t *testing.T) {
- alert: some
expr: up
partial_response_strategy: ABORT
limit: 10
- name: something2
rules:
- alert: some
Expand All @@ -302,7 +303,8 @@ func TestConfigRuleAdapterUnmarshalMarshalYAML(t *testing.T) {
b, err := yaml.Marshal(c)
testutil.Ok(t, err)
testutil.Equals(t, `groups:
- name: something1
- limit: 10
name: something1
rules:
- alert: some
expr: up
Expand Down Expand Up @@ -397,3 +399,56 @@ groups:
testutil.Ok(t, err)
testutil.Equals(t, 0, len(thanosRuleMgr.RuleGroups()))
}

func TestManagerRunRulesWithRuleGroupLimit(t *testing.T) {
dir, err := ioutil.TempDir("", "test_rule_rule_groups")
testutil.Ok(t, err)
defer func() { testutil.Ok(t, os.RemoveAll(dir)) }()
filename := filepath.Join(dir, "with_limit.yaml")
testutil.Ok(t, ioutil.WriteFile(filename, []byte(`
groups:
- name: "something1"
limit: 1
rules:
- alert: "some"
expr: "up>0"
for: 0s
`), os.ModePerm))

thanosRuleMgr := NewManager(
context.Background(),
nil,
dir,
rules.ManagerOptions{
Logger: log.NewLogfmtLogger(os.Stderr),
Queryable: nopQueryable{},
},
func(partialResponseStrategy storepb.PartialResponseStrategy) rules.QueryFunc {
return func(ctx context.Context, q string, ts time.Time) (promql.Vector, error) {
return []promql.Sample{
{
Point: promql.Point{T: 0, V: 1},
Metric: labels.FromStrings("foo", "bar"),
},
{
Point: promql.Point{T: 0, V: 1},
Metric: labels.FromStrings("foo1", "bar1"),
},
}, nil
}
},
nil,
"http://localhost",
)
thanosRuleMgr.Run()
defer func() {
thanosRuleMgr.Stop()
}()
testutil.Ok(t, thanosRuleMgr.Update(time.Millisecond, []string{filename}))
testutil.Equals(t, 1, len(thanosRuleMgr.protoRuleGroups()))
testutil.Equals(t, 1, len(thanosRuleMgr.protoRuleGroups()[0].Rules))
testutil.Equals(t, string(rules.HealthUnknown), thanosRuleMgr.protoRuleGroups()[0].Rules[0].GetAlert().Health)
time.Sleep(time.Millisecond * 2)
testutil.Equals(t, string(rules.HealthBad), thanosRuleMgr.protoRuleGroups()[0].Rules[0].GetAlert().Health)
testutil.Equals(t, "exceeded limit of 1 with 2 alerts", thanosRuleMgr.protoRuleGroups()[0].Rules[0].GetAlert().LastError)
}
151 changes: 90 additions & 61 deletions pkg/rules/rulespb/rpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion pkg/rules/rulespb/rpc.proto
Expand Up @@ -71,7 +71,8 @@ message RuleGroup {
repeated Rule rules = 3 [(gogoproto.jsontag) = "rules" ];
double interval = 4 [(gogoproto.jsontag) = "interval" ];
double evaluation_duration_seconds = 5 [(gogoproto.jsontag) = "evaluationTime" ]; // TODO: Is it really second?
google.protobuf.Timestamp last_evaluation = 6 [(gogoproto.jsontag) = "lastEvaluation", (gogoproto.stdtime) = true, (gogoproto.nullable) = false ];
google.protobuf.Timestamp last_evaluation = 6 [(gogoproto.jsontag) = "lastEvaluation", (gogoproto.stdtime) = true, (gogoproto.nullable) = false];
int64 limit = 9 [(gogoproto.jsontag) = "limit"];

// Thanos specific.
PartialResponseStrategy PartialResponseStrategy = 8 [(gogoproto.jsontag) = "partialResponseStrategy" ];
Expand Down

0 comments on commit 2738f1c

Please sign in to comment.