-
Notifications
You must be signed in to change notification settings - Fork 191
/
gopherduty.go
87 lines (75 loc) · 2.67 KB
/
gopherduty.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// A simple Go client for PagerDuty's API. This includes the trigger, acknowledge, and
// resolve event types. This also includes a retry feature when sending to PagerDuty
// fails.
package gopherduty
import (
"log"
"math"
"time"
)
const (
eventTrigger = "trigger"
eventAcknowledge = "acknowledge"
eventResolve = "resolve"
)
func init() {
log.SetPrefix("[ PagerDuty Client ] ")
}
// PagerDuty requires a Service Key to work. API call can be retried if MaxRetry is set to > 1. This retries the
// request with an exponential delay for each retry.
type PagerDuty struct {
ServiceKey string // The Service key needed to access PagerDuty.
MaxRetry int // Maximum API call retries. Defaults to 0.
RetryBaseInterval int // Starting delay for a retry in seconds. Defaults to 10.
retries int
}
// Convenience method to create a new PagerDuty struct.
func NewClient(serviceKey string) *PagerDuty {
return &PagerDuty{
ServiceKey: serviceKey,
}
}
// Send a TRIGGER event. The incidentKey may be left empty and PagerDuty will generate one.
func (p *PagerDuty) Trigger(incidentKey, description, client, clientUrl string, details interface{}) *PagerDutyResponse {
log.Println("Sending TRIGGER event")
return p.doRequest(eventTrigger, incidentKey, description, client, clientUrl, details)
}
// Send an ACKNOWLEDGE event.
func (p *PagerDuty) Acknowledge(incidentKey, description string, details interface{}) *PagerDutyResponse {
log.Println("Sending ACKENOWLEDGE event")
return p.doRequest(eventAcknowledge, incidentKey, description, "", "", details)
}
// Send a RESOLVE event.
func (p *PagerDuty) Resolve(incidentKey, description string, details interface{}) *PagerDutyResponse {
log.Println("Sending RESOLVE event")
return p.doRequest(eventResolve, incidentKey, description, "", "", details)
}
func (p *PagerDuty) doRequest(eventType, incidentKey, description, client, clientUrl string, details interface{}) *PagerDutyResponse {
request := &pagerDutyRequest{
ServiceKey: p.ServiceKey,
EventType: eventType,
IncidentKey: incidentKey,
Description: description,
Client: client,
ClientUrl: clientUrl,
Details: details,
}
response := request.submit()
if response.HasErrors() && p.retries < p.MaxRetry {
p.delayRetry()
p.retries++
response = p.doRequest(eventType, incidentKey, description, client, clientUrl, details)
}
p.retries = 0
return response
}
func (p *PagerDuty) delayRetry() {
interval := float64(p.RetryBaseInterval)
if interval == 0 {
interval = 10
}
delay := math.Pow(2, float64(p.retries)) * interval
duration := time.Duration(delay) * time.Second
log.Printf("Retrying in %v...\n", duration)
time.Sleep(duration)
}