diff --git a/pkg/services/ngalert/notifier/alertmanager.go b/pkg/services/ngalert/notifier/alertmanager.go index 2577d2c9b2a..55e76356940 100644 --- a/pkg/services/ngalert/notifier/alertmanager.go +++ b/pkg/services/ngalert/notifier/alertmanager.go @@ -370,6 +370,8 @@ func (am *Alertmanager) buildReceiverIntegrations(receiver *apimodels.PostableAp n, err = channels.NewTelegramNotifier(cfg, tmpl, externalURL) case "teams": n, err = channels.NewTeamsNotifier(cfg, tmpl, externalURL) + case "dingding": + n, err = channels.NewDingDingNotifier(cfg, tmpl, externalURL) } if err != nil { return nil, err diff --git a/pkg/services/ngalert/notifier/channels/dingding.go b/pkg/services/ngalert/notifier/channels/dingding.go new file mode 100644 index 00000000000..df60d3c6f80 --- /dev/null +++ b/pkg/services/ngalert/notifier/channels/dingding.go @@ -0,0 +1,125 @@ +package channels + +import ( + "context" + "encoding/json" + "net/url" + + gokit_log "github.com/go-kit/kit/log" + "github.com/pkg/errors" + "github.com/prometheus/alertmanager/notify" + "github.com/prometheus/alertmanager/template" + "github.com/prometheus/alertmanager/types" + + "github.com/grafana/grafana/pkg/bus" + "github.com/grafana/grafana/pkg/infra/log" + "github.com/grafana/grafana/pkg/models" + "github.com/grafana/grafana/pkg/services/alerting" + old_notifiers "github.com/grafana/grafana/pkg/services/alerting/notifiers" +) + +const defaultDingdingMsgType = "link" + +// NewDingDingNotifier is the constructor for the Dingding notifier +func NewDingDingNotifier(model *models.AlertNotification, t *template.Template, externalUrl *url.URL) (*DingDingNotifier, error) { + if model.Settings == nil { + return nil, alerting.ValidationError{Reason: "No Settings Supplied"} + } + + url := model.Settings.Get("url").MustString() + if url == "" { + return nil, alerting.ValidationError{Reason: "Could not find url property in settings"} + } + + msgType := model.Settings.Get("msgType").MustString(defaultDingdingMsgType) + + return &DingDingNotifier{ + NotifierBase: old_notifiers.NewNotifierBase(model), + MsgType: msgType, + URL: url, + Message: model.Settings.Get("message").MustString(`{{ template "default.message" .}}`), + log: log.New("alerting.notifier.dingding"), + tmpl: t, + externalUrl: externalUrl, + }, nil +} + +// DingDingNotifier is responsible for sending alert notifications to ding ding. +type DingDingNotifier struct { + old_notifiers.NotifierBase + MsgType string + URL string + Message string + tmpl *template.Template + log log.Logger + externalUrl *url.URL +} + +// Notify sends the alert notification to dingding. +func (dd *DingDingNotifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error) { + dd.log.Info("Sending dingding") + + q := url.Values{ + "pc_slide": {"false"}, + "url": {dd.externalUrl.String()}, // TODO: should this be rule URL according to original? + } + + // Use special link to auto open the message url outside of Dingding + // Refer: https://open-doc.dingtalk.com/docs/doc.htm?treeId=385&articleId=104972&docType=1#s9 + messageURL := "dingtalk://dingtalkclient/page/link?" + q.Encode() + + data := notify.GetTemplateData(ctx, &template.Template{ExternalURL: dd.externalUrl}, as, gokit_log.NewNopLogger()) + var tmplErr error + tmpl := notify.TmplText(dd.tmpl, data, &tmplErr) + + message := tmpl(dd.Message) + title := getTitleFromTemplateData(data) + + var bodyMsg map[string]interface{} + if dd.MsgType == "actionCard" { + bodyMsg = map[string]interface{}{ + "msgtype": "actionCard", + "actionCard": map[string]string{ + "text": message, + "title": title, + "singleTitle": "More", + "singleURL": messageURL, + }, + } + } else { + link := map[string]string{ + "text": message, + "title": title, + "messageUrl": messageURL, + } + + bodyMsg = map[string]interface{}{ + "msgtype": "link", + "link": link, + } + } + + if tmplErr != nil { + return false, errors.Wrap(tmplErr, "failed to template dingding message") + } + + body, err := json.Marshal(bodyMsg) + if err != nil { + return false, err + } + + cmd := &models.SendWebhookSync{ + Url: dd.URL, + Body: string(body), + } + + if err := bus.DispatchCtx(ctx, cmd); err != nil { + return false, errors.Wrap(err, "send notification to dingding") + } + + return true, nil +} + +func (dd *DingDingNotifier) SendResolved() bool { + return !dd.GetDisableResolveMessage() +} diff --git a/pkg/services/ngalert/notifier/channels/dingding_test.go b/pkg/services/ngalert/notifier/channels/dingding_test.go new file mode 100644 index 00000000000..b9ad065e537 --- /dev/null +++ b/pkg/services/ngalert/notifier/channels/dingding_test.go @@ -0,0 +1,145 @@ +package channels + +import ( + "context" + "encoding/json" + "errors" + "net/url" + "testing" + + "github.com/prometheus/alertmanager/notify" + "github.com/prometheus/alertmanager/template" + "github.com/prometheus/alertmanager/types" + "github.com/prometheus/common/model" + "github.com/stretchr/testify/require" + + "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" +) + +func TestDingdingNotifier(t *testing.T) { + tmpl, err := template.FromGlobs("templates/default.tmpl") + require.NoError(t, err) + + cases := []struct { + name string + settings string + alerts []*types.Alert + expMsg map[string]interface{} + expInitError error + expMsgError error + }{ + { + name: "Default config with one alert", + settings: `{"url": "http://localhost"}`, + alerts: []*types.Alert{ + { + Alert: model.Alert{ + Labels: model.LabelSet{"alertname": "alert1", "lbl1": "val1"}, + Annotations: model.LabelSet{"ann1": "annv1"}, + }, + }, + }, + expMsg: map[string]interface{}{ + "msgtype": "link", + "link": map[string]interface{}{ + "messageUrl": "dingtalk://dingtalkclient/page/link?pc_slide=false&url=http%3A%2F%2Flocalhost", + "text": "\n**Firing**\nLabels:\n - alertname = alert1\n - lbl1 = val1\nAnnotations:\n - ann1 = annv1\nSource: \n\n\n\n\n", + "title": "[firing:1] (val1)", + }, + }, + expInitError: nil, + expMsgError: nil, + }, { + name: "Custom config with multiple alerts", + settings: `{ + "url": "http://localhost", + "message": "{{ len .Alerts.Firing }} alerts are firing, {{ len .Alerts.Resolved }} are resolved", + "msgType": "actionCard" + }`, + alerts: []*types.Alert{ + { + Alert: model.Alert{ + Labels: model.LabelSet{"alertname": "alert1", "lbl1": "val1"}, + Annotations: model.LabelSet{"ann1": "annv1"}, + }, + }, { + Alert: model.Alert{ + Labels: model.LabelSet{"alertname": "alert1", "lbl1": "val2"}, + Annotations: model.LabelSet{"ann1": "annv2"}, + }, + }, + }, + expMsg: map[string]interface{}{ + "actionCard": map[string]interface{}{ + "singleTitle": "More", + "singleURL": "dingtalk://dingtalkclient/page/link?pc_slide=false&url=http%3A%2F%2Flocalhost", + "text": "2 alerts are firing, 0 are resolved", + "title": "[firing:2] ", + }, + "msgtype": "actionCard", + }, + expInitError: nil, + expMsgError: nil, + }, { + name: "Error in initing", + settings: `{}`, + expInitError: alerting.ValidationError{Reason: "Could not find url property in settings"}, + }, { + name: "Error in building message", + settings: `{ + "url": "http://localhost", + "message": "{{ .Status }" + }`, + expMsgError: errors.New("failed to template dingding message: template: :1: unexpected \"}\" in operand"), + }, + } + + for _, c := range cases { + t.Run(c.name, func(t *testing.T) { + settingsJSON, err := simplejson.NewJson([]byte(c.settings)) + require.NoError(t, err) + + m := &models.AlertNotification{ + Name: "dingding_testing", + Type: "dingding", + Settings: settingsJSON, + } + + externalURL, err := url.Parse("http://localhost") + require.NoError(t, err) + pn, err := NewDingDingNotifier(m, tmpl, externalURL) + if c.expInitError != nil { + require.Error(t, err) + require.Equal(t, c.expInitError.Error(), err.Error()) + return + } + require.NoError(t, err) + + body := "" + bus.AddHandlerCtx("test", func(ctx context.Context, webhook *models.SendWebhookSync) error { + body = webhook.Body + return nil + }) + + ctx := notify.WithGroupKey(context.Background(), "alertname") + ctx = notify.WithGroupLabels(ctx, model.LabelSet{"alertname": ""}) + ok, err := pn.Notify(ctx, c.alerts...) + if c.expMsgError != nil { + require.False(t, ok) + require.Error(t, err) + require.Equal(t, c.expMsgError.Error(), err.Error()) + return + } + require.NoError(t, err) + require.True(t, ok) + + expBody, err := json.Marshal(c.expMsg) + require.NoError(t, err) + + require.JSONEq(t, string(expBody), body) + }) + } +}