Skip to content

Commit

Permalink
[feat]: 集成优化功能
Browse files Browse the repository at this point in the history
  • Loading branch information
Cairry committed Jan 4, 2024
1 parent 66d774a commit ddcd6a4
Show file tree
Hide file tree
Showing 38 changed files with 212 additions and 154 deletions.
4 changes: 2 additions & 2 deletions controllers/api/alert_noticeObjcet.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package api

import (
"github.com/gin-gonic/gin"
"prometheus-manager/controllers/dao"
"prometheus-manager/utils/feishu"
"watchAlert/controllers/dao"
"watchAlert/utils/feishu"
)

type AlertNoticeObjectController struct{}
Expand Down
10 changes: 4 additions & 6 deletions controllers/api/alert_silence.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,19 @@ import (
"io/ioutil"
"log"
"net/http"
"prometheus-manager/controllers/dto"
"watchAlert/controllers/dto"
)

type AlertSilenceController struct{}

func (asc *AlertSilenceController) CreateSilence(ctx *gin.Context) {

var challengeInfo map[string]interface{}
var cardInfo dto.CardInfo

uuid := ctx.Query("uuid")
body := ctx.Request.Body
bodyByte, _ := ioutil.ReadAll(body)
_ = json.Unmarshal(bodyByte, &challengeInfo)
_ = ctx.ShouldBindJSON(&cardInfo)

err := alertSilenceService.CreateAlertSilence(uuid, challengeInfo)
err := alertSilenceService.CreateAlertSilence(uuid, cardInfo)
if err != nil {
ctx.JSON(200, gin.H{
"code": 2001,
Expand Down
2 changes: 1 addition & 1 deletion controllers/api/duty_manage.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package api

import (
"github.com/gin-gonic/gin"
"prometheus-manager/controllers/dao"
"watchAlert/controllers/dao"
)

type DutyManageController struct{}
Expand Down
2 changes: 1 addition & 1 deletion controllers/api/duty_people.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package api

import (
"github.com/gin-gonic/gin"
"prometheus-manager/controllers/dao"
"watchAlert/controllers/dao"
)

type DutyPeopleController struct{}
Expand Down
2 changes: 1 addition & 1 deletion controllers/api/duty_schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package api

import (
"github.com/gin-gonic/gin"
"prometheus-manager/controllers/dao"
"strconv"
"watchAlert/controllers/dao"
)

type DutyScheduleController struct{}
Expand Down
2 changes: 1 addition & 1 deletion controllers/api/entry.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package api

import "prometheus-manager/controllers/services"
import "watchAlert/controllers/services"

type ApiGroup struct {
AlertNoticeObjectController
Expand Down
4 changes: 2 additions & 2 deletions controllers/api/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"github.com/gin-gonic/gin"
"io/ioutil"
"log"
"prometheus-manager/globals"
"prometheus-manager/utils/http"
"watchAlert/globals"
"watchAlert/utils/http"
)

type EventController struct{}
Expand Down
2 changes: 1 addition & 1 deletion controllers/api/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package api

import (
"github.com/gin-gonic/gin"
"prometheus-manager/utils/http"
"watchAlert/utils/http"
)

type RuleController struct{}
Expand Down
20 changes: 18 additions & 2 deletions controllers/dto/feishu.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,24 @@ type CardInfo struct {
}

type CardInfoAction struct {
Value interface{} `json:"value"`
Tag string `json:"tag"`
Value SilenceValue `json:"value"`
Tag string `json:"tag"`
}

type SilenceValue struct {
Comment string `json:"comment"`
CreatedBy string `json:"createdBy"`
EndsAt string `json:"endsAt"`
Id string `json:"id"`
Matchers []MatchersLabels `json:"matchers"`
StartsAt string `json:"startsAt"`
}

type MatchersLabels struct {
IsEqual bool `json:"isEqual"`
IsRegex bool `json:"isRegex"`
Name string `json:"name"`
Value string `json:"value"`
}

// FeiShuUserInfo 飞书用户信息
Expand Down
5 changes: 3 additions & 2 deletions controllers/dto/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import "time"

// Alerts 接收Prometheus推送的告警
type Alerts struct {
AlertList []AlertInfo `json:"alerts"`
Receiver string `json:"receiver"`
AlertList []AlertInfo `json:"alerts"`
Receiver string `json:"receiver"`
Aggregated string `json:"aggregated"`
}

type AlertInfo struct {
Expand Down
4 changes: 2 additions & 2 deletions controllers/repo/alert_notice.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package repo

import (
"prometheus-manager/controllers/dao"
"prometheus-manager/globals"
"watchAlert/controllers/dao"
"watchAlert/globals"
)

type AlertNoticeRepo struct{}
Expand Down
4 changes: 2 additions & 2 deletions controllers/repo/duty_schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package repo

import (
"fmt"
"prometheus-manager/controllers/dao"
"prometheus-manager/globals"
"watchAlert/controllers/dao"
"watchAlert/globals"
)

type DutyScheduleRepo struct{}
Expand Down
10 changes: 5 additions & 5 deletions controllers/services/alert_noticeObject.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package services

import (
"bytes"
"prometheus-manager/controllers/dao"
"prometheus-manager/globals"
"prometheus-manager/utils/cmd"
"prometheus-manager/utils/feishu"
"prometheus-manager/utils/http"
"watchAlert/controllers/dao"
"watchAlert/globals"
"watchAlert/utils/cmd"
"watchAlert/utils/feishu"
"watchAlert/utils/http"
)

type AlertNoticeService struct{}
Expand Down
63 changes: 26 additions & 37 deletions controllers/services/alert_silence.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,70 +6,59 @@ import (
"fmt"
"io/ioutil"
"log"
dto2 "prometheus-manager/controllers/dto"
"prometheus-manager/globals"
"prometheus-manager/utils/cache"
"prometheus-manager/utils/http"
"strings"
"watchAlert/controllers/dto"
"watchAlert/globals"
"watchAlert/utils/cache"
"watchAlert/utils/http"
)

type AlertSilenceService struct{}

type InterAlertSilenceService interface {
CreateAlertSilence(uuid string, challengeInfo map[string]interface{}) error
CreateAlertSilence(uuid string, cardInfo dto.CardInfo) error
}

func NewInterAlertSilenceService() InterAlertSilenceService {
return &AlertSilenceService{}
}

func (ass *AlertSilenceService) CreateAlertSilence(uuid string, challengeInfo map[string]interface{}) error {
func (ass *AlertSilenceService) CreateAlertSilence(uuid string, cardInfo dto.CardInfo) error {

var (
cardInfo dto2.CardInfo
cacheAlertInfo dto.AlertInfo
AlertInfoList []dto.AlertInfo
)

rawDataJson, _ := json.Marshal(challengeInfo)
_ = json.Unmarshal(rawDataJson, &cardInfo)

// To Json
kLabel := cardInfo.Action.Value.(map[string]interface{})
// 获取静默请求参数
kLabel := cardInfo.Action.Value
silencesValueJson, _ := json.Marshal(kLabel)
bodyReader := bytes.NewReader(silencesValueJson)

fingerprintID := kLabel["comment"]
labelData := getAlertSilencesFingerprintID(fingerprintID.(string))
if labelData == fingerprintID {
globals.Logger.Sugar().Info("报警静默已存在, 无需重新创建, 报警ID ->", labelData)
return fmt.Errorf("报警静默已存在, 无需重新创建")
}
// 检查是否已创建
getAlertSilencesFingerprintID(kLabel.Comment)

// 发起静默请求
_, err := http.Post(globals.Config.AlertManager.URL+"/api/v2/silences", bodyReader)
if err != nil {
globals.Logger.Sugar().Error("创建报警静默失败 ->", string(silencesValueJson))
return fmt.Errorf("创建报警静默失败")
}
globals.Logger.Sugar().Info("创建报警静默成功 ->", string(silencesValueJson))

var (
promAlertManager = make(map[string]interface{})
cacheAlertInfo interface{}
AlertInfo []interface{}
)

cacheValue := globals.CacheCli.Get(fingerprintID.(string))
// 将告警状态转换为静默状态
cacheValue := globals.CacheCli.Get(kLabel.Comment)
cacheAlertJson, _ := json.Marshal(cacheValue.(cache.CacheItem).Values)
_ = json.Unmarshal(cacheAlertJson, &cacheAlertInfo)
AlertInfo = append(AlertInfo, cacheAlertInfo)

// 将告警状态转换为静默状态
promAlertManager["alert"] = AlertInfo
promAlertManager["alert"].([]interface{})[0].(map[string]interface{})["status"] = "silence"
prometheusAlertBody, _ := json.Marshal(promAlertManager)
cacheAlertInfo.Status = "silence"
AlertInfoList = append(AlertInfoList, cacheAlertInfo)
alerts := dto.Alerts{
AlertList: AlertInfoList,
}
body, _ := json.Marshal(alerts)

// 发送消息卡片
actionUserID := challengeInfo["user_id"].(string)
err = NewInterEventService().PushAlertToWebhook(actionUserID, prometheusAlertBody, uuid)
err = NewInterEventService().PushAlertToWebhook(cardInfo.UserID, body, uuid)
if err != nil {
log.Println("消息卡片发送失败", err)
return err
Expand All @@ -79,7 +68,7 @@ func (ass *AlertSilenceService) CreateAlertSilence(uuid string, challengeInfo ma

}

func getAlertSilencesFingerprintID(fingerprintID string) (labelData interface{}) {
func getAlertSilencesFingerprintID(fingerprintID string) {

resp, err := http.Get(globals.Config.AlertManager.URL + "/api/v2/silences")
if err != nil {
Expand All @@ -90,7 +79,7 @@ func getAlertSilencesFingerprintID(fingerprintID string) (labelData interface{})
content, err := ioutil.ReadAll(resp.Body)

var (
res []dto2.SearchAlertManager
res []dto.SearchAlertManager
activeLabelList []interface{}
)
err = json.Unmarshal(content, &res)
Expand All @@ -105,9 +94,9 @@ func getAlertSilencesFingerprintID(fingerprintID string) (labelData interface{})
for _, v := range activeLabelList {
activeV := strings.Replace(v.(string), "\"", "", -1)
if activeV == fingerprintID {
return fingerprintID
globals.Logger.Sugar().Info("报警静默已存在, 无需重新创建, 报警ID ->", fingerprintID)
return
}
}

return nil
}
6 changes: 3 additions & 3 deletions controllers/services/duty_manage.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package services

import (
"prometheus-manager/controllers/dao"
"prometheus-manager/globals"
"prometheus-manager/utils/cmd"
"watchAlert/controllers/dao"
"watchAlert/globals"
"watchAlert/utils/cmd"
)

type DutyManageService struct{}
Expand Down
6 changes: 3 additions & 3 deletions controllers/services/duty_people.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package services

import (
"prometheus-manager/controllers/dao"
"prometheus-manager/globals"
"prometheus-manager/utils/cmd"
"watchAlert/controllers/dao"
"watchAlert/globals"
"watchAlert/utils/cmd"
)

type DutyPeopleService struct{}
Expand Down
4 changes: 2 additions & 2 deletions controllers/services/duty_peopleGroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package services

import (
"log"
"prometheus-manager/controllers/dao"
"prometheus-manager/globals"
"watchAlert/controllers/dao"
"watchAlert/globals"
)

type DutyPeopleGroupService struct{}
Expand Down
4 changes: 2 additions & 2 deletions controllers/services/duty_schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package services
import (
"fmt"
"log"
"prometheus-manager/controllers/dao"
"prometheus-manager/globals"
"watchAlert/controllers/dao"
"watchAlert/globals"
"strconv"
"time"
)
Expand Down
35 changes: 31 additions & 4 deletions controllers/services/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package services

import (
"encoding/json"
"prometheus-manager/controllers/dao"
"prometheus-manager/globals"
"prometheus-manager/utils/renderTemplates/aliyun"
prom "prometheus-manager/utils/renderTemplates/prometheus"
"fmt"
"strconv"
"time"
"watchAlert/controllers/dao"
"watchAlert/controllers/dto"
"watchAlert/globals"
"watchAlert/utils/renderTemplates/aliyun"
prom "watchAlert/utils/renderTemplates/prometheus"
)

type EventService struct{}
Expand All @@ -30,6 +32,7 @@ func (es *EventService) PushAlertToWebhook(actionUserID string, body []byte, uui
switch alertNoticeObject.DataSource {
case "Prometheus":

body = alertAggregation(body)
err := prometheus(alertNoticeObject, actionUserID, body, dutyUser)
if err != nil {
return err
Expand Down Expand Up @@ -86,3 +89,27 @@ func aLiYun(alertNotice dao.AlertNotice, body []byte, dutyUser string) error {
return nil

}

func alertAggregation(body []byte) []byte {

var (
alerts dto.Alerts
alertName string
filteredAlertInfo []dto.AlertInfo
countIdenticalAlert = make(map[string]int)
)
_ = json.Unmarshal(body, &alerts)
for k, v := range alerts.AlertList {
alertName = v.Labels["alertname"]
countIdenticalAlert[alertName] = k + 1
}
if countIdenticalAlert[alertName] > 1 {
filteredAlertInfo = append(filteredAlertInfo, alerts.AlertList[0])
alerts.AlertList = filteredAlertInfo
alerts.Aggregated = fmt.Sprintf("\n聚合 %d 条告警, 详情查看告警链接 ⬇️", countIdenticalAlert[alertName])
}

jsonData, _ := json.Marshal(alerts)
return jsonData

}
Loading

0 comments on commit ddcd6a4

Please sign in to comment.