1
1
package notifiers
2
2
3
3
import (
4
+ "strconv"
5
+
4
6
"github.com/grafana/grafana/pkg/bus"
5
7
"github.com/grafana/grafana/pkg/components/simplejson"
6
8
"github.com/grafana/grafana/pkg/log"
18
20
)
19
21
20
22
func NewPagerdutyNotifier (model * m.AlertNotification ) (alerting.Notifier , error ) {
23
+ autoResolve := model .Settings .Get ("autoResolve" ).MustBool (true )
21
24
key := model .Settings .Get ("integrationKey" ).MustString ()
22
25
if key == "" {
23
26
return nil , alerting.ValidationError {Reason : "Could not find integration key property in settings" }
@@ -26,57 +29,66 @@ func NewPagerdutyNotifier(model *m.AlertNotification) (alerting.Notifier, error)
26
29
return & PagerdutyNotifier {
27
30
NotifierBase : NewNotifierBase (model .Id , model .IsDefault , model .Name , model .Type , model .Settings ),
28
31
Key : key ,
32
+ AutoResolve : autoResolve ,
29
33
log : log .New ("alerting.notifier.pagerduty" ),
30
34
}, nil
31
35
}
32
36
33
37
type PagerdutyNotifier struct {
34
38
NotifierBase
35
- Key string
36
- log log.Logger
39
+ Key string
40
+ AutoResolve bool
41
+ log log.Logger
37
42
}
38
43
39
44
func (this * PagerdutyNotifier ) Notify (evalContext * alerting.EvalContext ) error {
40
- this .log .Info ("Notifying Pagerduty" )
41
45
metrics .M_Alerting_Notification_Sent_PagerDuty .Inc (1 )
42
46
43
- if evalContext .Rule .State == m .AlertStateAlerting {
44
- bodyJSON := simplejson .New ()
45
- bodyJSON .Set ("service_key" , this .Key )
46
- bodyJSON .Set ("description" , evalContext .Rule .Name + " - " + evalContext .Rule .Message )
47
- bodyJSON .Set ("client" , "Grafana" )
48
- bodyJSON .Set ("event_type" , "trigger" )
49
-
50
- ruleUrl , err := evalContext .GetRuleUrl ()
51
- if err != nil {
52
- this .log .Error ("Failed get rule link" , "error" , err )
53
- return err
54
- }
55
- bodyJSON .Set ("client_url" , ruleUrl )
56
-
57
- if evalContext .ImagePublicUrl != "" {
58
- contexts := make ([]interface {}, 1 )
59
- imageJSON := simplejson .New ()
60
- imageJSON .Set ("type" , "image" )
61
- imageJSON .Set ("src" , evalContext .ImagePublicUrl )
62
- contexts [0 ] = imageJSON
63
- bodyJSON .Set ("contexts" , contexts )
64
- }
65
-
66
- body , _ := bodyJSON .MarshalJSON ()
67
-
68
- cmd := & m.SendWebhookSync {
69
- Url : pagerdutyEventApiUrl ,
70
- Body : string (body ),
71
- HttpMethod : "POST" ,
72
- }
73
-
74
- if err := bus .DispatchCtx (evalContext .Ctx , cmd ); err != nil {
75
- this .log .Error ("Failed to send notification to Pagerduty" , "error" , err , "body" , string (body ))
76
- }
77
-
78
- } else {
79
- this .log .Info ("Not sending a trigger to Pagerduty" , "state" , evalContext .Rule .State )
47
+ if evalContext .Rule .State == m .AlertStateOK && ! this .AutoResolve {
48
+ this .log .Info ("Not sending a trigger to Pagerduty" , "state" , evalContext .Rule .State , "auto resolve" , this .AutoResolve )
49
+ return nil
50
+ }
51
+
52
+ eventType := "trigger"
53
+ if evalContext .Rule .State == m .AlertStateOK {
54
+ eventType = "resolve"
55
+ }
56
+
57
+ this .log .Info ("Notifying Pagerduty" , "event_type" , eventType )
58
+
59
+ bodyJSON := simplejson .New ()
60
+ bodyJSON .Set ("service_key" , this .Key )
61
+ bodyJSON .Set ("description" , evalContext .Rule .Name + " - " + evalContext .Rule .Message )
62
+ bodyJSON .Set ("client" , "Grafana" )
63
+ bodyJSON .Set ("event_type" , eventType )
64
+ bodyJSON .Set ("incident_key" , "alertId-" + strconv .FormatInt (evalContext .Rule .Id , 10 ))
65
+
66
+ ruleUrl , err := evalContext .GetRuleUrl ()
67
+ if err != nil {
68
+ this .log .Error ("Failed get rule link" , "error" , err )
69
+ return err
70
+ }
71
+ bodyJSON .Set ("client_url" , ruleUrl )
72
+
73
+ if evalContext .ImagePublicUrl != "" {
74
+ contexts := make ([]interface {}, 1 )
75
+ imageJSON := simplejson .New ()
76
+ imageJSON .Set ("type" , "image" )
77
+ imageJSON .Set ("src" , evalContext .ImagePublicUrl )
78
+ contexts [0 ] = imageJSON
79
+ bodyJSON .Set ("contexts" , contexts )
80
+ }
81
+
82
+ body , _ := bodyJSON .MarshalJSON ()
83
+
84
+ cmd := & m.SendWebhookSync {
85
+ Url : pagerdutyEventApiUrl ,
86
+ Body : string (body ),
87
+ HttpMethod : "POST" ,
88
+ }
89
+
90
+ if err := bus .DispatchCtx (evalContext .Ctx , cmd ); err != nil {
91
+ this .log .Error ("Failed to send notification to Pagerduty" , "error" , err , "body" , string (body ))
80
92
}
81
93
82
94
return nil
0 commit comments