code refactor, use NotifyBuiltinChannels to control

This commit is contained in:
Ulric Qin 2022-04-14 18:56:14 +08:00
parent 6d27da8ad8
commit b25c523528
3 changed files with 27 additions and 10 deletions

View File

@ -70,8 +70,8 @@ Batch = 5
[Alerting]
TemplatesDir = "./etc/template"
NotifyConcurrency = 10
# use builtin go code notify by default
NotifyBuiltinEnable = true
# use builtin go code notify
NotifyBuiltinChannels = ["email", "dingtalk", "wecom", "feishu"]
[Alerting.CallScript]
# built in sending capability in go code

View File

@ -155,7 +155,7 @@ type SMTPConfig struct {
type Alerting struct {
TemplatesDir string
NotifyConcurrency int
NotifyBuiltinEnable bool
NotifyBuiltinChannels []string
CallScript CallScript
CallPlugin CallPlugin
RedisPub RedisPub

View File

@ -19,6 +19,7 @@ import (
"github.com/toolkits/pkg/file"
"github.com/toolkits/pkg/logger"
"github.com/toolkits/pkg/runner"
"github.com/toolkits/pkg/slice"
"github.com/didi/nightingale/v5/src/models"
"github.com/didi/nightingale/v5/src/pkg/sys"
@ -123,7 +124,7 @@ func handleNotice(notice Notice, bs []byte) {
alertingCallPlugin(bs)
if !config.C.Alerting.NotifyBuiltinEnable {
if len(config.C.Alerting.NotifyBuiltinChannels) == 0 {
return
}
@ -173,6 +174,10 @@ func handleNotice(notice Notice, bs []byte) {
continue
}
if !slice.ContainsString(config.C.Alerting.NotifyBuiltinChannels, "email") {
continue
}
subject, has := notice.Tpls["subject.tpl"]
if !has {
subject = "subject.tpl not found"
@ -189,6 +194,10 @@ func handleNotice(notice Notice, bs []byte) {
continue
}
if !slice.ContainsString(config.C.Alerting.NotifyBuiltinChannels, "dingtalk") {
continue
}
content, has := notice.Tpls["dingtalk.tpl"]
if !has {
content = "dingtalk.tpl not found"
@ -205,6 +214,10 @@ func handleNotice(notice Notice, bs []byte) {
continue
}
if !slice.ContainsString(config.C.Alerting.NotifyBuiltinChannels, "wecom") {
continue
}
content, has := notice.Tpls["wecom.tpl"]
if !has {
content = "wecom.tpl not found"
@ -218,6 +231,10 @@ func handleNotice(notice Notice, bs []byte) {
continue
}
if !slice.ContainsString(config.C.Alerting.NotifyBuiltinChannels, "feishu") {
continue
}
content, has := notice.Tpls["feishu.tpl"]
if !has {
content = "feishu.tpl not found"