-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathalerter.go
More file actions
69 lines (52 loc) · 1.48 KB
/
Copy pathalerter.go
File metadata and controls
69 lines (52 loc) · 1.48 KB
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
package alerter
import (
"context"
"fmt"
"log/slog"
"net/http"
"time"
cachestore "github.com/goware/cachestore2"
)
type Alerter interface {
Alert(ctx context.Context, format string, v ...interface{})
}
type Config struct {
// Noop toggle. If true will return a noop alerter.
Noop bool
// Logger
Logger *slog.Logger
// (required) WebhookURL is the discord webhook url for a channel
WebhookURL string
// (required) Env is the environment name that will be added to the title
Env string
// (required) Service is the name which will appear in the alert message
Service string
// (optional) Extra are extra config options to a specific sink
Extra map[string]interface{}
// (optional) AlertCooldown is the time to wait before sending the same alert again
AlertCooldown time.Duration
// (optional) Skip log entry on alert. In this case, its expected you will log on your own
SkipLogEntry bool
// Overrides the default http client and cache backend
Client *http.Client
CacheBackend cachestore.Backend
}
func NewAlerter(destination string, cfg *Config) (Alerter, error) {
if cfg.Noop {
return Noop(), nil
}
switch destination {
case "discord":
return NewDiscordAlerter(cfg)
case "slack":
return NewSlackAlerter(cfg)
default:
return nil, fmt.Errorf("alerter: unsupported destination '%s'", destination)
}
}
func Noop() Alerter {
return &noopAlerter{}
}
type noopAlerter struct{}
func (a *noopAlerter) Alert(ctx context.Context, format string, v ...interface{}) {
}