-
Notifications
You must be signed in to change notification settings - Fork 492
/
service.go
197 lines (170 loc) · 4.11 KB
/
service.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package pagerduty
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"sync/atomic"
"github.com/influxdata/kapacitor/alert"
"github.com/influxdata/kapacitor/keyvalue"
)
type Diagnostic interface {
WithContext(ctx ...keyvalue.T) Diagnostic
Error(msg string, err error)
}
type Service struct {
configValue atomic.Value
HTTPDService interface {
URL() string
}
diag Diagnostic
}
func NewService(c Config, d Diagnostic) *Service {
s := &Service{
diag: d,
}
s.configValue.Store(c)
return s
}
func (s *Service) Open() error {
return nil
}
func (s *Service) Close() error {
return nil
}
func (s *Service) config() Config {
return s.configValue.Load().(Config)
}
func (s *Service) Update(newConfig []interface{}) error {
if l := len(newConfig); l != 1 {
return fmt.Errorf("expected only one new config object, got %d", l)
}
if c, ok := newConfig[0].(Config); !ok {
return fmt.Errorf("expected config object to be of type %T, got %T", c, newConfig[0])
} else {
s.configValue.Store(c)
}
return nil
}
func (s *Service) Global() bool {
c := s.config()
return c.Global
}
type testOptions struct {
IncidentKey string `json:"incident-key"`
Description string `json:"description"`
Details string `json:"details"`
Level alert.Level `json:"level"`
}
func (s *Service) TestOptions() interface{} {
return &testOptions{
IncidentKey: "testIncidentKey",
Description: "test pagerduty message",
Level: alert.Critical,
}
}
func (s *Service) Test(options interface{}) error {
o, ok := options.(*testOptions)
if !ok {
return fmt.Errorf("unexpected options type %T", options)
}
c := s.config()
return s.Alert(
c.ServiceKey,
o.IncidentKey,
o.Description,
o.Level,
o.Details,
)
}
func (s *Service) Alert(serviceKey, incidentKey, desc string, level alert.Level, details string) error {
url, post, err := s.preparePost(serviceKey, incidentKey, desc, level, details)
if err != nil {
return err
}
resp, err := http.Post(url, "application/json", post)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
type response struct {
Message string `json:"message"`
}
r := &response{Message: fmt.Sprintf("failed to understand PagerDuty response. code: %d content: %s", resp.StatusCode, string(body))}
b := bytes.NewReader(body)
dec := json.NewDecoder(b)
dec.Decode(r)
return errors.New(r.Message)
}
return nil
}
func (s *Service) preparePost(serviceKey, incidentKey, desc string, level alert.Level, details string) (string, io.Reader, error) {
c := s.config()
if !c.Enabled {
return "", nil, errors.New("service is not enabled")
}
var eventType string
switch level {
case alert.Warning, alert.Critical:
eventType = "trigger"
case alert.Info:
return "", nil, fmt.Errorf("AlertLevel 'info' is currently ignored by the PagerDuty service")
default:
eventType = "resolve"
}
pData := make(map[string]string)
if serviceKey == "" {
pData["service_key"] = c.ServiceKey
} else {
pData["service_key"] = serviceKey
}
pData["event_type"] = eventType
pData["description"] = desc
pData["incident_key"] = incidentKey
pData["client"] = "kapacitor"
pData["client_url"] = s.HTTPDService.URL()
pData["details"] = details
// Post data to PagerDuty
var post bytes.Buffer
enc := json.NewEncoder(&post)
err := enc.Encode(pData)
if err != nil {
return "", nil, err
}
return c.URL, &post, nil
}
type HandlerConfig struct {
// The service key to use for the alert.
// Defaults to the value in the configuration if empty.
ServiceKey string `mapstructure:"service-key"`
}
type handler struct {
s *Service
c HandlerConfig
diag Diagnostic
}
func (s *Service) Handler(c HandlerConfig, ctx ...keyvalue.T) alert.Handler {
return &handler{
s: s,
c: c,
diag: s.diag.WithContext(ctx...),
}
}
func (h *handler) Handle(event alert.Event) {
if err := h.s.Alert(
h.c.ServiceKey,
event.State.ID,
event.State.Message,
event.State.Level,
event.State.Details,
); err != nil {
h.diag.Error("failed to send event to PagerDuty", err)
}
}