Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix persistence of rulegroup evaluation delay. #3392

Merged
merged 3 commits into from
Nov 4, 2022
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
* [BUGFIX] Flusher: Add `Overrides` as a dependency to prevent panics when starting with `-target=flusher`. #3151
* [BUGFIX] Updated `golang.org/x/text` dependency to fix CVE-2022-32149. #3285
* [BUGFIX] Query-frontend: properly close gRPC streams to the query-scheduler to stop memory and goroutines leak. #3302
* [BUGFIX] Ruler: persist evaluation delay configured in the rulegroup. #3392

### Mixin

Expand Down
7 changes: 7 additions & 0 deletions pkg/ruler/rulespb/compat.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ func ToProto(user string, namespace string, rl rulefmt.RuleGroup) *RuleGroupDesc
User: user,
SourceTenants: rl.SourceTenants,
}
if rl.EvaluationDelay != nil && *rl.EvaluationDelay > 0 {
rg.EvaluationDelay = time.Duration(*rl.EvaluationDelay)
}
return &rg
}

Expand Down Expand Up @@ -53,6 +56,10 @@ func FromProto(rg *RuleGroupDesc) rulefmt.RuleGroup {
Rules: make([]rulefmt.RuleNode, len(rg.GetRules())),
SourceTenants: rg.GetSourceTenants(),
}
if rg.EvaluationDelay > 0 {
formattedRuleGroup.EvaluationDelay = new(model.Duration)
*formattedRuleGroup.EvaluationDelay = model.Duration(rg.EvaluationDelay)
}

for i, rl := range rg.GetRules() {
exprNode := yaml.Node{}
Expand Down
76 changes: 76 additions & 0 deletions pkg/ruler/rulespb/compat_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// SPDX-License-Identifier: AGPL-3.0-only

package rulespb

import (
"testing"

"github.com/prometheus/prometheus/model/rulefmt"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
)

func TestRoundtrip(t *testing.T) {
for name, group := range map[string]string{
"no eval delay": `
name: testrules
rules:
- record: test_metric:sum:rate1m
expr: sum(rate(test_metric[1m]))

- alert: ThisIsBad
expr: sum(rate(test_metric[2m]))
for: 10m
`,

"with eval delay": `
name: testrules
evaluation_delay: 3m
rules:
- record: test_metric:sum:rate1m
expr: sum(rate(test_metric[1m]))
`,

"with eval delay and source tenants": `
name: testrules
evaluation_delay: 3m
source_tenants:
- a
- b
rules:
- record: test_metric:sum:rate1m
expr: sum(rate(test_metric[1m]))
`,
} {
t.Run(name, func(t *testing.T) {
rg := rulefmt.RuleGroup{}
require.NoError(t, yaml.Unmarshal([]byte(group), &rg))

desc := ToProto("user", "namespace", rg)
newRg := FromProto(desc)

newYaml, err := yaml.Marshal(newRg)
require.NoError(t, err)

assert.YAMLEq(t, group, string(newYaml))
})
}
}

func TestZeroEvalDelayIsIgnored(t *testing.T) {
const group = `
name: testrules
evaluation_delay: 0s
rules:
- record: test_metric:sum:rate1m
expr: sum(rate(test_metric[1m]))
`
rg := rulefmt.RuleGroup{}
require.NoError(t, yaml.Unmarshal([]byte(group), &rg))

desc := ToProto("user", "namespace", rg)
newRg := FromProto(desc)

assert.Nil(t, newRg.EvaluationDelay)
}
149 changes: 103 additions & 46 deletions pkg/ruler/rulespb/rules.pb.go

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

4 changes: 2 additions & 2 deletions pkg/ruler/rulespb/rules.proto
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ message RuleGroupDesc {
reserved 5, 7, 8;
string name = 1;
string namespace = 2;
google.protobuf.Duration interval = 3
[(gogoproto.nullable) = false, (gogoproto.stdduration) = true];
google.protobuf.Duration interval = 3 [(gogoproto.nullable) = false, (gogoproto.stdduration) = true];
repeated RuleDesc rules = 4;
string user = 6;
// The options field can be used to extend Mimir Ruler functionality without
Expand All @@ -32,6 +31,7 @@ message RuleGroupDesc {
// to the Prometheus Manager.
repeated google.protobuf.Any options = 9;
repeated string sourceTenants = 10;
google.protobuf.Duration evaluationDelay = 11 [(gogoproto.nullable) = false, (gogoproto.stdduration) = true];
}

// RuleDesc is a proto representation of a Prometheus Rule
Expand Down