forked from influxdata/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
472 lines (414 loc) · 10.7 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
package alerta
import (
"bytes"
"crypto/tls"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"path"
"sync/atomic"
text "text/template"
"time"
"github.com/influxdata/kapacitor/alert"
"github.com/influxdata/kapacitor/keyvalue"
"github.com/influxdata/kapacitor/models"
"github.com/pkg/errors"
)
const (
defaultResource = "{{ .Name }}"
defaultEvent = "{{ .ID }}"
defaultGroup = "{{ .Group }}"
defaultTimeout = time.Duration(24 * time.Hour)
defaultTokenPrefix = "Bearer"
)
type Diagnostic interface {
WithContext(ctx ...keyvalue.T) Diagnostic
TemplateError(err error, kv keyvalue.T)
Error(msg string, err error)
}
type Service struct {
configValue atomic.Value
clientValue atomic.Value
diag Diagnostic
}
func NewService(c Config, d Diagnostic) *Service {
s := &Service{
diag: d,
}
s.configValue.Store(c)
s.clientValue.Store(&http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
TLSClientConfig: &tls.Config{InsecureSkipVerify: c.InsecureSkipVerify},
},
})
return s
}
type testOptions struct {
Resource string `json:"resource"`
Event string `json:"event"`
Environment string `json:"environment"`
Severity string `json:"severity"`
Group string `json:"group"`
Value string `json:"value"`
Message string `json:"message"`
Origin string `json:"origin"`
Service []string `json:"service"`
Timeout string `json:"timeout"`
}
func (s *Service) TestOptions() interface{} {
c := s.config()
return &testOptions{
Resource: "testResource",
Event: "testEvent",
Environment: c.Environment,
Severity: "critical",
Group: "testGroup",
Value: "testValue",
Message: "test alerta message",
Origin: c.Origin,
Service: []string{"testServiceA", "testServiceB"},
Timeout: "24h0m0s",
}
}
func (s *Service) Test(options interface{}) error {
o, ok := options.(*testOptions)
if !ok {
return fmt.Errorf("unexpected options type %T", options)
}
c := s.config()
timeout, _ := time.ParseDuration(o.Timeout)
return s.Alert(
c.Token,
c.TokenPrefix,
o.Resource,
o.Event,
o.Environment,
o.Severity,
o.Group,
o.Value,
o.Message,
o.Origin,
o.Service,
timeout,
map[string]string{},
models.Result{},
)
}
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)
s.clientValue.Store(&http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
TLSClientConfig: &tls.Config{InsecureSkipVerify: c.InsecureSkipVerify},
},
})
}
return nil
}
func (s *Service) Alert(token, tokenPrefix, resource, event, environment, severity, group, value, message, origin string, service []string, timeout time.Duration, tags map[string]string, data models.Result) error {
if resource == "" || event == "" {
return errors.New("Resource and Event are required to send an alert")
}
req, err := s.preparePost(token, tokenPrefix, resource, event, environment, severity, group, value, message, origin, service, timeout, tags, data)
if err != nil {
return err
}
client := s.clientValue.Load().(*http.Client)
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusCreated {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
type response struct {
Message string `json:"message"`
}
r := &response{Message: fmt.Sprintf("failed to understand Alerta 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(token, tokenPrefix, resource, event, environment, severity, group, value, message, origin string, service []string, timeout time.Duration, tags map[string]string, data models.Result) (*http.Request, error) {
c := s.config()
if !c.Enabled {
return nil, errors.New("service is not enabled")
}
if token == "" {
token = c.Token
}
if tokenPrefix == "" {
if c.TokenPrefix == "" {
tokenPrefix = defaultTokenPrefix
} else {
tokenPrefix = c.TokenPrefix
}
}
if environment == "" {
environment = c.Environment
}
if origin == "" {
origin = c.Origin
}
u, err := url.Parse(c.URL)
if err != nil {
return nil, err
}
u.Path = path.Join(u.Path, "alert")
postData := make(map[string]interface{})
postData["resource"] = resource
postData["event"] = event
postData["environment"] = environment
postData["severity"] = severity
postData["group"] = group
postData["value"] = value
postData["text"] = message
postData["origin"] = origin
postData["rawData"] = data
if len(service) > 0 {
postData["service"] = service
}
postData["timeout"] = int64(timeout / time.Second)
tagList := make([]string, 0)
for k, v := range tags {
tagList = append(tagList, fmt.Sprintf("%s=%s", k, v))
}
postData["tags"] = tagList
var post bytes.Buffer
enc := json.NewEncoder(&post)
err = enc.Encode(postData)
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", u.String(), &post)
req.Header.Add("Authorization", tokenPrefix+" "+token)
req.Header.Add("Content-Type", "application/json")
if err != nil {
return nil, err
}
return req, nil
}
type HandlerConfig struct {
// Alerta authentication token.
// If empty uses the token from the configuration.
Token string `mapstructure:"token"`
// Alerta authentication token prefix.
// If empty uses Bearer.
TokenPrefix string `mapstructure:"token-prefix"`
// Alerta resource.
// Can be a template and has access to the same data as the AlertNode.Details property.
// Default: {{ .Name }}
Resource string `mapstructure:"resource"`
// Alerta event.
// Can be a template and has access to the same data as the idInfo property.
// Default: {{ .ID }}
Event string `mapstructure:"event"`
// Alerta environment.
// Can be a template and has access to the same data as the AlertNode.Details property.
// Defaut is set from the configuration.
Environment string `mapstructure:"environment"`
// Alerta group.
// Can be a template and has access to the same data as the AlertNode.Details property.
// Default: {{ .Group }}
Group string `mapstructure:"group"`
// Alerta value.
// Can be a template and has access to the same data as the AlertNode.Details property.
// Default is an empty string.
Value string `mapstructure:"value"`
// Alerta origin.
// If empty uses the origin from the configuration.
Origin string `mapstructure:"origin"`
// List of effected Services
Service []string `mapstructure:"service"`
// Alerta timeout.
// Default: 24h
Timeout time.Duration `mapstructure:"timeout"`
}
type handler struct {
s *Service
c HandlerConfig
diag Diagnostic
resourceTmpl *text.Template
eventTmpl *text.Template
environmentTmpl *text.Template
valueTmpl *text.Template
groupTmpl *text.Template
serviceTmpl []*text.Template
}
func (s *Service) DefaultHandlerConfig() HandlerConfig {
return HandlerConfig{
Resource: defaultResource,
Event: defaultEvent,
Group: defaultGroup,
Timeout: defaultTimeout,
}
}
func (s *Service) Handler(c HandlerConfig, ctx ...keyvalue.T) (alert.Handler, error) {
// Parse and validate alerta templates
rtmpl, err := text.New("resource").Parse(c.Resource)
if err != nil {
return nil, err
}
evtmpl, err := text.New("event").Parse(c.Event)
if err != nil {
return nil, err
}
etmpl, err := text.New("environment").Parse(c.Environment)
if err != nil {
return nil, err
}
gtmpl, err := text.New("group").Parse(c.Group)
if err != nil {
return nil, err
}
vtmpl, err := text.New("value").Parse(c.Value)
if err != nil {
return nil, err
}
var stmpl []*text.Template
for _, service := range c.Service {
tmpl, err := text.New("service").Parse(service)
if err != nil {
return nil, err
}
stmpl = append(stmpl, tmpl)
}
return &handler{
s: s,
c: c,
diag: s.diag.WithContext(ctx...),
resourceTmpl: rtmpl,
eventTmpl: evtmpl,
environmentTmpl: etmpl,
groupTmpl: gtmpl,
valueTmpl: vtmpl,
serviceTmpl: stmpl,
}, nil
}
type eventData struct {
ID string
// Measurement name
Name string
// Task name
TaskName string
// Concatenation of all group-by tags of the form [key=value,]+.
// If not groupBy is performed equal to literal 'nil'
Group string
// Map of tags
Tags map[string]string
}
func (h *handler) Handle(event alert.Event) {
td := event.TemplateData()
var buf bytes.Buffer
err := h.resourceTmpl.Execute(&buf, td)
if err != nil {
h.diag.TemplateError(err, keyvalue.KV("resource", h.c.Resource))
return
}
resource := buf.String()
buf.Reset()
data := eventData{
ID: td.ID,
Name: td.Name,
TaskName: td.TaskName,
Group: td.Group,
Tags: td.Tags,
}
err = h.eventTmpl.Execute(&buf, data)
if err != nil {
h.diag.TemplateError(err, keyvalue.KV("event", h.c.Event))
return
}
eventStr := buf.String()
buf.Reset()
err = h.environmentTmpl.Execute(&buf, td)
if err != nil {
h.diag.TemplateError(err, keyvalue.KV("environment", h.c.Environment))
return
}
environment := buf.String()
buf.Reset()
err = h.groupTmpl.Execute(&buf, td)
if err != nil {
h.diag.TemplateError(err, keyvalue.KV("group", h.c.Group))
return
}
group := buf.String()
buf.Reset()
err = h.valueTmpl.Execute(&buf, td)
if err != nil {
h.diag.TemplateError(err, keyvalue.KV("value", h.c.Value))
return
}
value := buf.String()
buf.Reset()
var service []string
if len(h.serviceTmpl) == 0 {
service = []string{td.Name}
} else {
for _, tmpl := range h.serviceTmpl {
err = tmpl.Execute(&buf, td)
if err != nil {
h.diag.TemplateError(err, keyvalue.KV("service", tmpl.Name()))
return
}
service = append(service, buf.String())
buf.Reset()
}
}
var severity string
switch event.State.Level {
case alert.OK:
severity = "ok"
case alert.Info:
severity = "informational"
case alert.Warning:
severity = "warning"
case alert.Critical:
severity = "critical"
default:
severity = "indeterminate"
}
if err := h.s.Alert(
h.c.Token,
h.c.TokenPrefix,
resource,
eventStr,
environment,
severity,
group,
value,
event.State.Message,
h.c.Origin,
service,
h.c.Timeout,
event.Data.Tags,
event.Data.Result,
); err != nil {
h.diag.Error("failed to send event to Alerta", err)
}
}