-
Notifications
You must be signed in to change notification settings - Fork 2
/
alerts.go
187 lines (156 loc) · 4.36 KB
/
alerts.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
package main
import (
"fmt"
"github.com/mitchellh/hashstructure"
"github.com/nlopes/slack"
"github.com/spf13/viper"
"log"
"net/url"
"strconv"
"time"
)
func (alert Alert) Hash() string {
hash, err := hashstructure.Hash(map[string]KV{
"labels": alert.Labels,
}, nil)
fatal(err, "Hash cant be calculated")
log.Printf("Hash calculated: %d", hash)
return strconv.FormatUint(hash, 10)
}
func (alert Alert) GeneratePictures() ([]SlackImage, error) {
generatorUrl, err := url.Parse(alert.GeneratorURL)
if err != nil {
return nil, err
}
generatorQuery, err := url.ParseQuery(generatorUrl.RawQuery)
if err != nil {
return nil, err
}
var alertFormula string
for key, param := range generatorQuery {
if key == "g0.expr" {
alertFormula = param[0]
break
}
}
fmt.Println(alertFormula)
plotExpression := GetPlotExpr(alertFormula)
queryTime, duration := alert.GetPlotTimeRange()
var images []SlackImage
for _, expr := range plotExpression {
plot, err := Plot(
expr,
queryTime,
duration,
time.Duration(viper.GetInt64("metric_resolution")),
viper.GetString("prometheus_url"),
alert,
)
if err != nil {
return nil, fmt.Errorf("Plotter error: %v\n", err)
}
publicURL, err := UploadFile(viper.GetString("s3_bucket"), viper.GetString("s3_region"), plot)
if err != nil {
return nil, fmt.Errorf("S3 error: %v\n", err)
}
log.Printf("Graph uploaded, URL: %s", publicURL)
images = append(images, SlackImage{
Url: publicURL,
Title: expr.String(),
})
}
return images, nil
}
func (alert Alert) PostMessage() (string, string, []slack.Block, error) {
log.Printf("Alert: status=%s,Labels=%v,Annotations=%v", alert.Status, alert.Labels, alert.Annotations)
severity := alert.Labels["severity"]
log.Printf("no action on severity: %s", severity)
options := make([]slack.MsgOption, 0)
if alert.Status == AlertStatusFiring || alert.MessageTS == "" {
log.Print("Composing full message")
images, err := alert.GeneratePictures()
if err != nil {
return "", "", nil, err
}
messageBlocks, err := ComposeMessageBody(
alert,
viper.GetString("message_template"),
viper.GetString("header_template"),
images...,
)
if err != nil {
return "", "", nil, err
}
alert.MessageBody = messageBlocks
options = append(options, slack.MsgOptionBlocks(messageBlocks...))
if alert.MessageTS != "" {
options = append(options, slack.MsgOptionBroadcast())
log.Print("Adding broadcast flag to message")
}
} else {
log.Print("Composing short update message")
images, err := alert.GeneratePictures()
if err != nil {
return "", "", nil, err
}
messageBody, err := ComposeResolveUpdateBody(
alert,
viper.GetString("header_template"),
images...,
)
if err != nil {
return "", "", nil, err
}
options = append(options, messageBody)
}
if alert.MessageTS != "" {
log.Printf("MessageTS founded, posting to thread: %s", alert.MessageTS)
options = append(options, slack.MsgOptionTS(alert.MessageTS))
updateBlocks := alert.MessageBody
d, err := ComposeUpdateFooter(alert, viper.GetString("footer_template"))
if err != nil {
return "", "", nil, err
}
updateBlocks = append(updateBlocks, d...)
respChannel, respTimestamp, err := SlackUpdateAlertMessage(
viper.GetString("slack_token"),
alert.Channel,
alert.MessageTS,
slack.MsgOptionBlocks(updateBlocks...),
)
if err != nil {
return "", "", nil, err
}
log.Printf("Slack message updated, channel: %s thread: %s", respChannel, respTimestamp)
}
channel := viper.GetString("slack_channel")
if alert.Channel != "" {
channel = alert.Channel
}
respChannel, respTimestamp, err := SlackSendAlertMessage(
viper.GetString("slack_token"),
channel,
options...,
)
if err != nil {
return "", "", nil, err
}
log.Printf("Slack message sended, channel: %s thread: %s", respChannel, respTimestamp)
return respChannel, respTimestamp, alert.MessageBody, nil
}
func (alert Alert) GetPlotTimeRange() (time.Time, time.Duration) {
var queryTime time.Time
var duration time.Duration
if alert.StartsAt.Second() > alert.EndsAt.Second() {
queryTime = alert.StartsAt
duration = time.Minute * 20
} else {
queryTime = alert.EndsAt
duration = queryTime.Sub(alert.StartsAt)
if duration < time.Minute*20 {
duration = time.Minute * 20
}
}
log.Printf("Querying Time %v Duration: %v", queryTime, duration)
return queryTime, duration
}