From c3081741bed43909eea7bfb35106ae6b56b86363 Mon Sep 17 00:00:00 2001 From: SuperLandy <44797172+SuperLandy@users.noreply.github.com> Date: Thu, 25 Dec 2025 18:30:52 +0800 Subject: [PATCH] =?UTF-8?q?Title=E5=8A=A8=E6=80=81=E5=8F=96=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controllers/prometheusalert.go | 56 ++++++++++++++++++++++++++++------ 1 file changed, 46 insertions(+), 10 deletions(-) diff --git a/controllers/prometheusalert.go b/controllers/prometheusalert.go index c4de9ebe..110ecdcb 100644 --- a/controllers/prometheusalert.go +++ b/controllers/prometheusalert.go @@ -79,6 +79,19 @@ type PrometheusAlertMsg struct { } func (c *PrometheusAlertController) PrometheusAlert() { + Title := beego.AppConfig.String("title") + // 优先使用请求参数中的 alertname 覆盖配置;若无,再尝试从 JSON body 的顶层 title 覆盖 + if t := c.Input().Get("alertname"); t != "" { + Title = t + } else { + var bodyMap map[string]interface{} + if err := json.Unmarshal(c.Ctx.Input.RequestBody, &bodyMap); err == nil { + if v, ok := bodyMap["title"].(string); ok && v != "" { + Title = v + } + } + } + logsign := "[" + LogsSign() + "]" var p_json interface{} //针对prometheus的消息特殊处理 @@ -110,6 +123,19 @@ func (c *PrometheusAlertController) PrometheusAlert() { json.Unmarshal(c.Ctx.Input.RequestBody, &p_json) //针对prometheus的消息特殊处理 json.Unmarshal(c.Ctx.Input.RequestBody, &p_alertmanager_json) + + // (优先级: query/form 顶层的.title > alerts[0].labels.title > 配置 title) + if Title == beego.AppConfig.String("title") { + if alertsArr, ok := p_alertmanager_json["alerts"].([]interface{}); ok && len(alertsArr) > 0 { + if firstAlert, ok := alertsArr[0].(map[string]interface{}); ok { + if labels, ok := firstAlert["labels"].(map[string]interface{}); ok { + if an, ok := labels["title"].(string); ok && an != "" { + Title = an + } + } + } + } + } } // alertgroup @@ -187,14 +213,19 @@ func (c *PrometheusAlertController) PrometheusAlert() { //logs.Debug("当前模版:", Return_pMsg.TplName) //获取渲染后的模版 err, msg := TransformAlertMessage(p_alertmanager_json, Return_pMsg.Tpl) + title_err, title_msg := TransformAlertMessage(p_json, Title) if err != nil { //失败不发送消息 logs.Error(logsign, err.Error()) message = err.Error() } else { - //发送消息 - message = SendMessagePrometheusAlert(msg, &Return_pMsg, logsign) + if title_err != nil { + logs.Error(logsign, title_err.Error()) + message = SendMessagePrometheusAlert(Title, msg, &Return_pMsg, logsign) + } else { + message = SendMessagePrometheusAlert(title_msg, msg, &Return_pMsg, logsign) + } } } @@ -203,13 +234,19 @@ func (c *PrometheusAlertController) PrometheusAlert() { } else { //获取渲染后的模版 err, msg := TransformAlertMessage(p_json, PrometheusAlertTpl.Tpl) + title_err, title_msg := TransformAlertMessage(p_json, Title) if err != nil { logs.Error(logsign, err.Error()) message = err.Error() } else { //发送消息 - message = SendMessagePrometheusAlert(msg, &pMsg, logsign) + if title_err != nil { + logs.Error(logsign, title_err.Error()) + message = SendMessagePrometheusAlert(Title, msg, &pMsg, logsign) + } else { + message = SendMessagePrometheusAlert(title_msg, msg, &pMsg, logsign) + } } } @@ -443,8 +480,7 @@ func TransformAlertMessage(p_json interface{}, tpltext string) (error error, msg } // 发送消息 -func SendMessagePrometheusAlert(message string, pmsg *PrometheusAlertMsg, logsign string) string { - Title := beego.AppConfig.String("title") +func SendMessagePrometheusAlert(title, message string, pmsg *PrometheusAlertMsg, logsign string) string { var ReturnMsg string models.AlertsFromCounter.WithLabelValues("/prometheusalert").Add(1) ChartsJson.Prometheusalert += 1 @@ -464,10 +500,10 @@ func SendMessagePrometheusAlert(message string, pmsg *PrometheusAlertMsg, logsig case "dd": Ddurl := strings.Split(pmsg.Ddurl, ",") if pmsg.RoundRobin == "true" { - ReturnMsg += PostToDingDing(Title, message, DoBalance(Ddurl), pmsg.AtSomeOne, logsign) + ReturnMsg += PostToDingDing(title, message, DoBalance(Ddurl), pmsg.AtSomeOne, logsign) } else { for _, url := range Ddurl { - ReturnMsg += PostToDingDing(Title, message, url, pmsg.AtSomeOne, logsign) + ReturnMsg += PostToDingDing(title, message, url, pmsg.AtSomeOne, logsign) } } @@ -475,10 +511,10 @@ func SendMessagePrometheusAlert(message string, pmsg *PrometheusAlertMsg, logsig case "fs": Fsurl := strings.Split(pmsg.Fsurl, ",") if pmsg.RoundRobin == "true" { - ReturnMsg += PostToFS(Title, message, DoBalance(Fsurl), pmsg.AtSomeOne, logsign) + ReturnMsg += PostToFS(title, message, DoBalance(Fsurl), pmsg.AtSomeOne, logsign) } else { for _, url := range Fsurl { - ReturnMsg += PostToFS(Title, message, url, pmsg.AtSomeOne, logsign) + ReturnMsg += PostToFS(title, message, url, pmsg.AtSomeOne, logsign) } } @@ -540,7 +576,7 @@ func SendMessagePrometheusAlert(message string, pmsg *PrometheusAlertMsg, logsig ReturnMsg += SendVoice(message, logsign) //飞书APP渠道 case "fsapp": - ReturnMsg += PostToFeiShuApp(Title, message, pmsg.AtSomeOne, logsign) + ReturnMsg += PostToFeiShuApp(title, message, pmsg.AtSomeOne, logsign) //kafka渠道 case "kafka": ReturnMsg += SendKafka(message, logsign)