Alerting: add field for custom slack endpoint (#45751)

* add field for custom slack endpoint

* add test for using custom endpoint

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

Co-authored-by: Alexander Weaver <weaver.alex.d@gmail.com>

* specify description for endpoint

* remove brittle string constants

Co-authored-by: Alexander Weaver <weaver.alex.d@gmail.com>
This commit is contained in:
Nathan Rodman
2022-02-23 14:33:23 -08:00
committed by GitHub
parent cbf96e6a8b
commit f9701d78b1
4 changed files with 61 additions and 1652 deletions

View File

@@ -459,6 +459,14 @@ func GetAvailableNotifiers() []*alerting.NotifierPlugin {
PropertyName: "url",
Secure: true,
},
{ // New in 8.4.
Label: "Endpoint URL",
Element: alerting.ElementTypeInput,
InputType: alerting.InputTypeText,
Description: "Optionally provide a custom Slack message API endpoint for non-webhook requests, default is https://slack.com/api/chat.postMessage",
Placeholder: "Slack endpoint url",
PropertyName: "endpointUrl",
},
{ // New in 8.0.
Label: "Title",
Element: alerting.ElementTypeInput,

View File

@@ -52,9 +52,11 @@ func NewSlackNotifier(model *NotificationChannelConfig, t *template.Template, fn
return nil, receiverInitError{Cfg: *model, Reason: "no secure settings supplied"}
}
endpointURL := model.Settings.Get("endpointUrl").MustString(SlackAPIEndpoint)
slackURL := fn(context.Background(), model.SecureSettings, "url", model.Settings.Get("url").MustString())
if slackURL == "" {
slackURL = SlackAPIEndpoint
slackURL = endpointURL
}
apiURL, err := url.Parse(slackURL)
if err != nil {
@@ -62,7 +64,7 @@ func NewSlackNotifier(model *NotificationChannelConfig, t *template.Template, fn
}
recipient := strings.TrimSpace(model.Settings.Get("recipient").MustString())
if recipient == "" && apiURL.String() == SlackAPIEndpoint {
if recipient == "" && apiURL.String() == endpointURL {
return nil, receiverInitError{Cfg: *model,
Reason: "recipient must be specified when using the Slack chat API",
}

View File

@@ -160,6 +160,42 @@ func TestSlackNotifier(t *testing.T) {
}`,
expInitError: `failed to validate receiver "slack_testing" of type "slack": recipient must be specified when using the Slack chat API`,
},
{
name: "Custom endpoint url",
settings: `{
"token": "1234",
"recipient": "#testchannel",
"endpointUrl": "https://slack-custom.com/api/",
"icon_emoji": ":emoji:"
}`,
alerts: []*types.Alert{
{
Alert: model.Alert{
Labels: model.LabelSet{"alertname": "alert1", "lbl1": "val1"},
Annotations: model.LabelSet{"ann1": "annv1"},
},
},
},
expMsg: &slackMessage{
Channel: "#testchannel",
Username: "Grafana",
IconEmoji: ":emoji:",
Attachments: []attachment{
{
Title: "[FIRING:1] (val1)",
TitleLink: "http://localhost/alerting/list",
Text: "**Firing**\n\nValue: [no value]\nLabels:\n - alertname = alert1\n - lbl1 = val1\nAnnotations:\n - ann1 = annv1\nSilence: http://localhost/alerting/silence/new?alertmanager=grafana&matchers=alertname%3Dalert1%2Clbl1%3Dval1\n",
Fallback: "[FIRING:1] (val1)",
Fields: nil,
Footer: "Grafana v" + setting.BuildVersion,
FooterIcon: "https://grafana.com/assets/img/fav32.png",
Color: "#D63232",
Ts: 0,
},
},
},
expMsgError: nil,
},
}
for _, c := range cases {
@@ -196,6 +232,12 @@ func TestSlackNotifier(t *testing.T) {
_ = request.Body.Close()
}()
url := settingsJSON.Get("url").MustString()
if len(url) == 0 {
endpointUrl := settingsJSON.Get("endpointUrl").MustString(SlackAPIEndpoint)
require.Equal(t, endpointUrl, request.URL.String())
}
b, err := io.ReadAll(request.Body)
require.NoError(t, err)
body = string(b)

File diff suppressed because it is too large Load Diff