-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
new notify process #2464
base: notify-rule-feat
Are you sure you want to change the base?
new notify process #2464
Conversation
|
||
func MessageTemplateGetsAll(ctx *ctx.Context) ([]*MessageTemplate, error) { | ||
if !ctx.IsCenter { | ||
templates, err := poster.GetByUrls[[]*MessageTemplate](ctx, "/v1/n9e/message-templates") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个 api 看在 router 里没加上
models/notify_channel.go
Outdated
"encoding/base64" | ||
"encoding/json" | ||
"fmt" | ||
"github.com/ccfos/nightingale/v6/pkg/ctx" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pkg 顺序调整下
|
||
func NotifyChannelGetsAll(ctx *ctx.Context) ([]*NotifyChannelConfig, error) { | ||
if !ctx.IsCenter { | ||
channels, err := poster.GetByUrls[[]*NotifyChannelConfig](ctx, "/v1/n9e/notify-channels") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
实现下 /v1/n9e/notify-channels 接口
models/notify_channel.go
Outdated
func GetSMTPClient(nc *NotifyChannelConfig) (*smtp.Client, error) { | ||
// 连接到 SMTP 服务器 | ||
addr := fmt.Sprintf("%s:%d", nc.SMTPRequestConfig.Host, nc.SMTPRequestConfig.Port) | ||
client, err := smtp.Dial(addr) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里可以参考之前的 smtp 代码,使用 gomail.NewDialer,smtp.Dial不确定是否会踩坑
alert/dispatch/dispatch.go
Outdated
|
||
switch notifyChannel.RequestType { | ||
case "http": | ||
notifyChannel.SendHTTP(content, params, e.notifyChannelCache.GetHttpClient(notifyChannel.ID)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里需要把返回的 err 信息打印出来,方便排查问题
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里需要把 events 也传进去,方便在 http 参数和 body 中使用,更灵活一些
alert/dispatch/dispatch.go
Outdated
userGroups := e.userGroupCache.GetByUserGroupIds(p.UserGroupIDs) | ||
var origin []string | ||
var val []string | ||
if notifyChannel.ParamConfig.UserInfo.ContactKey == "phone" { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里缺少了对用户的其他联系方式的处理,比如公司内部 im,个人相关的 token
models/notify_channel.go
Outdated
|
||
// 将 MessageTemplate 与变量配置的信息渲染进 reqBody | ||
body, err := ncc.parseRequestBody(content, param) | ||
if err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里 err 需要打印日志,方便排查问题
models/notify_channel.go
Outdated
return body.Bytes(), err | ||
} | ||
|
||
func (ncc *NotifyChannelConfig) replaceVariables(param map[string]string) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里用 go 模板渲染的方式处理更灵活一些
models/notify_channel.go
Outdated
for i := range params { | ||
param := params[i] | ||
var to []string | ||
if ncc.ParamConfig.BatchSend { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
邮件发送,直接批量发送就可以了,不需要使用这个参数区分了
models/notify_channel.go
Outdated
|
||
m.SetHeader("From", ncc.SMTPRequestConfig.From) | ||
m.SetHeader("To", strings.Join(to, ",")) | ||
m.SetHeader("Subject", "Test Email") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里 Subject 可以使用 tpl.subject,body 可以使用 tpl.content
memsto/notify_channel_cache.go
Outdated
httpClient[lst[i].ID] = cli | ||
case "smtp": | ||
ch := make(chan *models.EmailContext) | ||
ncc.startEmailSender(lst[i].ID, lst[i].SMTPRequestConfig, ch) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里会卡住吧?方法里是一个死循环
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
另外,这个可能存在 gorutine 泄露,如果 lst[i].ID 被删除之后,这里的 gorutine 会一直存在
models/notify_channel.go
Outdated
// 设置 TLS 配置 | ||
tlsConfig := &tls.Config{ | ||
//InsecureSkipVerify: nc.HTTPRequestConfig.TLS != nil && nc.HTTPRequestConfig.TLS.SkipVerify, | ||
InsecureSkipVerify: true, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里没有使用配置?
alert/dispatch/dispatch.go
Outdated
} | ||
|
||
if notifyChannel.ParamConfig.BatchSend { | ||
val = append(val, strings.Join(origin, ",")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里直接使用逗号把联系方式拼接起来,后面灵活度不太够,可能有接口参数要求是以 ; 分割,或者是字符串数组 phone=["123...","334..."]
通知规则改造