Add discord notifier channel and test (#34150)

* Add discord notifier channel and test

* Correct payload

* remove print statement

* PR feedback and update due to changes in main

* Add discord notifier channel and test

* Correct payload

* remove print statement

* PR feedback and update due to changes in main

* update constructor and tests

* group imports sensibly

* Fix lint

Signed-off-by: Ganesh Vernekar <ganeshvern@gmail.com>

Co-authored-by: Ganesh Vernekar <ganeshvern@gmail.com>
This commit is contained in:
David Parrott 2021-05-19 08:31:55 -07:00 committed by GitHub
parent 2be27c391b
commit b9f4ec2030
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 347 additions and 0 deletions

View File

@ -426,6 +426,8 @@ func (am *Alertmanager) buildReceiverIntegrations(receiver *apimodels.PostableAp
n, err = channels.NewWebHookNotifier(cfg, tmpl)
case "sensugo":
n, err = channels.NewSensuGoNotifier(cfg, tmpl)
case "discord":
n, err = channels.NewDiscordNotifier(cfg, tmpl)
case "alertmanager":
n, err = channels.NewAlertmanagerNotifier(cfg, tmpl)
default:

View File

@ -596,5 +596,29 @@ func GetAvailableNotifiers() []*alerting.NotifierPlugin {
},
},
},
{
Type: "discord",
Name: "Discord",
Heading: "Discord settings",
Description: "Sends notifications to Discord",
Options: []alerting.NotifierOption{
{
Label: "Message Content",
Description: "Mention a group using @ or a user using <@ID> when notifying in a channel",
Element: alerting.ElementTypeInput,
InputType: alerting.InputTypeText,
Placeholder: `{{ template "default.message" . }}`,
PropertyName: "message",
},
{
Label: "Webhook URL",
Element: alerting.ElementTypeInput,
InputType: alerting.InputTypeText,
Placeholder: "Discord webhook URL",
PropertyName: "url",
Required: true,
},
},
},
}
}

View File

@ -0,0 +1,122 @@
package channels
import (
"context"
"encoding/json"
"fmt"
"net/url"
"path"
"strconv"
"strings"
gokit_log "github.com/go-kit/kit/log"
"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/components/simplejson"
"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"
"github.com/grafana/grafana/pkg/setting"
)
type DiscordNotifier struct {
old_notifiers.NotifierBase
log log.Logger
tmpl *template.Template
Content string
WebhookURL string
}
func NewDiscordNotifier(model *NotificationChannelConfig, t *template.Template) (*DiscordNotifier, error) {
if model.Settings == nil {
return nil, alerting.ValidationError{Reason: "No Settings Supplied"}
}
discordURL := model.Settings.Get("url").MustString()
if discordURL == "" {
return nil, alerting.ValidationError{Reason: "Could not find webhook url property in settings"}
}
content := model.Settings.Get("message").MustString(`{{ template "default.message" . }}`)
return &DiscordNotifier{
NotifierBase: old_notifiers.NewNotifierBase(&models.AlertNotification{
Uid: model.UID,
Name: model.Name,
Type: model.Type,
DisableResolveMessage: model.DisableResolveMessage,
Settings: model.Settings,
SecureSettings: model.SecureSettings,
}),
Content: content,
WebhookURL: discordURL,
log: log.New("alerting.notifier.discord"),
tmpl: t,
}, nil
}
func (d DiscordNotifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error) {
data := notify.GetTemplateData(ctx, d.tmpl, as, gokit_log.NewNopLogger())
alerts := types.Alerts(as...)
bodyJSON := simplejson.New()
bodyJSON.Set("username", "Grafana")
var tmplErr error
tmpl := notify.TmplText(d.tmpl, data, &tmplErr)
if d.Content != "" {
bodyJSON.Set("content", tmpl(d.Content))
}
footer := map[string]interface{}{
"text": "Grafana v" + setting.BuildVersion,
"icon_url": "https://grafana.com/assets/img/fav32.png",
}
embed := simplejson.New()
embed.Set("title", tmpl(`{{ template "default.title" . }}`))
embed.Set("footer", footer)
embed.Set("type", "rich")
color, _ := strconv.ParseInt(strings.TrimLeft(getAlertStatusColor(alerts.Status()), "#"), 16, 0)
embed.Set("color", color)
u, err := url.Parse(d.tmpl.ExternalURL.String())
if err != nil {
return false, fmt.Errorf("failed to parse external URL: %w", err)
}
u.Path = path.Join(u.Path, "/alerting/list")
ruleURL := u.String()
embed.Set("url", ruleURL)
bodyJSON.Set("embeds", []interface{}{embed})
if tmplErr != nil {
return false, fmt.Errorf("failed to template discord message: %w", tmplErr)
}
body, err := json.Marshal(bodyJSON)
if err != nil {
return false, err
}
cmd := &models.SendWebhookSync{
Url: d.WebhookURL,
HttpMethod: "POST",
ContentType: "application/json",
Body: string(body),
}
if err := bus.DispatchCtx(ctx, cmd); err != nil {
d.log.Error("Failed to send notification to Discord", "error", err)
return false, err
}
return true, nil
}
func (d DiscordNotifier) SendResolved() bool {
return !d.GetDisableResolveMessage()
}

View File

@ -0,0 +1,158 @@
package channels
import (
"context"
"encoding/json"
"errors"
"net/url"
"testing"
"github.com/prometheus/alertmanager/notify"
"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 TestDiscordNotifier(t *testing.T) {
tmpl := templateForTests(t)
externalURL, err := url.Parse("http://localhost")
require.NoError(t, err)
tmpl.ExternalURL = externalURL
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{}{
"content": "\n**Firing**\nLabels:\n - alertname = alert1\n - lbl1 = val1\nAnnotations:\n - ann1 = annv1\nSource: \n\n\n\n\n",
"embeds": []interface{}{map[string]interface{}{
"color": 1.4037554e+07,
"footer": map[string]interface{}{
"icon_url": "https://grafana.com/assets/img/fav32.png",
"text": "Grafana v",
},
"title": "[FIRING:1] (val1)",
"url": "http://localhost/alerting/list",
"type": "rich",
}},
"username": "Grafana",
},
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"
}`,
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{}{
"content": "2 alerts are firing, 0 are resolved",
"embeds": []interface{}{map[string]interface{}{
"color": 1.4037554e+07,
"footer": map[string]interface{}{
"icon_url": "https://grafana.com/assets/img/fav32.png",
"text": "Grafana v",
},
"title": "[FIRING:2] ",
"url": "http://localhost/alerting/list",
"type": "rich",
}},
"username": "Grafana",
},
expInitError: nil,
expMsgError: nil,
},
{
name: "Error in initialization",
settings: `{}`,
expInitError: alerting.ValidationError{Reason: "Could not find webhook url property in settings"},
},
{
name: "Error in building messsage",
settings: `{
"url": "http://localhost",
"message": "{{ .Status }"
}`,
expMsgError: errors.New("failed to template discord 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 := &NotificationChannelConfig{
Name: "discord_testing",
Type: "discord",
Settings: settingsJson,
}
dn, err := NewDiscordNotifier(m, tmpl)
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 := dn.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)
})
}
}

View File

@ -1223,6 +1223,47 @@ var expAvailableChannelJsonOutput = `
"secure": false
}
]
},
{
"type": "discord",
"name": "Discord",
"heading": "Discord settings",
"description": "Sends notifications to Discord",
"info": "",
"options": [
{
"label": "Message Content",
"description": "Mention a group using @ or a user using <@ID> when notifying in a channel",
"element": "input",
"inputType": "text",
"placeholder": "{{ template \"default.message\" . }}",
"propertyName": "message",
"selectOptions": null,
"showWhen": {
"field": "",
"is": ""
},
"required": false,
"validationRule": "",
"secure": false
},
{
"label": "Webhook URL",
"description": "",
"element": "input",
"inputType": "text",
"placeholder": "Discord webhook URL",
"propertyName": "url",
"selectOptions": null,
"showWhen": {
"field": "",
"is": ""
},
"required": true,
"validationRule": "",
"secure": false
}
]
}
]
`