@@ -11,6 +11,13 @@ import (
11
11
"strings"
12
12
)
13
13
14
+ // Discord color values
15
+ const (
16
+ ColorRed = 10038562
17
+ ColorGreen = 3066993
18
+ ColorGrey = 9807270
19
+ )
20
+
14
21
type alertManAlert struct {
15
22
Annotations struct {
16
23
Description string `json:"description"`
@@ -42,8 +49,20 @@ type alertManOut struct {
42
49
}
43
50
44
51
type discordOut struct {
45
- Content string `json:"content"`
46
- Name string `json:"username"`
52
+ Content string `json:"content"`
53
+ Embeds []discordEmbed `json:"embeds"`
54
+ }
55
+
56
+ type discordEmbed struct {
57
+ Title string `json:"title"`
58
+ Description string `json:"description"`
59
+ Color int `json:"color"`
60
+ Fields []discordEmbedField `json:"fields"`
61
+ }
62
+
63
+ type discordEmbedField struct {
64
+ Name string `json:"name"`
65
+ Value string `json:"value"`
47
66
}
48
67
49
68
func main () {
@@ -76,24 +95,38 @@ func main() {
76
95
}
77
96
78
97
for status , alerts := range groupedAlerts {
79
- DO := discordOut {
80
- Name : status ,
98
+ DO := discordOut {}
99
+
100
+ RichEmbed := discordEmbed {
101
+ Title : fmt .Sprintf ("[%s:%d] %s" , strings .ToUpper (status ), len (alerts ), amo .CommonLabels .Alertname ),
102
+ Description : amo .CommonAnnotations .Summary ,
103
+ Color : ColorGrey ,
104
+ Fields : []discordEmbedField {},
105
+ }
106
+
107
+ if status == "firing" {
108
+ RichEmbed .Color = ColorRed
109
+ } else if status == "resolved" {
110
+ RichEmbed .Color = ColorGreen
81
111
}
82
112
83
- Content := "```"
84
113
if amo .CommonAnnotations .Summary != "" {
85
- Content = fmt .Sprintf (" === %s === \n ``` " , amo .CommonAnnotations .Summary )
114
+ DO . Content = fmt .Sprintf (" === %s === \n " , amo .CommonAnnotations .Summary )
86
115
}
87
116
88
117
for _ , alert := range alerts {
89
118
realname := alert .Labels ["instance" ]
90
119
if strings .Contains (realname , "localhost" ) && alert .Labels ["exported_instance" ] != "" {
91
120
realname = alert .Labels ["exported_instance" ]
92
121
}
93
- Content += fmt .Sprintf ("[%s]: %s on %s\n %s\n \n " , strings .ToUpper (status ), alert .Labels ["alertname" ], realname , alert .Annotations .Description )
122
+
123
+ RichEmbed .Fields = append (RichEmbed .Fields , discordEmbedField {
124
+ Name : fmt .Sprintf ("[%s]: %s on %s" , strings .ToUpper (status ), alert .Labels ["alertname" ], realname ),
125
+ Value : alert .Annotations .Description ,
126
+ })
94
127
}
95
128
96
- DO .Content = Content + "```"
129
+ DO .Embeds = [] discordEmbed { RichEmbed }
97
130
98
131
DOD , _ := json .Marshal (DO )
99
132
http .Post (* whURL , "application/json" , bytes .NewReader (DOD ))
0 commit comments