mirror of
https://github.com/grafana/grafana.git
synced 2024-12-29 10:21:41 -06:00
Legacy Alerting: Replace simplejson with a struct in webhook notification channel (#34952)
* Add type for webhook body * Update pkg/services/alerting/notifiers/webhook.go Co-authored-by: Sofia Papagiannaki <papagian@users.noreply.github.com>
This commit is contained in:
parent
b81d7eb886
commit
0b95fd8ae6
@ -1,8 +1,9 @@
|
||||
package notifiers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"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"
|
||||
@ -86,20 +87,38 @@ type WebhookNotifier struct {
|
||||
log log.Logger
|
||||
}
|
||||
|
||||
// WebhookNotifierBody is the body of webhook
|
||||
// notification channel
|
||||
type WebhookNotifierBody struct {
|
||||
Title string `json:"title"`
|
||||
RuleID int64 `json:"ruleId"`
|
||||
RuleName string `json:"ruleName"`
|
||||
State models.AlertStateType `json:"state"`
|
||||
EvalMatches []*alerting.EvalMatch `json:"evalMatches"`
|
||||
OrgID int64 `json:"orgId"`
|
||||
DashboardID int64 `json:"dashboardId"`
|
||||
PanelID int64 `json:"panelId"`
|
||||
Tags map[string]string `json:"tags"`
|
||||
RuleURL string `json:"ruleUrl,omitempty"`
|
||||
ImageURL string `json:"imageUrl,omitempty"`
|
||||
Message string `json:"message,omitempty"`
|
||||
}
|
||||
|
||||
// Notify send alert notifications as
|
||||
// webhook as http requests.
|
||||
func (wn *WebhookNotifier) Notify(evalContext *alerting.EvalContext) error {
|
||||
wn.log.Info("Sending webhook")
|
||||
|
||||
bodyJSON := simplejson.New()
|
||||
bodyJSON.Set("title", evalContext.GetNotificationTitle())
|
||||
bodyJSON.Set("ruleId", evalContext.Rule.ID)
|
||||
bodyJSON.Set("ruleName", evalContext.Rule.Name)
|
||||
bodyJSON.Set("state", evalContext.Rule.State)
|
||||
bodyJSON.Set("evalMatches", evalContext.EvalMatches)
|
||||
bodyJSON.Set("orgId", evalContext.Rule.OrgID)
|
||||
bodyJSON.Set("dashboardId", evalContext.Rule.DashboardID)
|
||||
bodyJSON.Set("panelId", evalContext.Rule.PanelID)
|
||||
body := WebhookNotifierBody{
|
||||
Title: evalContext.GetNotificationTitle(),
|
||||
RuleID: evalContext.Rule.ID,
|
||||
RuleName: evalContext.Rule.Name,
|
||||
State: evalContext.Rule.State,
|
||||
EvalMatches: evalContext.EvalMatches,
|
||||
OrgID: evalContext.Rule.OrgID,
|
||||
DashboardID: evalContext.Rule.DashboardID,
|
||||
PanelID: evalContext.Rule.PanelID,
|
||||
}
|
||||
|
||||
tags := make(map[string]string)
|
||||
|
||||
@ -107,28 +126,28 @@ func (wn *WebhookNotifier) Notify(evalContext *alerting.EvalContext) error {
|
||||
tags[tag.Key] = tag.Value
|
||||
}
|
||||
|
||||
bodyJSON.Set("tags", tags)
|
||||
body.Tags = tags
|
||||
|
||||
ruleURL, err := evalContext.GetRuleURL()
|
||||
if err == nil {
|
||||
bodyJSON.Set("ruleUrl", ruleURL)
|
||||
body.RuleURL = ruleURL
|
||||
}
|
||||
|
||||
if wn.NeedsImage() && evalContext.ImagePublicURL != "" {
|
||||
bodyJSON.Set("imageUrl", evalContext.ImagePublicURL)
|
||||
body.ImageURL = evalContext.ImagePublicURL
|
||||
}
|
||||
|
||||
if evalContext.Rule.Message != "" {
|
||||
bodyJSON.Set("message", evalContext.Rule.Message)
|
||||
body.Message = evalContext.Rule.Message
|
||||
}
|
||||
|
||||
body, _ := bodyJSON.MarshalJSON()
|
||||
bodyJSON, _ := json.Marshal(body)
|
||||
|
||||
cmd := &models.SendWebhookSync{
|
||||
Url: wn.URL,
|
||||
User: wn.User,
|
||||
Password: wn.Password,
|
||||
Body: string(body),
|
||||
Body: string(bodyJSON),
|
||||
HttpMethod: wn.HTTPMethod,
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user