-
Notifications
You must be signed in to change notification settings - Fork 2
/
discord_alerter.go
197 lines (171 loc) · 4.68 KB
/
discord_alerter.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
188
189
190
191
192
193
194
195
196
197
package alerter
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"log/slog"
"net/http"
"time"
"github.com/goware/cachestore"
"github.com/goware/cachestore/cachestorectl"
"github.com/goware/cachestore/memlru"
)
type discordAlerter struct {
Logger *slog.Logger
Env string
WebhookURL string
Username string
AvatarURL string
MentionRoleID uint64
SkipLogEntry bool
errStore cachestore.Store[bool]
Client *http.Client
}
var _ Alerter = &discordAlerter{}
func NewDiscordAlerter(cfg *Config) (Alerter, error) {
if cfg.WebhookURL == "" {
return nil, fmt.Errorf("webhook url is required")
}
if cfg.Env == "" {
return nil, fmt.Errorf("env is required")
}
if cfg.Service == "" {
cfg.Service = "Alerter"
}
username, _ := cfg.Extra["username"].(string)
if username == "" {
username = cfg.Service
}
avatarURL, _ := cfg.Extra["avatarUrl"].(string)
if avatarURL == "" {
avatarURL = "https://cdn.discordapp.com/embed/avatars/4.png"
}
mentionRoleID, _ := cfg.Extra["mentionRoleId"].(uint64)
if cfg.AlertCooldown == 0 {
cfg.AlertCooldown = 1 * time.Minute
}
if cfg.Client == nil {
cfg.Client = http.DefaultClient
}
if cfg.CacheBackend == nil {
cfg.CacheBackend = memlru.Backend(512)
}
mstore, err := cachestorectl.Open[bool](cfg.CacheBackend, cachestore.WithDefaultKeyExpiry(cfg.AlertCooldown))
if err != nil {
return nil, err
}
return &discordAlerter{
Logger: cfg.Logger,
Env: cfg.Env,
WebhookURL: cfg.WebhookURL,
Username: username,
AvatarURL: avatarURL,
MentionRoleID: mentionRoleID,
SkipLogEntry: cfg.SkipLogEntry,
errStore: mstore,
Client: cfg.Client,
}, nil
}
func (a *discordAlerter) Alert(ctx context.Context, format string, v ...interface{}) {
// log it
if !a.SkipLogEntry && a.Logger != nil {
a.Logger.With("alert", "alert").Error(fmt.Sprintf(format, v...))
}
cacheKey := fmt.Sprintf("%d", xxh64FromString(fmt.Sprintf(format, v...)))
if _, exists, _ := a.errStore.Get(ctx, cacheKey); exists {
return
}
p, err := a.formJsonPayload(format, v...)
if err != nil {
if a.Logger != nil {
a.Logger.With("alert", "alert", "err", err).Error(fmt.Sprintf("failed to form json payload: %v", err))
}
return
}
a.doRequest(ctx, cacheKey, p)
}
func (a *discordAlerter) doRequest(ctx context.Context, cacheKey string, payload string) {
req, err := http.NewRequestWithContext(ctx, "POST", a.WebhookURL, bytes.NewReader([]byte(payload)))
if err != nil {
if a.Logger != nil {
a.Logger.With("alert", "alert", "err", err).Error(fmt.Sprintf("failed to create request: %v", err))
}
return
}
req.Header.Set("Content-Type", "application/json")
resp, err := a.Client.Do(req)
if err != nil {
if a.Logger != nil {
a.Logger.With("alert", "alert", "err", err).Error(fmt.Sprintf("failed to send alert: %v", err))
}
return
}
defer resp.Body.Close()
switch statusCode := resp.StatusCode; {
case (statusCode >= http.StatusOK && statusCode < 300):
// success - cache the alert
a.errStore.Set(ctx, cacheKey, true)
return
case statusCode == 429:
if a.Logger != nil {
a.Logger.With("alert", "alert").Error("alerter has been rate limited")
}
timeToWait, err := time.ParseDuration(req.Header.Get("Retry-After"))
if err != nil {
if a.Logger != nil {
a.Logger.With("alert", "alert", "err", err).Error(fmt.Sprintf("failed to parse retry after header: %v", err))
}
}
go func() {
time.Sleep(timeToWait)
a.doRequest(ctx, cacheKey, payload)
}()
default:
body, _ := io.ReadAll(resp.Body)
if a.Logger != nil {
a.Logger.With("alert", "alert").Error(fmt.Sprintf("unexpected status code: %v, body: %v", resp.StatusCode, string(body)))
}
}
}
type embed struct {
Author struct {
Name string `json:"name"`
IconURL string `json:"icon_url"`
}
Title string `json:"title"`
Description string `json:"description"`
Color int `json:"color"`
}
type payload struct {
Username string `json:"username"`
AvatarURL string `json:"avatar_url"`
Content string `json:"content"`
Embeds []embed `json:"embeds"`
}
func (a *discordAlerter) formJsonPayload(format string, v ...interface{}) (string, error) {
p := payload{
Username: a.Username,
AvatarURL: a.AvatarURL,
Embeds: []embed{
{
Author: struct {
Name string `json:"name"`
IconURL string `json:"icon_url"`
}{Name: a.Username, IconURL: a.AvatarURL},
Title: fmt.Sprintf("Alert - %s", a.Env),
Description: fmt.Sprintf(format, v...),
Color: 0xff0000,
},
},
}
if a.MentionRoleID > 0 {
p.Content = fmt.Sprintf("<@&%d>", a.MentionRoleID)
}
b, err := json.Marshal(p)
if err != nil {
return "", err
}
return string(b), nil
}