Skip to content
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

Open
wants to merge 5 commits into
base: notify-rule-feat
Choose a base branch
from

Conversation

Reditiny
Copy link
Contributor

通知规则改造

@710leo 710leo changed the title notify rule new notify process Feb 12, 2025

func MessageTemplateGetsAll(ctx *ctx.Context) ([]*MessageTemplate, error) {
if !ctx.IsCenter {
templates, err := poster.GetByUrls[[]*MessageTemplate](ctx, "/v1/n9e/message-templates")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个 api 看在 router 里没加上

"encoding/base64"
"encoding/json"
"fmt"
"github.com/ccfos/nightingale/v6/pkg/ctx"
Copy link
Member

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")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

实现下 /v1/n9e/notify-channels 接口

func GetSMTPClient(nc *NotifyChannelConfig) (*smtp.Client, error) {
// 连接到 SMTP 服务器
addr := fmt.Sprintf("%s:%d", nc.SMTPRequestConfig.Host, nc.SMTPRequestConfig.Port)
client, err := smtp.Dial(addr)
Copy link
Member

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不确定是否会踩坑


switch notifyChannel.RequestType {
case "http":
notifyChannel.SendHTTP(content, params, e.notifyChannelCache.GetHttpClient(notifyChannel.ID))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里需要把返回的 err 信息打印出来,方便排查问题

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里需要把 events 也传进去,方便在 http 参数和 body 中使用,更灵活一些

userGroups := e.userGroupCache.GetByUserGroupIds(p.UserGroupIDs)
var origin []string
var val []string
if notifyChannel.ParamConfig.UserInfo.ContactKey == "phone" {
Copy link
Member

@710leo 710leo Feb 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里缺少了对用户的其他联系方式的处理,比如公司内部 im,个人相关的 token


// 将 MessageTemplate 与变量配置的信息渲染进 reqBody
body, err := ncc.parseRequestBody(content, param)
if err != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里 err 需要打印日志,方便排查问题

return body.Bytes(), err
}

func (ncc *NotifyChannelConfig) replaceVariables(param map[string]string) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里用 go 模板渲染的方式处理更灵活一些

for i := range params {
param := params[i]
var to []string
if ncc.ParamConfig.BatchSend {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

邮件发送,直接批量发送就可以了,不需要使用这个参数区分了


m.SetHeader("From", ncc.SMTPRequestConfig.From)
m.SetHeader("To", strings.Join(to, ","))
m.SetHeader("Subject", "Test Email")
Copy link
Member

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

httpClient[lst[i].ID] = cli
case "smtp":
ch := make(chan *models.EmailContext)
ncc.startEmailSender(lst[i].ID, lst[i].SMTPRequestConfig, ch)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里会卡住吧?方法里是一个死循环

Copy link
Member

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 会一直存在

// 设置 TLS 配置
tlsConfig := &tls.Config{
//InsecureSkipVerify: nc.HTTPRequestConfig.TLS != nil && nc.HTTPRequestConfig.TLS.SkipVerify,
InsecureSkipVerify: true,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里没有使用配置?

}

if notifyChannel.ParamConfig.BatchSend {
val = append(val, strings.Join(origin, ","))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里直接使用逗号把联系方式拼接起来,后面灵活度不太够,可能有接口参数要求是以 ; 分割,或者是字符串数组 phone=["123...","334..."]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants