refactor: log regex check api
This commit is contained in:
parent
fd9d78061b
commit
e0cc7dbffa
|
@ -146,7 +146,7 @@ func configRoutes(r *gin.Engine) {
|
||||||
pages.POST("/collect-rules", login(), collectRuleAdd)
|
pages.POST("/collect-rules", login(), collectRuleAdd)
|
||||||
pages.DELETE("/collect-rules", login(), collectRuleDel)
|
pages.DELETE("/collect-rules", login(), collectRuleDel)
|
||||||
pages.PUT("/collect-rule/:id", login(), collectRulePut)
|
pages.PUT("/collect-rule/:id", login(), collectRulePut)
|
||||||
pages.POST("/log/check", regExpCheck) // check collect rule
|
pages.POST("/log/check", regExpCheck)
|
||||||
|
|
||||||
pages.GET("/metric-descriptions", metricDescriptionGets)
|
pages.GET("/metric-descriptions", metricDescriptionGets)
|
||||||
pages.POST("/metric-descriptions", login(), metricDescriptionAdd)
|
pages.POST("/metric-descriptions", login(), metricDescriptionAdd)
|
||||||
|
|
|
@ -1,16 +1,15 @@
|
||||||
package http
|
package http
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/toolkits/pkg/logger"
|
|
||||||
|
|
||||||
"github.com/didi/nightingale/v5/cache"
|
"github.com/didi/nightingale/v5/cache"
|
||||||
"github.com/didi/nightingale/v5/models"
|
"github.com/didi/nightingale/v5/models"
|
||||||
|
"github.com/didi/nightingale/v5/pkg/i18n"
|
||||||
)
|
)
|
||||||
|
|
||||||
type collectRuleForm struct {
|
type collectRuleForm struct {
|
||||||
|
@ -136,8 +135,6 @@ type RegExpCheck struct {
|
||||||
Data []map[string]string `json:"tags"`
|
Data []map[string]string `json:"tags"`
|
||||||
}
|
}
|
||||||
|
|
||||||
var RegExpExcludePatition string = "```EXCLUDE```"
|
|
||||||
|
|
||||||
func regExpCheck(c *gin.Context) {
|
func regExpCheck(c *gin.Context) {
|
||||||
param := make(map[string]string)
|
param := make(map[string]string)
|
||||||
dangerous(c.ShouldBind(¶m))
|
dangerous(c.ShouldBind(¶m))
|
||||||
|
@ -147,75 +144,45 @@ func regExpCheck(c *gin.Context) {
|
||||||
Data: make([]map[string]string, 0),
|
Data: make([]map[string]string, 0),
|
||||||
}
|
}
|
||||||
|
|
||||||
// 处理时间格式
|
calcMethod := param["func"]
|
||||||
if t, ok := param["time"]; !ok || t == "" {
|
if calcMethod == "" {
|
||||||
tmp := map[string]string{"time": "time参数不存在或为空"}
|
tmp := map[string]string{"func": "is empty"}
|
||||||
ret.Data = append(ret.Data, tmp)
|
ret.Data = append(ret.Data, tmp)
|
||||||
} else {
|
renderData(c, ret, nil)
|
||||||
timePat, _ := GetPatAndTimeFormat(param["time"])
|
return
|
||||||
if timePat == "" {
|
|
||||||
tmp := map[string]string{"time": genErrMsg("时间格式")}
|
|
||||||
ret.Data = append(ret.Data, tmp)
|
|
||||||
} else {
|
|
||||||
suc, tRes, _ := checkRegPat(timePat, param["log"], true)
|
|
||||||
if !suc {
|
|
||||||
ret.Success = false
|
|
||||||
tRes = genErrMsg("时间格式")
|
|
||||||
}
|
|
||||||
tmp := map[string]string{"time": tRes}
|
|
||||||
ret.Data = append(ret.Data, tmp)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 计算方式
|
// 处理主正则
|
||||||
calc_method := param["calc_method"]
|
|
||||||
|
|
||||||
// 处理主正则(with exclude)
|
|
||||||
if re, ok := param["re"]; !ok || re == "" {
|
if re, ok := param["re"]; !ok || re == "" {
|
||||||
tmp := map[string]string{"re": "re参数不存在或为空"}
|
tmp := map[string]string{"re": "regex does not exist or is empty"}
|
||||||
ret.Data = append(ret.Data, tmp)
|
ret.Data = append(ret.Data, tmp)
|
||||||
} else {
|
renderData(c, ret, nil)
|
||||||
// 处理exclude的情况
|
return
|
||||||
exclude := ""
|
|
||||||
if strings.Contains(re, RegExpExcludePatition) {
|
|
||||||
l := strings.Split(re, RegExpExcludePatition)
|
|
||||||
if len(l) >= 2 {
|
|
||||||
param["re"] = l[0]
|
|
||||||
exclude = l[1]
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 匹配主正则
|
// 匹配主正则
|
||||||
suc, reRes, isSub := checkRegPat(param["re"], param["log"], false)
|
suc, reRes, isSub := checkRegex(param["re"], param["log"])
|
||||||
if !suc {
|
if !suc {
|
||||||
ret.Success = false
|
ret.Success = false
|
||||||
reRes = genErrMsg("主正则")
|
reRes = genErrMsg("re")
|
||||||
|
ret.Data = append(ret.Data, map[string]string{"re": reRes})
|
||||||
|
renderData(c, ret, nil)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
if calc_method != "" && calc_method != "cnt" && !isSub {
|
if calcMethod == "histogram" && !isSub {
|
||||||
ret.Success = false
|
ret.Success = false
|
||||||
reRes = genSubErrMsg("主正则")
|
reRes = genSubErrMsg("re")
|
||||||
}
|
ret.Data = append(ret.Data, map[string]string{"re": reRes})
|
||||||
tmp := map[string]string{"主正则": reRes}
|
renderData(c, ret, nil)
|
||||||
ret.Data = append(ret.Data, tmp)
|
return
|
||||||
|
|
||||||
// 匹配exclude, 这个不影响失败
|
|
||||||
if exclude != "" {
|
|
||||||
suc, exRes, _ := checkRegPat(exclude, param["log"], false)
|
|
||||||
if !suc {
|
|
||||||
//ret.Success = false
|
|
||||||
exRes = "未匹配到排除串,请检查是否符合预期"
|
|
||||||
}
|
|
||||||
tmp := map[string]string{"排除串": exRes}
|
|
||||||
ret.Data = append(ret.Data, tmp)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ret.Data = append(ret.Data, map[string]string{"re": reRes})
|
||||||
// 处理tags
|
// 处理tags
|
||||||
var nonTagKey = map[string]bool{
|
var nonTagKey = map[string]bool{
|
||||||
"re": true,
|
"re": true,
|
||||||
"log": true,
|
"log": true,
|
||||||
"time": true,
|
"func": true,
|
||||||
"calc_method": true,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for tagk, pat := range param {
|
for tagk, pat := range param {
|
||||||
|
@ -223,7 +190,7 @@ func regExpCheck(c *gin.Context) {
|
||||||
if _, ok := nonTagKey[tagk]; ok {
|
if _, ok := nonTagKey[tagk]; ok {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
suc, tagRes, isSub := checkRegPat(pat, param["log"], false)
|
suc, tagRes, isSub := checkRegex(pat, param["log"])
|
||||||
if !suc {
|
if !suc {
|
||||||
// 正则错误
|
// 正则错误
|
||||||
ret.Success = false
|
ret.Success = false
|
||||||
|
@ -245,49 +212,8 @@ func regExpCheck(c *gin.Context) {
|
||||||
renderData(c, ret, nil)
|
renderData(c, ret, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
//根据配置的时间格式,获取对应的正则匹配pattern和time包用的时间格式
|
|
||||||
func GetPatAndTimeFormat(tf string) (string, string) {
|
|
||||||
var pat, timeFormat string
|
|
||||||
switch tf {
|
|
||||||
case "dd/mmm/yyyy:HH:MM:SS":
|
|
||||||
pat = `([012][0-9]|3[01])/[JFMASOND][a-z]{2}/(2[0-9]{3}):([01][0-9]|2[0-4])(:[012345][0-9]){2}`
|
|
||||||
timeFormat = "02/Jan/2006:15:04:05"
|
|
||||||
case "dd/mmm/yyyy HH:MM:SS":
|
|
||||||
pat = `([012][0-9]|3[01])/[JFMASOND][a-z]{2}/(2[0-9]{3})\s([01][0-9]|2[0-4])(:[012345][0-9]){2}`
|
|
||||||
timeFormat = "02/Jan/2006 15:04:05"
|
|
||||||
case "yyyy-mm-ddTHH:MM:SS":
|
|
||||||
pat = `(2[0-9]{3})-(0[1-9]|1[012])-([012][0-9]|3[01])T([01][0-9]|2[0-4])(:[012345][0-9]){2}`
|
|
||||||
timeFormat = "2006-01-02T15:04:05"
|
|
||||||
case "dd-mmm-yyyy HH:MM:SS":
|
|
||||||
pat = `([012][0-9]|3[01])-[JFMASOND][a-z]{2}-(2[0-9]{3})\s([01][0-9]|2[0-4])(:[012345][0-9]){2}`
|
|
||||||
timeFormat = "02-Jan-2006 15:04:05"
|
|
||||||
case "yyyy-mm-dd HH:MM:SS":
|
|
||||||
pat = `(2[0-9]{3})-(0[1-9]|1[012])-([012][0-9]|3[01])\s([01][0-9]|2[0-4])(:[012345][0-9]){2}`
|
|
||||||
timeFormat = "2006-01-02 15:04:05"
|
|
||||||
case "yyyy/mm/dd HH:MM:SS":
|
|
||||||
pat = `(2[0-9]{3})/(0[1-9]|1[012])/([012][0-9]|3[01])\s([01][0-9]|2[0-4])(:[012345][0-9]){2}`
|
|
||||||
timeFormat = "2006/01/02 15:04:05"
|
|
||||||
case "yyyymmdd HH:MM:SS":
|
|
||||||
pat = `(2[0-9]{3})(0[1-9]|1[012])([012][0-9]|3[01])\s([01][0-9]|2[0-4])(:[012345][0-9]){2}`
|
|
||||||
timeFormat = "20060102 15:04:05"
|
|
||||||
case "mmm dd HH:MM:SS":
|
|
||||||
pat = `[JFMASOND][a-z]{2}\s+([1-9]|[1-2][0-9]|3[01])\s([01][0-9]|2[0-4])(:[012345][0-9]){2}`
|
|
||||||
timeFormat = "Jan 2 15:04:05"
|
|
||||||
case "mmdd HH:MM:SS":
|
|
||||||
pat = `(0[1-9]|1[012])([012][0-9]|3[01])\s([01][0-9]|2[0-4])(:[012345][0-9]){2}`
|
|
||||||
timeFormat = "0102 15:04:05"
|
|
||||||
case "dd mmm yyyy HH:MM:SS":
|
|
||||||
pat = `([012][0-9]|3[01])\s+[JFMASOND][a-z]{2}\s+(2[0-9]{3})\s([01][0-9]|2[0-4])(:[012345][0-9]){2}`
|
|
||||||
timeFormat = "02 Jan 2006 15:04:05"
|
|
||||||
default:
|
|
||||||
logger.Errorf("match time pac failed : [timeFormat:%s]", tf)
|
|
||||||
return "", ""
|
|
||||||
}
|
|
||||||
return pat, timeFormat
|
|
||||||
}
|
|
||||||
|
|
||||||
// 出错信息直接放在body里
|
// 出错信息直接放在body里
|
||||||
func checkRegPat(pat string, log string, origin bool) (succ bool, result string, isSub bool) {
|
func checkRegex(pat string, log string) (succ bool, result string, isSub bool) {
|
||||||
if pat == "" {
|
if pat == "" {
|
||||||
return false, "", false
|
return false, "", false
|
||||||
}
|
}
|
||||||
|
@ -307,15 +233,7 @@ func checkRegPat(pat string, log string, origin bool) (succ bool, result string,
|
||||||
return true, res[0], false
|
return true, res[0], false
|
||||||
// 查到了,默认取第一个串
|
// 查到了,默认取第一个串
|
||||||
default:
|
default:
|
||||||
var msg string
|
return true, res[1], true
|
||||||
if origin {
|
|
||||||
msg = res[0]
|
|
||||||
isSub = false
|
|
||||||
} else {
|
|
||||||
msg = res[1]
|
|
||||||
isSub = true
|
|
||||||
}
|
|
||||||
return true, msg, isSub
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -326,15 +244,15 @@ func includeIllegalChar(s string) bool {
|
||||||
|
|
||||||
// 生成返回错误信息
|
// 生成返回错误信息
|
||||||
func genErrMsg(sign string) string {
|
func genErrMsg(sign string) string {
|
||||||
return fmt.Sprintf("正则匹配失败,请认真检查您[%s]的配置", sign)
|
return i18n.Sprintf("regular match failed, please check your configuration:[%s]", sign)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 生成子串匹配错误信息
|
// 生成子串匹配错误信息
|
||||||
func genSubErrMsg(sign string) string {
|
func genSubErrMsg(sign string) string {
|
||||||
return fmt.Sprintf("正则匹配成功。但根据配置,并没有获取到()内的子串,请认真检查您[%s]的配置", sign)
|
return i18n.Sprintf("regular match was successful. according to the configuration, it does not get the substring inside(), please check your configuration:[%s]", sign)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 生成子串匹配错误信息
|
// 生成子串匹配错误信息
|
||||||
func genIllegalCharErrMsg() string {
|
func genIllegalCharErrMsg() string {
|
||||||
return `正则匹配成功。但是tag的key或者value包含非法字符:[:,/=\r\n\t], 请重新调整`
|
return i18n.Sprintf(`key or value of tag contains illegal characters:[:,/=\r\n\t]`)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue