Modify Grafana Pagerduty notifier to use Pagerduty API V2

This commit is contained in:
John Baublitz 2018-01-16 12:12:42 -05:00 committed by Leonard Gram
parent da19000733
commit 89557dd67b

View File

@ -1,6 +1,8 @@
package notifiers
import (
"os"
"time"
"strconv"
"fmt"
@ -38,7 +40,7 @@ func init() {
}
var (
pagerdutyEventApiUrl string = "https://events.pagerduty.com/generic/2010-04-15/create_event.json"
pagerdutyEventApiUrl string = "https://events.pagerduty.com/v2/enqueue"
)
func NewPagerdutyNotifier(model *m.AlertNotification) (alerting.Notifier, error) {
@ -85,28 +87,39 @@ func (this *PagerdutyNotifier) Notify(evalContext *alerting.EvalContext) error {
this.log.Info("Notifying Pagerduty", "event_type", eventType)
payloadJSON := simplejson.New()
payloadJSON.Set("summary", evalContext.Rule.Name+" - "+evalContext.Rule.Message)
if hostname, err := os.Hostname(); err == nil {
payloadJSON.Set("source", hostname)
}
payloadJSON.Set("severity", "critical")
payloadJSON.Set("timestamp", time.Now())
payloadJSON.Set("component", "Grafana")
payloadJSON.Set("custom_details", customData)
bodyJSON := simplejson.New()
bodyJSON.Set("service_key", this.Key)
bodyJSON.Set("description", evalContext.Rule.Name+" - "+evalContext.Rule.Message)
bodyJSON.Set("client", "Grafana")
bodyJSON.Set("details", customData)
bodyJSON.Set("event_type", eventType)
bodyJSON.Set("incident_key", "alertId-"+strconv.FormatInt(evalContext.Rule.Id, 10))
bodyJSON.Set("routing_key", this.Key)
bodyJSON.Set("event_action", eventType)
bodyJSON.Set("dedup_key", "alertId-"+strconv.FormatInt(evalContext.Rule.Id, 10))
bodyJSON.Set("payload", payloadJSON)
ruleUrl, err := evalContext.GetRuleUrl()
if err != nil {
this.log.Error("Failed get rule link", "error", err)
return err
}
bodyJSON.Set("client_url", ruleUrl)
links := make([]interface{}, 1)
linkJSON := simplejson.New()
linkJSON.Set("href", ruleUrl)
links[0] = linkJSON
bodyJSON.Set("links", links)
if evalContext.ImagePublicUrl != "" {
contexts := make([]interface{}, 1)
imageJSON := simplejson.New()
imageJSON.Set("type", "image")
imageJSON.Set("src", evalContext.ImagePublicUrl)
contexts[0] = imageJSON
bodyJSON.Set("contexts", contexts)
bodyJSON.Set("images", contexts)
}
body, _ := bodyJSON.MarshalJSON()
@ -115,6 +128,9 @@ func (this *PagerdutyNotifier) Notify(evalContext *alerting.EvalContext) error {
Url: pagerdutyEventApiUrl,
Body: string(body),
HttpMethod: "POST",
HttpHeader: map[string]string{
"Content-Type": "application/json",
},
}
if err := bus.DispatchCtx(evalContext.Ctx, cmd); err != nil {