From 2027e1aaee53fb867945c6eba856fdc873c03199 Mon Sep 17 00:00:00 2001 From: Dima Ryskin <8168970+aSapien@users.noreply.github.com> Date: Tue, 3 Dec 2019 12:54:37 +0200 Subject: [PATCH] AlertNotifier: Support alert tags in OpsGenie notifier (#20810) * support alert tags in OpsGenie notifier * update readme: OpsGenie alert tags support * lintfix: remove redundant string formatting --- docs/sources/alerting/notifications.md | 2 +- pkg/services/alerting/notifiers/opsgenie.go | 12 +++++ .../alerting/notifiers/opsgenie_test.go | 53 ++++++++++++++++++- 3 files changed, 64 insertions(+), 3 deletions(-) diff --git a/docs/sources/alerting/notifications.md b/docs/sources/alerting/notifications.md index e2231986eec..4118b197ec4 100644 --- a/docs/sources/alerting/notifications.md +++ b/docs/sources/alerting/notifications.md @@ -177,7 +177,7 @@ Hipchat | `hipchat` | yes, external only | no Kafka | `kafka` | yes, external only | no Line | `line` | yes, external only | no Microsoft Teams | `teams` | yes, external only | no -OpsGenie | `opsgenie` | yes, external only | no +OpsGenie | `opsgenie` | yes, external only | yes Pagerduty | `pagerduty` | yes, external only | no Prometheus Alertmanager | `prometheus-alertmanager` | yes, external only | yes Pushover | `pushover` | yes | no diff --git a/pkg/services/alerting/notifiers/opsgenie.go b/pkg/services/alerting/notifiers/opsgenie.go index 833927dee9f..6e6bc795205 100644 --- a/pkg/services/alerting/notifiers/opsgenie.go +++ b/pkg/services/alerting/notifiers/opsgenie.go @@ -116,6 +116,18 @@ func (on *OpsGenieNotifier) createAlert(evalContext *alerting.EvalContext) error } bodyJSON.Set("details", details) + + tags := make([]string, 0) + for _, tag := range evalContext.Rule.AlertRuleTags { + if len(tag.Value) > 0 { + tags = append(tags, fmt.Sprintf("%s:%s", tag.Key, tag.Value)) + } else { + tags = append(tags, tag.Key) + } + + } + bodyJSON.Set("tags", tags) + body, _ := bodyJSON.MarshalJSON() cmd := &models.SendWebhookSync{ diff --git a/pkg/services/alerting/notifiers/opsgenie_test.go b/pkg/services/alerting/notifiers/opsgenie_test.go index 1df57676b38..48619376541 100644 --- a/pkg/services/alerting/notifiers/opsgenie_test.go +++ b/pkg/services/alerting/notifiers/opsgenie_test.go @@ -1,11 +1,13 @@ package notifiers import ( - "testing" - + "context" + "github.com/grafana/grafana/pkg/bus" "github.com/grafana/grafana/pkg/components/simplejson" "github.com/grafana/grafana/pkg/models" + "github.com/grafana/grafana/pkg/services/alerting" . "github.com/smartystreets/goconvey/convey" + "testing" ) func TestOpsGenieNotifier(t *testing.T) { @@ -47,6 +49,53 @@ func TestOpsGenieNotifier(t *testing.T) { So(opsgenieNotifier.Type, ShouldEqual, "opsgenie") So(opsgenieNotifier.APIKey, ShouldEqual, "abcdefgh0123456789") }) + + Convey("alert payload should include tag pairs in a ['key1:value1'] format when a value exists and in ['key2'] format when a value is absent", func() { + json := ` + { + "apiKey": "abcdefgh0123456789" + }` + + tagPairs := []*models.Tag{ + {Key: "keyOnly"}, + {Key: "aKey", Value: "aValue"}, + } + + settingsJSON, _ := simplejson.NewJson([]byte(json)) + model := &models.AlertNotification{ + Name: "opsgenie_testing", + Type: "opsgenie", + Settings: settingsJSON, + } + + notifier, notifierErr := NewOpsGenieNotifier(model) //unhandled error + + opsgenieNotifier := notifier.(*OpsGenieNotifier) + + evalContext := alerting.NewEvalContext(context.Background(), &alerting.Rule{ + ID: 0, + Name: "someRule", + Message: "someMessage", + State: models.AlertStateAlerting, + AlertRuleTags: tagPairs, + }) + evalContext.IsTestRun = true + + receivedTags := make([]string, 0) + bus.AddHandlerCtx("alerting", func(ctx context.Context, cmd *models.SendWebhookSync) error { + bodyJson, err := simplejson.NewJson([]byte(cmd.Body)) + if err == nil { + receivedTags = bodyJson.Get("tags").MustStringArray([]string{}) + } + return err + }) + + alertErr := opsgenieNotifier.createAlert(evalContext) + + So(notifierErr, ShouldBeNil) + So(alertErr, ShouldBeNil) + So(receivedTags, ShouldResemble, []string{"keyOnly", "aKey:aValue"}) + }) }) }) }