Alerting: Add custom templated title to Wecom notifier (#51529)

* add custom title in wecom channel

* add wecom test case and setting config in ui

* Update pkg/services/ngalert/notifier/channels/wecom_test.go

Co-authored-by: Matthew Jacobson <JacobsonMT@gmail.com>

* change version in comment

* Update pkg/services/ngalert/notifier/available_channels.go

Co-authored-by: Matthew Jacobson <JacobsonMT@gmail.com>

* format

Co-authored-by: Matthew Jacobson <JacobsonMT@gmail.com>
This commit is contained in:
dingweiqings 2022-07-06 23:54:46 +08:00 committed by GitHub
parent 396278a6e5
commit 119c13e666
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 1 deletions

View File

@ -696,6 +696,14 @@ func GetAvailableNotifiers() []*alerting.NotifierPlugin {
Placeholder: `{{ template "default.message" . }}`, Placeholder: `{{ template "default.message" . }}`,
PropertyName: "message", PropertyName: "message",
}, },
{ // New in 9.1.
Label: "Title",
Element: alerting.ElementTypeInput,
InputType: alerting.InputTypeText,
Description: "Templated title of the message",
PropertyName: "title",
Placeholder: `{{ template "default.title" . }}`,
},
}, },
}, },
{ {

View File

@ -18,6 +18,7 @@ type WeComConfig struct {
*NotificationChannelConfig *NotificationChannelConfig
URL string URL string
Message string Message string
Title string
} }
func WeComFactory(fc FactoryConfig) (NotificationChannel, error) { func WeComFactory(fc FactoryConfig) (NotificationChannel, error) {
@ -40,6 +41,7 @@ func NewWeComConfig(config *NotificationChannelConfig, decryptFunc GetDecryptedV
NotificationChannelConfig: config, NotificationChannelConfig: config,
URL: url, URL: url,
Message: config.Settings.Get("message").MustString(`{{ template "default.message" .}}`), Message: config.Settings.Get("message").MustString(`{{ template "default.message" .}}`),
Title: config.Settings.Get("title").MustString(DefaultMessageTitleEmbed),
}, nil }, nil
} }
@ -55,6 +57,7 @@ func NewWeComNotifier(config *WeComConfig, ns notifications.WebhookSender, t *te
}), }),
URL: config.URL, URL: config.URL,
Message: config.Message, Message: config.Message,
Title: config.Title,
log: log.New("alerting.notifier.wecom"), log: log.New("alerting.notifier.wecom"),
ns: ns, ns: ns,
tmpl: t, tmpl: t,
@ -66,6 +69,7 @@ type WeComNotifier struct {
*Base *Base
URL string URL string
Message string Message string
Title string
tmpl *template.Template tmpl *template.Template
log log.Logger log log.Logger
ns notifications.WebhookSender ns notifications.WebhookSender
@ -82,7 +86,7 @@ func (w *WeComNotifier) Notify(ctx context.Context, as ...*types.Alert) (bool, e
"msgtype": "markdown", "msgtype": "markdown",
} }
content := fmt.Sprintf("# %s\n%s\n", content := fmt.Sprintf("# %s\n%s\n",
tmpl(DefaultMessageTitleEmbed), tmpl(w.Title),
tmpl(w.Message), tmpl(w.Message),
) )

View File

@ -76,6 +76,33 @@ func TestWeComNotifier(t *testing.T) {
"msgtype": "markdown", "msgtype": "markdown",
}, },
expMsgError: nil, expMsgError: nil,
}, {
name: "Custom title and message with multiple alerts",
settings: `{
"url": "http://localhost",
"message": "{{ len .Alerts.Firing }} alerts are firing, {{ len .Alerts.Resolved }} are resolved",
"title": "This notification is {{ .Status }}!"
}`,
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{}{
"markdown": map[string]interface{}{
"content": "# This notification is firing!\n2 alerts are firing, 0 are resolved\n",
},
"msgtype": "markdown",
},
expMsgError: nil,
}, { }, {
name: "Error in initing", name: "Error in initing",
settings: `{}`, settings: `{}`,