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 support for keep_firing_for field in alert rules #5823

Merged
merged 3 commits into from
Mar 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
22 changes: 12 additions & 10 deletions pkg/ruler/rulespb/compat.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ func formattedRuleToProto(rls []rulefmt.RuleNode) []*RuleDesc {
rules := make([]*RuleDesc, len(rls))
for i := range rls {
rules[i] = &RuleDesc{
Expr: rls[i].Expr.Value,
Record: rls[i].Record.Value,
Alert: rls[i].Alert.Value,
For: time.Duration(rls[i].For),
Labels: cortexpb.FromLabelsToLabelAdapters(labels.FromMap(rls[i].Labels)),
Annotations: cortexpb.FromLabelsToLabelAdapters(labels.FromMap(rls[i].Annotations)),
Expr: rls[i].Expr.Value,
Record: rls[i].Record.Value,
Alert: rls[i].Alert.Value,
For: time.Duration(rls[i].For),
KeepFiringFor: time.Duration(rls[i].KeepFiringFor),
mustafain117 marked this conversation as resolved.
Show resolved Hide resolved
Labels: cortexpb.FromLabelsToLabelAdapters(labels.FromMap(rls[i].Labels)),
Annotations: cortexpb.FromLabelsToLabelAdapters(labels.FromMap(rls[i].Annotations)),
}
}

Expand All @@ -54,10 +55,11 @@ func FromProto(rg *RuleGroupDesc) rulefmt.RuleGroup {
exprNode.SetString(rl.GetExpr())

newRule := rulefmt.RuleNode{
Expr: exprNode,
Labels: cortexpb.FromLabelAdaptersToLabels(rl.Labels).Map(),
Annotations: cortexpb.FromLabelAdaptersToLabels(rl.Annotations).Map(),
For: model.Duration(rl.GetFor()),
Expr: exprNode,
Labels: cortexpb.FromLabelAdaptersToLabels(rl.Labels).Map(),
Annotations: cortexpb.FromLabelAdaptersToLabels(rl.Annotations).Map(),
For: model.Duration(rl.GetFor()),
KeepFiringFor: model.Duration(rl.GetKeepFiringFor()),
}

if rl.GetRecord() != "" {
Expand Down
49 changes: 49 additions & 0 deletions pkg/ruler/rulespb/compat_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package rulespb

import (
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/model/rulefmt"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"
yeya24 marked this conversation as resolved.
Show resolved Hide resolved
"testing"
"time"
)

func TestProto(t *testing.T) {
rules := make([]rulefmt.RuleNode, 0)

alertNode := yaml.Node{}
alertNode.SetString("test_rule")
exprNode := yaml.Node{}
exprNode.SetString("test_expr")

testRule := rulefmt.RuleNode{
Alert: alertNode,
Expr: exprNode,
Labels: map[string]string{"label1": "value1"},
Annotations: map[string]string{"key1": "value1"},
For: model.Duration(time.Minute * 2),
KeepFiringFor: model.Duration(time.Hour),
}

rules = append(rules, testRule)

rg := rulefmt.RuleGroup{
Name: "group1",
Rules: rules,
Interval: model.Duration(time.Minute),
}
desc := ToProto("test", "namespace", rg)

assert.Equal(t, len(rules), len(desc.Rules))

ruleDesc := desc.Rules[0]

assert.Equal(t, "test_rule", ruleDesc.Alert)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we also verify the keep firing for in alerts field?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added an integration test that validates the KeepFiringSince field in alerts

assert.Equal(t, "test_expr", ruleDesc.Expr)
assert.Equal(t, time.Minute*2, ruleDesc.For)
assert.Equal(t, time.Hour, ruleDesc.KeepFiringFor)

formatted := FromProto(desc)
assert.Equal(t, rg, formatted)
}
Loading