-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
增加分组聚合支持,增加 Meta 字段排序函数,修复时间范围报警频率bug
- Loading branch information
Showing
18 changed files
with
1,695 additions
and
1,384 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package matcher | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/antonmedv/expr" | ||
"github.com/antonmedv/expr/vm" | ||
"github.com/mylxsw/adanos-alert/internal/repository" | ||
) | ||
|
||
type MessageFinger struct { | ||
expr string | ||
program *vm.Program | ||
} | ||
|
||
func NewMessageFinger(fingerExpr string) (*MessageFinger, error) { | ||
if fingerExpr == "" { | ||
fingerExpr = `""` | ||
} | ||
|
||
program, err := expr.Compile(fingerExpr, expr.Env(&MessageWrap{})) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &MessageFinger{ | ||
expr: fingerExpr, | ||
program: program, | ||
}, nil | ||
} | ||
|
||
// Run 根据指定的表达式创建 message 的指纹 | ||
func (m *MessageFinger) Run(msg repository.Message) (string, error) { | ||
result, err := expr.Run(m.program, NewMessageWrap(msg)) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
if result == nil { | ||
return "", nil | ||
} | ||
|
||
return fmt.Sprintf("%v", result), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package matcher_test | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/mylxsw/adanos-alert/internal/matcher" | ||
"github.com/mylxsw/adanos-alert/internal/repository" | ||
"github.com/stretchr/testify/assert" | ||
"go.mongodb.org/mongo-driver/bson/primitive" | ||
) | ||
|
||
func TestMessageFinger(t *testing.T) { | ||
var msg = repository.Message{ | ||
ID: primitive.NewObjectID(), | ||
Content: `{"log_level": "debug", "message": "request", "context": {"user_id": 123}}`, | ||
Meta: repository.MessageMeta{ | ||
"environment": "dev", | ||
"server": "192.168.1.1", | ||
}, | ||
Tags: []string{"php", "nodejs"}, | ||
Origin: "Filebeat", | ||
CreatedAt: time.Now(), | ||
} | ||
|
||
{ | ||
f, err := matcher.NewMessageFinger(`Meta["server"] + ":" + Origin`) | ||
assert.NoError(t, err) | ||
|
||
finger, err := f.Run(msg) | ||
assert.NoError(t, err) | ||
assert.Equal(t, "192.168.1.1:Filebeat", finger) | ||
} | ||
|
||
{ | ||
|
||
f, err := matcher.NewMessageFinger(``) | ||
assert.NoError(t, err) | ||
|
||
finger, err := f.Run(msg) | ||
assert.NoError(t, err) | ||
assert.Equal(t, "", finger) | ||
} | ||
|
||
{ | ||
f, err := matcher.NewMessageFinger(`"hello world"`) | ||
assert.NoError(t, err) | ||
|
||
finger, err := f.Run(msg) | ||
assert.NoError(t, err) | ||
assert.Equal(t, "hello world", finger) | ||
} | ||
|
||
{ | ||
f, err := matcher.NewMessageFinger(`Meta["not_exist_key"]`) | ||
assert.NoError(t, err) | ||
|
||
finger, err := f.Run(msg) | ||
assert.NoError(t, err) | ||
assert.Equal(t, "", finger) | ||
} | ||
|
||
{ | ||
f, err := matcher.NewMessageFinger(`124`) | ||
assert.NoError(t, err) | ||
|
||
finger, err := f.Run(msg) | ||
assert.NoError(t, err) | ||
assert.Equal(t, "124", finger) | ||
} | ||
|
||
{ | ||
f, err := matcher.NewMessageFinger(`true`) | ||
assert.NoError(t, err) | ||
|
||
finger, err := f.Run(msg) | ||
assert.NoError(t, err) | ||
assert.Equal(t, "true", finger) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.