-
Notifications
You must be signed in to change notification settings - Fork 307
/
pagerduty.go
59 lines (49 loc) · 1.43 KB
/
pagerduty.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
package alert
import (
"bytes"
"encoding/json"
"io"
"net/http"
"time"
"github.com/rudderlabs/rudder-server/config"
"github.com/rudderlabs/rudder-server/utils/httputil"
"github.com/rudderlabs/rudder-server/utils/logger"
)
var (
pagerDutyEndPoint = "https://events.pagerduty.com/v2/enqueue"
pkgLogger logger.Logger
)
func (ops *PagerDuty) Alert(message string) {
payload := map[string]interface{}{
"summary": message,
"severity": "critical",
"source": ops.instanceName,
}
event := map[string]interface{}{
"payload": payload,
"event_action": "trigger",
"routing_key": ops.routingKey,
}
eventJSON, _ := json.Marshal(event)
client := &http.Client{Timeout: config.GetDuration("HttpClient.pagerduty.timeout", 30, time.Second)}
resp, err := client.Post(pagerDutyEndPoint, "application/json", bytes.NewBuffer(eventJSON))
// Not handling errors when sending alert to victorops
if err != nil {
pkgLogger.Errorf("Alert: Failed to alert service: %s", err.Error())
return
}
if resp.StatusCode != 200 && resp.StatusCode != 202 {
pkgLogger.Errorf("Alert: Got error response %d", resp.StatusCode)
}
body, err := io.ReadAll(resp.Body)
defer func() { httputil.CloseResponse(resp) }()
if err != nil {
pkgLogger.Errorf("Alert: Failed to read response body: %s", err.Error())
return
}
pkgLogger.Infof("Alert: Successful %s", string(body))
}
type PagerDuty struct {
instanceName string
routingKey string
}