This repository has been archived by the owner on Jun 21, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
senders.go
56 lines (50 loc) · 1.59 KB
/
senders.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
package notifier
import (
"fmt"
"strings"
"github.com/rcrowley/go-metrics"
)
func run(sender Sender, ch chan notificationPackage) {
defer wg.Done()
for pkg := range ch {
err := sender.SendEvents(pkg.Events, pkg.Contact, pkg.Trigger, pkg.Throttled)
if err == nil {
sendersOkMetrics[pkg.Contact.Type].Mark(1)
} else if !pkg.DontResend {
pkg.resend(err.Error())
}
}
}
// StopSenders close all sending channels
func StopSenders() {
for _, ch := range sending {
close(ch)
}
sending = make(map[string]chan notificationPackage)
log.Debug("Waiting senders finish ...")
wg.Wait()
}
// RegisterSender adds sender for notification type and registers metrics
func RegisterSender(senderSettings map[string]string, sender Sender) error {
var senderIdent string
if senderSettings["type"] == "script" {
senderIdent = senderSettings["name"]
} else {
senderIdent = senderSettings["type"]
}
err := sender.Init(senderSettings, log)
if err != nil {
return fmt.Errorf("Don't initialize sender [%s], err [%s]", senderIdent, err.Error())
}
ch := make(chan notificationPackage)
sending[senderIdent] = ch
sendersOkMetrics[senderIdent] = metrics.NewRegisteredMeter(fmt.Sprintf("%s.sends_ok", getGraphiteSenderIdent(senderIdent)), metrics.DefaultRegistry)
sendersFailedMetrics[senderIdent] = metrics.NewRegisteredMeter(fmt.Sprintf("%s.sends_failed", getGraphiteSenderIdent(senderIdent)), metrics.DefaultRegistry)
wg.Add(1)
go run(sender, ch)
log.Debugf("Sender %s registered", senderIdent)
return nil
}
func getGraphiteSenderIdent(ident string) string {
return strings.Replace(ident, " ", "_", -1)
}