@@ -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,19 @@ type alertManOut struct {
42
49
}
43
50
44
51
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"`
47
65
}
48
66
49
67
func main () {
@@ -76,24 +94,34 @@ func main() {
76
94
}
77
95
78
96
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 {},
81
104
}
82
105
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
86
110
}
87
111
88
112
for _ , alert := range alerts {
89
113
realname := alert .Labels ["instance" ]
90
114
if strings .Contains (realname , "localhost" ) && alert .Labels ["exported_instance" ] != "" {
91
115
realname = alert .Labels ["exported_instance" ]
92
116
}
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
+ })
94
122
}
95
123
96
- DO .Content = Content + "```"
124
+ DO .Embeds = [] discordEmbed { RichEmbed }
97
125
98
126
DOD , _ := json .Marshal (DO )
99
127
http .Post (* whURL , "application/json" , bytes .NewReader (DOD ))
0 commit comments