Skip to content

Commit 093b67f

Browse files
committed
Send alerts in Discord's rich embed format
1 parent 3b4fad8 commit 093b67f

File tree

1 file changed

+37
-9
lines changed

1 file changed

+37
-9
lines changed

main.go

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ import (
1111
"strings"
1212
)
1313

14+
// Discord color values
15+
const (
16+
ColorRed = 10038562
17+
ColorGreen = 3066993
18+
ColorGrey = 9807270
19+
)
20+
1421
type alertManAlert struct {
1522
Annotations struct {
1623
Description string `json:"description"`
@@ -42,8 +49,19 @@ type alertManOut struct {
4249
}
4350

4451
type discordOut struct {
45-
Content string `json:"content"`
46-
Name string `json:"username"`
52+
Embeds []discordEmbed `json:"embeds"`
53+
}
54+
55+
type discordEmbed struct {
56+
Title string `json:"title"`
57+
Description string `json:"description"`
58+
Color int `json:"color"`
59+
Fields []discordEmbedField `json:"fields"`
60+
}
61+
62+
type discordEmbedField struct {
63+
Name string `json:"name"`
64+
Value string `json:"value"`
4765
}
4866

4967
func main() {
@@ -76,24 +94,34 @@ func main() {
7694
}
7795

7896
for status, alerts := range groupedAlerts {
79-
DO := discordOut{
80-
Name: status,
97+
DO := discordOut{}
98+
99+
RichEmbed := discordEmbed{
100+
Title: amo.CommonLabels.Alertname,
101+
Description: amo.CommonAnnotations.Summary,
102+
Color: ColorGrey,
103+
Fields: []discordEmbedField{},
81104
}
82105

83-
Content := "```"
84-
if amo.CommonAnnotations.Summary != "" {
85-
Content = fmt.Sprintf(" === %s === \n```", amo.CommonAnnotations.Summary)
106+
if status == "firing" {
107+
RichEmbed.Color = ColorRed
108+
} else if status == "resolved" {
109+
RichEmbed.Color = ColorGreen
86110
}
87111

88112
for _, alert := range alerts {
89113
realname := alert.Labels["instance"]
90114
if strings.Contains(realname, "localhost") && alert.Labels["exported_instance"] != "" {
91115
realname = alert.Labels["exported_instance"]
92116
}
93-
Content += fmt.Sprintf("[%s]: %s on %s\n%s\n\n", strings.ToUpper(status), alert.Labels["alertname"], realname, alert.Annotations.Description)
117+
118+
RichEmbed.Fields = append(RichEmbed.Fields, discordEmbedField{
119+
Name: fmt.Sprintf("[%s]: %s on %s", strings.ToUpper(status), alert.Labels["alertname"], realname),
120+
Value: alert.Annotations.Description,
121+
})
94122
}
95123

96-
DO.Content = Content + "```"
124+
DO.Embeds = []discordEmbed{RichEmbed}
97125

98126
DOD, _ := json.Marshal(DO)
99127
http.Post(*whURL, "application/json", bytes.NewReader(DOD))

0 commit comments

Comments
 (0)