Skip to content

Commit

Permalink
add meta_filter & meta_prefix_filter functions to template
Browse files Browse the repository at this point in the history
  • Loading branch information
mylxsw committed Jul 15, 2020
1 parent 3466e66 commit 60dd6b7
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 25 deletions.
2 changes: 2 additions & 0 deletions dashboard/src/components/TemplateHelp.vue
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ Group: {
<li><code>string_mask(content string, left int) string</code> 在左右两侧只保留 left 个字符,中间所有字符替换为 * </li>
<li><code>string_tags(tags string, sep string) []string</code> 将字符串 tags 用 sep 作为分隔符,切割成多个 tag,空的 tag 会被排除 </li>
<li><code>remove_empty_line(content string) string</code> 移除字符串中的空行</li>
<li><code>meta_filter(meta map[string]interface{}, allowKeys ...string) map[string]interface{}</code> 过滤Meta,只保留允许的Key</li>
<li><code>meta_prefix_filter(meta map[string]interface{}, allowPrefix ...string) map[string]interface{}</code> 过滤Meta,只保留包含指定 prefix 的Key</li>
</ul>
</li>
</ul>
Expand Down
2 changes: 2 additions & 0 deletions dashboard/src/views/RuleEdit.vue
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,8 @@
sources.push({text: 'string_mask STR LEFT', displayText: 'string_mask(content string, left int) string | 在左右两侧只保留 left 个字符,中间所有字符替换为 *'})
sources.push({text: 'string_tags TAG_STR SEPARATOR', displayText: 'string_tags(tags string, sep string) []string | 将字符串 tags 用 sep 作为分隔符,切割成多个 tag,空的 tag 会被排除'})
sources.push({text: 'remove_empty_line STR', displayText: 'remove_empty_line(content string) string | 移除字符串中的空行'})
sources.push({text: 'meta_filter STR FILTER_STR', displayText: 'meta_filter(meta map[string]interface{}, allowKeys ...string) map[string]interface{} | 过滤Meta,只保留允许的Key'})
sources.push({text: 'meta_prefix_filter STR FILTER_PREFIX', displayText: 'meta_prefix_filter(meta map[string]interface{}, allowPrefix ...string) map[string]interface{} | 过滤Meta,只保留包含指定 prefix 的Key'})
}
var cur = editor.getCursor();
Expand Down
44 changes: 44 additions & 0 deletions pkg/array/array.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package array

import "strings"

// StringUnique remove duplicate elements from array
func StringUnique(input []string) []string {
u := make([]string, 0, len(input))
Expand All @@ -14,3 +16,45 @@ func StringUnique(input []string) []string {

return u
}

// StringsContain 判断元素是否在字符串数组中
func StringsContain(val string, items []string) bool {
for _, item := range items {
if item == val {
return true
}
}

return false
}

// StringContainPrefix 判断字符串是否以指定的前缀开始
func StringsContainPrefix(val string, prefixs []string) bool {
for _, prefix := range prefixs {
if strings.HasPrefix(val, prefix) {
return true
}
}

return false
}

// StringsFilter 字符串数组过滤
func StringsFilter(items []string, filter func(item string) bool) []string {
res := make([]string, 0)
for _, item := range items {
item = strings.TrimSpace(item)
if filter(item) {
res = append(res, item)
}
}

return res
}

// StringsRemoveEmpty 过滤掉字符串数组中的空元素
func StringsRemoveEmpty(items []string) []string {
return StringsFilter(items, func(item string) bool {
return item != ""
})
}
6 changes: 6 additions & 0 deletions pkg/array/array_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,9 @@ func TestStringUnique(t *testing.T) {
assert.EqualValues(t, 4, len(array.StringUnique(arr)))
}


func TestStringsContainPrefix(t *testing.T) {
s1 := "Hello, world"
assert.True(t, array.StringsContainPrefix(s1, []string{"xxxx", "yyyy", "Hell"}))
assert.False(t, array.StringsContainPrefix(s1, []string{"xxxx", "yyyy", "oops"}))
}
75 changes: 51 additions & 24 deletions pkg/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"text/template"
"time"

"github.com/mylxsw/adanos-alert/pkg/array"
pkgJSON "github.com/mylxsw/adanos-alert/pkg/json"
"github.com/mylxsw/asteria/log"
"github.com/mylxsw/coll"
Expand All @@ -30,30 +31,32 @@ func Parse(templateStr string, data interface{}) (string, error) {
// CreateParse create a template parser
func CreateParser(templateStr string) (*template.Template, error) {
funcMap := template.FuncMap{
"cutoff": cutOff,
"implode": strings.Join,
"explode": strings.Split,
"ident": leftIdent,
"json": jsonFormatter,
"datetime": datetimeFormat,
"datetime_noloc": datetimeFormatNoLoc,
"json_get": pkgJSON.Get,
"json_gets": pkgJSON.Gets,
"json_array": pkgJSON.GetArray,
"json_flatten": jsonFlatten,
"starts_with": startsWith,
"ends_with": endsWith,
"trim": strings.Trim,
"trim_right": strings.TrimRight,
"trim_left": strings.TrimLeft,
"trim_space": strings.TrimSpace,
"format": fmt.Sprintf,
"integer": toInteger,
"mysql_slowlog": parseMySQLSlowlog,
"open_falcon_im": ParseOpenFalconImMessage,
"string_mask": StringMask,
"string_tags": StringTags,
"remove_empty_line": RemoveEmptyLine,
"cutoff": cutOff,
"implode": strings.Join,
"explode": strings.Split,
"ident": leftIdent,
"json": jsonFormatter,
"datetime": datetimeFormat,
"datetime_noloc": datetimeFormatNoLoc,
"json_get": pkgJSON.Get,
"json_gets": pkgJSON.Gets,
"json_array": pkgJSON.GetArray,
"json_flatten": jsonFlatten,
"starts_with": startsWith,
"ends_with": endsWith,
"trim": strings.Trim,
"trim_right": strings.TrimRight,
"trim_left": strings.TrimLeft,
"trim_space": strings.TrimSpace,
"format": fmt.Sprintf,
"integer": toInteger,
"mysql_slowlog": parseMySQLSlowlog,
"open_falcon_im": ParseOpenFalconImMessage,
"string_mask": StringMask,
"string_tags": StringTags,
"remove_empty_line": RemoveEmptyLine,
"meta_filter": MetaFilter,
"meta_prefix_filter": MetaFilterPrefix,
}

return template.New("").Funcs(funcMap).Parse(templateStr)
Expand Down Expand Up @@ -228,3 +231,27 @@ func RemoveEmptyLine(content string) string {
"\n",
)
}

// MetaFilter 过滤 Meta,只保留允许的key
func MetaFilter(meta map[string]interface{}, allowKeys ...string) map[string]interface{} {
res := make(map[string]interface{})
for k, v := range meta {
if array.StringsContain(k, allowKeys) {
res[k] = v
}
}

return res
}

// MetaFilter 过滤 Meta,只保留以 allowKeyPrefix 开头的项
func MetaFilterPrefix(meta map[string]interface{}, allowKeyPrefix ...string) map[string]interface{} {
res := make(map[string]interface{})
for k, v := range meta {
if array.StringsContainPrefix(k, allowKeyPrefix) {
res[k] = v
}
}

return res
}
21 changes: 20 additions & 1 deletion pkg/template/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

pkgJSON "github.com/mylxsw/adanos-alert/pkg/json"
"github.com/mylxsw/go-toolkit/file"
"github.com/stretchr/testify/assert"
)

var content = `
Expand Down Expand Up @@ -248,4 +249,22 @@ What are you doing?`

result := RemoveEmptyLine(original)
fmt.Println(result)
}
}

func TestMetaFilter(t *testing.T) {
meta := make(map[string]interface{})
meta["message.k1"] = "v1"
meta["message.k2"] = "v2"
meta["message.k3"] = "v3"
meta["message.k4"] = "v4"
meta["message"] = "Hello, world"
meta["version"] = "1.0"

var temp = `{{ range $i, $msg := meta_prefix_filter .Meta "message." "version" }}[{{ $i }}: {{ $msg }}]{{ end }}`

res, err := Parse(temp, map[string]interface{}{
"Meta": meta,
})
assert.NoError(t, err)
assert.Equal(t, "[message.k1: v1][message.k2: v2][message.k3: v3][message.k4: v4][version: 1.0]", res)
}

0 comments on commit 60dd6b7

Please sign in to comment.