Skip to content

Commit 7a32366

Browse files
author
nerves_dev
committed
Refactoring: Split send alert messages into func
1 parent 49e3076 commit 7a32366

File tree

1 file changed

+47
-44
lines changed

1 file changed

+47
-44
lines changed

main.go

Lines changed: 47 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,52 @@ func checkWhURL(whURL string) {
9090
}
9191
}
9292

93+
func sendWebhook(amo *alertManOut) {
94+
groupedAlerts := make(map[string][]alertManAlert)
95+
96+
for _, alert := range amo.Alerts {
97+
groupedAlerts[alert.Status] = append(groupedAlerts[alert.Status], alert)
98+
}
99+
100+
for status, alerts := range groupedAlerts {
101+
DO := discordOut{}
102+
103+
RichEmbed := discordEmbed{
104+
Title: fmt.Sprintf("[%s:%d] %s", strings.ToUpper(status), len(alerts), amo.CommonLabels.Alertname),
105+
Description: amo.CommonAnnotations.Summary,
106+
Color: ColorGrey,
107+
Fields: []discordEmbedField{},
108+
}
109+
110+
if status == "firing" {
111+
RichEmbed.Color = ColorRed
112+
} else if status == "resolved" {
113+
RichEmbed.Color = ColorGreen
114+
}
115+
116+
if amo.CommonAnnotations.Summary != "" {
117+
DO.Content = fmt.Sprintf(" === %s === \n", amo.CommonAnnotations.Summary)
118+
}
119+
120+
for _, alert := range alerts {
121+
realname := alert.Labels["instance"]
122+
if strings.Contains(realname, "localhost") && alert.Labels["exported_instance"] != "" {
123+
realname = alert.Labels["exported_instance"]
124+
}
125+
126+
RichEmbed.Fields = append(RichEmbed.Fields, discordEmbedField{
127+
Name: fmt.Sprintf("[%s]: %s on %s", strings.ToUpper(status), alert.Labels["alertname"], realname),
128+
Value: alert.Annotations.Description,
129+
})
130+
}
131+
132+
DO.Embeds = []discordEmbed{RichEmbed}
133+
134+
DOD, _ := json.Marshal(DO)
135+
http.Post(*whURL, "application/json", bytes.NewReader(DOD))
136+
}
137+
}
138+
93139
func main() {
94140
flag.Parse()
95141
checkWhURL(*whURL)
@@ -147,49 +193,6 @@ func main() {
147193

148194
return
149195
}
150-
151-
groupedAlerts := make(map[string][]alertManAlert)
152-
153-
for _, alert := range amo.Alerts {
154-
groupedAlerts[alert.Status] = append(groupedAlerts[alert.Status], alert)
155-
}
156-
157-
for status, alerts := range groupedAlerts {
158-
DO := discordOut{}
159-
160-
RichEmbed := discordEmbed{
161-
Title: fmt.Sprintf("[%s:%d] %s", strings.ToUpper(status), len(alerts), amo.CommonLabels.Alertname),
162-
Description: amo.CommonAnnotations.Summary,
163-
Color: ColorGrey,
164-
Fields: []discordEmbedField{},
165-
}
166-
167-
if status == "firing" {
168-
RichEmbed.Color = ColorRed
169-
} else if status == "resolved" {
170-
RichEmbed.Color = ColorGreen
171-
}
172-
173-
if amo.CommonAnnotations.Summary != "" {
174-
DO.Content = fmt.Sprintf(" === %s === \n", amo.CommonAnnotations.Summary)
175-
}
176-
177-
for _, alert := range alerts {
178-
realname := alert.Labels["instance"]
179-
if strings.Contains(realname, "localhost") && alert.Labels["exported_instance"] != "" {
180-
realname = alert.Labels["exported_instance"]
181-
}
182-
183-
RichEmbed.Fields = append(RichEmbed.Fields, discordEmbedField{
184-
Name: fmt.Sprintf("[%s]: %s on %s", strings.ToUpper(status), alert.Labels["alertname"], realname),
185-
Value: alert.Annotations.Description,
186-
})
187-
}
188-
189-
DO.Embeds = []discordEmbed{RichEmbed}
190-
191-
DOD, _ := json.Marshal(DO)
192-
http.Post(*whURL, "application/json", bytes.NewReader(DOD))
193-
}
196+
sendWebhook(&amo)
194197
}))
195198
}

0 commit comments

Comments
 (0)