Alerting: Add title and description to Webhook contact point (#58058)

* Add title and description to Webhook contact point

* Remove deprecation message
This commit is contained in:
Alex Moreno 2022-11-03 10:52:07 +01:00 committed by GitHub
parent 76532c1f27
commit 3558cadb7e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 13 deletions

View File

@ -43,6 +43,9 @@ type webhookSettings struct {
// HTTP Basic Authentication.
User string `json:"username,omitempty" yaml:"username,omitempty"`
Password string `json:"password,omitempty" yaml:"password,omitempty"`
Title string `json:"title,omitempty" yaml:"title,omitempty"`
Message string `json:"message,omitempty" yaml:"message,omitempty"`
}
func buildWebhookSettings(factoryConfig FactoryConfig) (webhookSettings, error) {
@ -66,6 +69,12 @@ func buildWebhookSettings(factoryConfig FactoryConfig) (webhookSettings, error)
if settings.User != "" && settings.Password != "" && settings.AuthorizationScheme != "" && settings.AuthorizationCredentials != "" {
return settings, errors.New("both HTTP Basic Authentication and Authorization Header are set, only 1 is permitted")
}
if settings.Title == "" {
settings.Title = DefaultMessageTitleEmbed
}
if settings.Message == "" {
settings.Message = DefaultMessageEmbed
}
return settings, err
}
@ -132,12 +141,9 @@ type WebhookMessage struct {
GroupKey string `json:"groupKey"`
TruncatedAlerts int `json:"truncatedAlerts"`
OrgID int64 `json:"orgId"`
// Deprecated, to be removed in Grafana 10
// These are present to make migration a little less disruptive.
Title string `json:"title"`
State string `json:"state"`
Message string `json:"message"`
Title string `json:"title"`
State string `json:"state"`
Message string `json:"message"`
}
// Notify implements the Notifier interface.
@ -167,8 +173,8 @@ func (wn *WebhookNotifier) Notify(ctx context.Context, as ...*types.Alert) (bool
GroupKey: groupKey.String(),
TruncatedAlerts: numTruncated,
OrgID: wn.orgID,
Title: tmpl(DefaultMessageTitleEmbed),
Message: tmpl(DefaultMessageEmbed),
Title: tmpl(wn.settings.Title),
Message: tmpl(wn.settings.Message),
}
if types.Alerts(as...).Status() == model.AlertFiring {
msg.State = string(models.AlertStateAlerting)

View File

@ -42,8 +42,8 @@ func TestWebhookNotifier(t *testing.T) {
expMsgError error
}{
{
name: "Default config with one alert",
settings: `{"url": "http://localhost/test"}`,
name: "Default config with one alert with custom message",
settings: `{"url": "http://localhost/test", "message": "Custom message"}`,
alerts: []*types.Alert{
{
Alert: model.Alert{
@ -90,16 +90,17 @@ func TestWebhookNotifier(t *testing.T) {
GroupKey: "alertname",
Title: "[FIRING:1] (val1)",
State: "alerting",
Message: "**Firing**\n\nValue: [no value]\nLabels:\n - alertname = alert1\n - lbl1 = val1\nAnnotations:\n - ann1 = annv1\nSilence: http://localhost/alerting/silence/new?alertmanager=grafana&matcher=alertname%3Dalert1&matcher=lbl1%3Dval1\nDashboard: http://localhost/d/abcd\nPanel: http://localhost/d/abcd?viewPanel=efgh\n",
Message: "Custom message",
OrgID: orgID,
},
expMsgError: nil,
expHeaders: map[string]string{},
},
{
name: "Custom config with multiple alerts",
name: "Custom config with multiple alerts with custom title",
settings: `{
"url": "http://localhost/test1",
"title": "Alerts firing: {{ len .Alerts.Firing }}",
"username": "user1",
"password": "mysecret",
"httpMethod": "PUT",
@ -168,7 +169,7 @@ func TestWebhookNotifier(t *testing.T) {
Version: "1",
GroupKey: "alertname",
TruncatedAlerts: 1,
Title: "[FIRING:2] ",
Title: "Alerts firing: 2",
State: "alerting",
Message: "**Firing**\n\nValue: [no value]\nLabels:\n - alertname = alert1\n - lbl1 = val1\nAnnotations:\n - ann1 = annv1\nSilence: http://localhost/alerting/silence/new?alertmanager=grafana&matcher=alertname%3Dalert1&matcher=lbl1%3Dval1\n\nValue: [no value]\nLabels:\n - alertname = alert1\n - lbl1 = val2\nAnnotations:\n - ann1 = annv2\nSilence: http://localhost/alerting/silence/new?alertmanager=grafana&matcher=alertname%3Dalert1&matcher=lbl1%3Dval2\n",
OrgID: orgID,

View File

@ -735,6 +735,21 @@ func GetAvailableNotifiers() []*NotifierPlugin {
InputType: InputTypeText,
PropertyName: "maxAlerts",
},
{ // New in 9.3.
Label: "Title",
Description: "Templated title of the message.",
Element: ElementTypeInput,
InputType: InputTypeText,
PropertyName: "title",
Placeholder: channels.DefaultMessageTitleEmbed,
},
{ // New in 9.3.
Label: "Message",
Description: "Custom message. You can use template variables.",
Element: ElementTypeTextArea,
PropertyName: "message",
Placeholder: channels.DefaultMessageEmbed,
},
},
},
{