Alerting: Do not include button in googlechat notification if URL invalid (#47317)

* Alerting: Do not include button in googlechat notification if URL invalid

* Apply suggestions from code review

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

* Alerting: Add test case for invalid external URL in googlechat notifier

Co-authored-by: Alexander Weaver <weaver.alex.d@gmail.com>
This commit is contained in:
Johannes Hertenstein
2022-05-26 18:23:39 +02:00
committed by GitHub
parent bae9a60089
commit 16d738a03a
3 changed files with 163 additions and 38 deletions

View File

@@ -3,6 +3,7 @@ package notifiers
import (
"encoding/json"
"fmt"
"net/url"
"time"
"github.com/grafana/grafana/pkg/infra/log"
@@ -171,21 +172,25 @@ func (gcn *GoogleChatNotifier) Notify(evalContext *alerting.EvalContext) error {
}
}
// add a button widget (link to Grafana)
widgets = append(widgets, buttonWidget{
Buttons: []button{
{
TextButton: textButton{
Text: "OPEN IN GRAFANA",
OnClick: onClick{
OpenLink: openLink{
URL: ruleURL,
if gcn.isUrlAbsolute(ruleURL) {
// add a button widget (link to Grafana)
widgets = append(widgets, buttonWidget{
Buttons: []button{
{
TextButton: textButton{
Text: "OPEN IN GRAFANA",
OnClick: onClick{
OpenLink: openLink{
URL: ruleURL,
},
},
},
},
},
},
})
})
} else {
gcn.log.Warn("Grafana External URL setting is missing or invalid. Skipping 'open in grafana' button to prevent google from displaying empty alerts.", "ruleURL", ruleURL)
}
// add text paragraph widget for the build version and timestamp
widgets = append(widgets, textParagraphWidget{
@@ -227,3 +232,13 @@ func (gcn *GoogleChatNotifier) Notify(evalContext *alerting.EvalContext) error {
return nil
}
func (gcn *GoogleChatNotifier) isUrlAbsolute(urlToCheck string) bool {
parsed, err := url.Parse(urlToCheck)
if err != nil {
gcn.log.Warn("Could not parse URL", "urlToCheck", urlToCheck)
return false
}
return parsed.IsAbs()
}