Skip to content

Commit

Permalink
adding support for env/file provider expansion for annotations in ale…
Browse files Browse the repository at this point in the history
…rt rule. see: grafana#84250

Signed-off-by: Syed Nihal <syed.nihal@nokia.com>
  • Loading branch information
wasim-nihal committed May 24, 2024
1 parent 6579344 commit 07a0bd9
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/services/provisioning/alerting/rules_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func (rule *AlertRuleV1) mapToModel(orgID int64) (models.AlertRule, error) {
if alertRule.Condition == "" {
return models.AlertRule{}, fmt.Errorf("rule '%s' failed to parse: no condition set", alertRule.Title)
}
alertRule.Annotations = rule.Annotations.Raw
alertRule.Annotations = rule.Annotations.Value()
alertRule.Labels = rule.Labels.Value()
for _, queryV1 := range rule.Data {
query, err := queryV1.mapToModel()
Expand Down
50 changes: 50 additions & 0 deletions pkg/services/provisioning/alerting/rules_types_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package alerting

import (
"fmt"
"os"
"testing"
"time"

Expand Down Expand Up @@ -199,6 +201,54 @@ func TestRules(t *testing.T) {
require.Len(t, ruleMapped.NotificationSettings, 1)
require.Equal(t, models.NotificationSettings{Receiver: "test-receiver"}, ruleMapped.NotificationSettings[0])
})
t.Run("a rule with annotations should be correctly expanded with respect to env variable provider", func(t *testing.T) {
t.Setenv("ENV_ANNOTATION", "envAnnotationValue")
var annotations values.StringMapValue
// Test Annotations
annotationsMap := `{
"annotationName1": "annotationValue1",
"annotationName2": "${ENV_ANNOTATION}",
"annotationName3": "$__env{ENV_ANNOTATION}",
"annotationName4": "http://localhost:3000/$__env{ENV_ANNOTATION}/foobar",
"annotationName5": "$$__env{ENV_ANNOTATION}",
}`
err := yaml.Unmarshal([]byte(annotationsMap), &annotations)
require.NoError(t, err)
rule := validRuleV1(t)
rule.Annotations = annotations
alertRule, err := rule.mapToModel(1)
require.NoError(t, err)
require.Equal(t, "annotationValue1", alertRule.Annotations["annotationName1"])
require.Equal(t, "envAnnotationValue", alertRule.Annotations["annotationName2"])
require.Equal(t, "envAnnotationValue", alertRule.Annotations["annotationName3"])
require.Equal(t, "http://localhost:3000/envAnnotationValue/foobar", alertRule.Annotations["annotationName4"])
require.Equal(t, "$__env{ENV_ANNOTATION}", alertRule.Annotations["annotationName5"])
})

t.Run("a rule with annotations should be correctly expanded with respect to file provider", func(t *testing.T) {
f, err := os.CreateTemp(os.TempDir(), "file expansion *")
require.NoError(t, err)
file := f.Name()
defer func() {
require.NoError(t, os.Remove(file))
}()

const expected = "foobar"
_, err = f.WriteString(expected)
require.NoError(t, err)
require.NoError(t, f.Close())

var annotations values.StringMapValue
// annotation with file provider
annotationsMap := fmt.Sprintf(`{"annotationName" : "$__file{%s}"}`, file)
err = yaml.Unmarshal([]byte(annotationsMap), &annotations)
require.NoError(t, err)
rule := validRuleV1(t)
rule.Annotations = annotations
alertRule, err := rule.mapToModel(1)
require.NoError(t, err)
require.Equal(t, "foobar", alertRule.Annotations["annotationName"])
})
}

func TestNotificationsSettingsV1MapToModel(t *testing.T) {
Expand Down

0 comments on commit 07a0bd9

Please sign in to comment.