/
service.go
56 lines (48 loc) · 1.22 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
package prometheus
import (
"encoding/json"
"fmt"
"github.com/weAutomateEverything/go2hal/alert"
"golang.org/x/net/context"
"sort"
)
type Service interface {
sendPrometheusAlert(ctx context.Context, chat uint32, message string) error
}
func NewService(alertService alert.Service) Service {
return &service{
alertService: alertService,
}
}
type service struct {
alertService alert.Service
}
func (s *service) sendPrometheusAlert(ctx context.Context, chat uint32, message string) (err error) {
var r map[string]interface{}
err = json.Unmarshal([]byte(message), &r)
if err != nil {
return
}
alerts := r["alerts"].([]interface{})
for _, alert := range alerts {
a := alert.(map[string]interface{})
msg := a["status"].(string) + "\n"
labels := a["labels"].(map[string]interface{})
keys := make([]string, 1)
for key, _ := range labels {
keys = append(keys, key)
}
sort.Strings(keys)
for _, key := range keys {
if key != "" {
msg = msg + fmt.Sprintf("*%v*: %v\n", key, labels[key])
}
}
annotation := a["annotations"].(map[string]interface{})
for key, value := range annotation {
msg = msg + fmt.Sprintf("*%v*: %v\n", key, value)
}
s.alertService.SendAlert(ctx, chat, msg)
}
return nil
}