Apply templating on alert notifications on OK state (#47355)

* OK notifications using previous evaluation data

* copy rule.EvalMatches to avoid changes to the underlying array

* test cases added/modified

* delete trailing newline

* fix double newline in go import

* add change to the changelog

* specify that this only affects legacy alerting (changelog)

* use current eval data instead of prev eval data

* create evalMatch just once

* code comments, renamings, getTemplateMatches() function

* changelog and docs updated
This commit is contained in:
Santiago
2022-04-13 17:04:10 -03:00
committed by GitHub
parent e00db6a826
commit 5fb80498b1
8 changed files with 102 additions and 9 deletions
@@ -340,3 +340,66 @@ func TestEvaluateTemplate(t *testing.T) {
})
}
}
func TestEvaluateNotificationTemplateFields(t *testing.T) {
tests := []struct {
name string
evalMatches []*EvalMatch
allMatches []*EvalMatch
expectedName string
expectedMessage string
}{
{
"with evaluation matches",
[]*EvalMatch{{
Tags: map[string]string{"value1": "test1", "value2": "test2"},
}},
[]*EvalMatch{{
Tags: map[string]string{"value1": "test1", "value2": "test2"},
}},
"Rule name: test1",
"Rule message: test2",
},
{
"missing key",
[]*EvalMatch{{
Tags: map[string]string{"value1": "test1", "value3": "test2"},
}},
[]*EvalMatch{{
Tags: map[string]string{"value1": "test1", "value3": "test2"},
}},
"Rule name: test1",
"Rule message: ${value2}",
},
{
"no evaluation matches, with series",
[]*EvalMatch{},
[]*EvalMatch{{
Tags: map[string]string{"value1": "test1", "value2": "test2"},
}},
"Rule name: test1",
"Rule message: test2",
},
{
"no evaluation matches, no series",
[]*EvalMatch{},
[]*EvalMatch{},
"Rule name: ${value1}",
"Rule message: ${value2}",
},
}
for _, test := range tests {
t.Run(test.name, func(tt *testing.T) {
evalContext := NewEvalContext(context.Background(), &Rule{Name: "Rule name: ${value1}", Message: "Rule message: ${value2}", Conditions: []Condition{&conditionStub{firing: true}}}, &validations.OSSPluginRequestValidator{}, nil)
evalContext.EvalMatches = test.evalMatches
evalContext.AllMatches = test.allMatches
err := evalContext.evaluateNotificationTemplateFields()
require.NoError(tt, err)
require.Equal(tt, test.expectedName, evalContext.Rule.Name)
require.Equal(tt, test.expectedMessage, evalContext.Rule.Message)
})
}
}