Commit 89557dd6 by John Baublitz Committed by Leonard Gram

Modify Grafana Pagerduty notifier to use Pagerduty API V2

parent da190007
package notifiers package notifiers
import ( import (
"os"
"time"
"strconv" "strconv"
"fmt" "fmt"
...@@ -38,7 +40,7 @@ func init() { ...@@ -38,7 +40,7 @@ func init() {
} }
var ( 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) { func NewPagerdutyNotifier(model *m.AlertNotification) (alerting.Notifier, error) {
...@@ -85,28 +87,39 @@ func (this *PagerdutyNotifier) Notify(evalContext *alerting.EvalContext) error { ...@@ -85,28 +87,39 @@ func (this *PagerdutyNotifier) Notify(evalContext *alerting.EvalContext) error {
this.log.Info("Notifying Pagerduty", "event_type", eventType) 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 := simplejson.New()
bodyJSON.Set("service_key", this.Key) bodyJSON.Set("routing_key", this.Key)
bodyJSON.Set("description", evalContext.Rule.Name+" - "+evalContext.Rule.Message) bodyJSON.Set("event_action", eventType)
bodyJSON.Set("client", "Grafana") bodyJSON.Set("dedup_key", "alertId-"+strconv.FormatInt(evalContext.Rule.Id, 10))
bodyJSON.Set("details", customData) bodyJSON.Set("payload", payloadJSON)
bodyJSON.Set("event_type", eventType)
bodyJSON.Set("incident_key", "alertId-"+strconv.FormatInt(evalContext.Rule.Id, 10))
ruleUrl, err := evalContext.GetRuleUrl() ruleUrl, err := evalContext.GetRuleUrl()
if err != nil { if err != nil {
this.log.Error("Failed get rule link", "error", err) this.log.Error("Failed get rule link", "error", err)
return 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 != "" { if evalContext.ImagePublicUrl != "" {
contexts := make([]interface{}, 1) contexts := make([]interface{}, 1)
imageJSON := simplejson.New() imageJSON := simplejson.New()
imageJSON.Set("type", "image")
imageJSON.Set("src", evalContext.ImagePublicUrl) imageJSON.Set("src", evalContext.ImagePublicUrl)
contexts[0] = imageJSON contexts[0] = imageJSON
bodyJSON.Set("contexts", contexts) bodyJSON.Set("images", contexts)
} }
body, _ := bodyJSON.MarshalJSON() body, _ := bodyJSON.MarshalJSON()
...@@ -115,6 +128,9 @@ func (this *PagerdutyNotifier) Notify(evalContext *alerting.EvalContext) error { ...@@ -115,6 +128,9 @@ func (this *PagerdutyNotifier) Notify(evalContext *alerting.EvalContext) error {
Url: pagerdutyEventApiUrl, Url: pagerdutyEventApiUrl,
Body: string(body), Body: string(body),
HttpMethod: "POST", HttpMethod: "POST",
HttpHeader: map[string]string{
"Content-Type": "application/json",
},
} }
if err := bus.DispatchCtx(evalContext.Ctx, cmd); err != nil { if err := bus.DispatchCtx(evalContext.Ctx, cmd); err != nil {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment