Skip to content

Commit

Permalink
Optimize send alarm logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Cairry committed Oct 23, 2024
1 parent 7a0dede commit 09bad26
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 77 deletions.
12 changes: 7 additions & 5 deletions alert/consumer/consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,17 +275,19 @@ func (ec *Consume) handleAlert(rule models.AlertRule, alerts []models.AlertCurEv
TenantId: alert.TenantId,
Uuid: noticeId,
}

if !alert.IsRecovered {
alert.LastSendTime = curTime
ctx.Redis.Event().SetCache("Firing", alert, 0)
}

noticeData, _ := ec.ctx.DB.Notice().Get(r)
alert.DutyUser = process.GetDutyUser(ec.ctx, noticeData)
err := sender.Sender(ec.ctx, alert, noticeData)
if err != nil {
global.Logger.Sugar().Errorf(err.Error())
return
}

if !alert.IsRecovered {
alert.LastSendTime = curTime
ctx.Redis.Event().SetCache("Firing", alert, 0)
}
}

return
Expand Down
40 changes: 40 additions & 0 deletions alert/sender/dingding.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package sender

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"watchAlert/pkg/utils/http"
)

type DingResponseMsg struct {
Code int `json:"errcode"`
Msg string `json:"errmsg"`
}

func SendToDingDing(hook, msg string) error {
cardContentByte := bytes.NewReader([]byte(msg))
res, err := http.Post(nil, hook, cardContentByte)
if err != nil {
return err
}

// 读取响应体内容
body, err := io.ReadAll(res.Body)
if err != nil {
return errors.New(fmt.Sprintf("Error reading Dingding response body: %s", err.Error()))
}

var response DingResponseMsg
err = json.Unmarshal(body, &response)
if err != nil {
return errors.New(fmt.Sprintf("Error unmarshalling Dingding response: %s", err.Error()))
}
if response.Code != 0 {
return errors.New(response.Msg)
}

return nil
}
81 changes: 9 additions & 72 deletions alert/sender/entry.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,15 @@
package sender

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"watchAlert/alert/mute"
"watchAlert/internal/global"
"watchAlert/internal/models"
"watchAlert/pkg/ctx"
"watchAlert/pkg/utils/http"
"watchAlert/pkg/utils/templates"
)

type DingResponseMsg struct {
Code int `json:"errcode"`
Msg string `json:"errmsg"`
}

type FeishuResponseMsg struct {
Code int `json:"code"`
Msg string `json:"msg"`
Data string `json:"data"`
}

func Sender(ctx *ctx.Context, alert models.AlertCurEvent, notice models.AlertNotice) error {
ok := mute.IsMuted(ctx, &alert)
if ok {
Expand All @@ -37,70 +22,22 @@ func Sender(ctx *ctx.Context, alert models.AlertCurEvent, notice models.AlertNot
case "Email":
err := SendToEmail(alert, notice.Email.Subject, notice.Email.To, notice.Email.CC, n.CardContentMsg)
if err != nil {
return fmt.Errorf("邮件发送失败, err: %s", err.Error())
return fmt.Errorf("Send alarm failed to email , err: %s", err.Error())
}
case "FeiShu", "DingDing":
var msg string
cardContentByte := bytes.NewReader([]byte(n.CardContentMsg))
res, err := http.Post(nil, notice.Hook, cardContentByte)
case "FeiShu":
err := SendToFeiShu(notice.Hook, n.CardContentMsg)
if err != nil {
msg = err.Error()
} else {
if res.StatusCode != 200 {
all, err := io.ReadAll(res.Body)
if err != nil {
global.Logger.Sugar().Error(err.Error())
return err
}
msg = string(all)
}
return fmt.Errorf("Send alarm failed to FeiShu, err: %s", err.Error())
}

// 读取响应体内容
body, err := io.ReadAll(res.Body)
case "DingDing":
err := SendToDingDing(notice.Hook, n.CardContentMsg)
if err != nil {
global.Logger.Sugar().Errorf("Error reading response body: %v", err)
return err
}

if NoticeType == "FeiShu" {
var response FeishuResponseMsg
err = json.Unmarshal(body, &response)
if err != nil {
global.Logger.Sugar().Errorf("Error unmarshalling %v response: %v", NoticeType, err)
return err
}
if response.Code != 0 {
global.Logger.Sugar().Error(response.Msg)
return errors.New(response.Msg)
}
}

if NoticeType == "DingDing" {
var response DingResponseMsg
err = json.Unmarshal(body, &response)
if err != nil {
global.Logger.Sugar().Errorf("Error unmarshalling %v response: %v", NoticeType, err)
return err
}
if response.Code != 0 {
global.Logger.Sugar().Error(response.Msg)
return errors.New(response.Msg)
}
}

if res.StatusCode != 200 {
msg = string(body)
}

if msg != "" {
global.Logger.Sugar().Errorf("Hook 类型报警发送失败 data: %s", n.CardContentMsg)
return errors.New(msg)
return fmt.Errorf("Send alarm failed to DingDing, err: %s", err.Error())
}
default:
return errors.New("无效的通知类型: " + notice.NoticeType)
return errors.New(fmt.Sprintf("Send alarm failed, exist 无效的通知类型: %s, NoticeId: %s, NoticeName: %s", notice.NoticeType, notice.Uuid, notice.Name))
}

global.Logger.Sugar().Info("报警发送成功: ", n.CardContentMsg)
global.Logger.Sugar().Info("Send alarm ok, msg: ", n.CardContentMsg)
return nil
}
40 changes: 40 additions & 0 deletions alert/sender/feishu.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package sender

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"watchAlert/pkg/utils/http"
)

type FeiShuResponseMsg struct {
Code int `json:"code"`
Msg string `json:"msg"`
}

func SendToFeiShu(hook, msg string) error {
cardContentByte := bytes.NewReader([]byte(msg))
res, err := http.Post(nil, hook, cardContentByte)
if err != nil {
return err
}

// 读取响应体内容
body, err := io.ReadAll(res.Body)
if err != nil {
return errors.New(fmt.Sprintf("Error reading Feishu response body: %s, msg: %s", string(body), err.Error()))
}

var response FeiShuResponseMsg
err = json.Unmarshal(body, &response)
if err != nil {
return errors.New(fmt.Sprintf("Error unmarshalling Feishu response: %s", err.Error()))
}
if response.Code != 0 {
return errors.New(response.Msg)
}

return nil
}

0 comments on commit 09bad26

Please sign in to comment.