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 2 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
74 changes: 74 additions & 0 deletions pkg/ruler/rulespb/compat_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
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