Skip to content

Commit 0b75186

Browse files
committed
<feature>support metadata && threshold
1 parent 13a2b49 commit 0b75186

File tree

4 files changed

+47
-2
lines changed

4 files changed

+47
-2
lines changed

alert/alert.go

+34-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package alert
44
import (
55
"bytes"
66
"fmt"
7+
"strconv"
78
"text/template"
89
"time"
910

@@ -27,11 +28,15 @@ type Alert struct {
2728
Search search.Dict `yaml:"search"`
2829
SearchQuery string `yaml:"search_query"`
2930
Process luautil.LuaRunner `yaml:"process"`
31+
ThrottlePeriodStr string `yaml:"throttle_period"`
32+
Metadata map[string]interface{} `yaml:"metadata"`
3033

3134
Jobber *jobber.FullTimeSpec
3235
SearchIndexTPL *template.Template
3336
SearchTypeTPL *template.Template
3437
SearchTPL *template.Template
38+
ThrottlePeriod uint
39+
LastActionTime uint
3540
}
3641

3742
func templatizeHelper(i interface{}, lastErr error) (*template.Template, error) {
@@ -73,10 +78,16 @@ func (a *Alert) Init() error {
7378
}
7479
a.Jobber = jb
7580

81+
a.LastActionTime = 0
82+
a.ThrottlePeriod, err = parseThrottlePeriod(a.ThrottlePeriodStr)
83+
if err != nil {
84+
return err
85+
}
86+
7687
return nil
7788
}
7889

79-
func (a Alert) Run() {
90+
func (a *Alert) Run() {
8091
kv := log.Fields{
8192
"name": a.Name,
8293
}
@@ -87,6 +98,7 @@ func (a Alert) Run() {
8798
Name: a.Name,
8899
StartedTS: uint64(now.Unix()),
89100
Time: now,
101+
Metadata: a.Metadata,
90102
}
91103

92104
searchIndex, searchType, searchQuery, err := a.CreateSearch(c)
@@ -139,6 +151,7 @@ func (a Alert) Run() {
139151
return
140152
}
141153
}
154+
a.LastActionTime = uint(now.Unix())
142155
}
143156

144157
func (a Alert) CreateSearch(c context.Context) (string, string, interface{}, error) {
@@ -167,3 +180,23 @@ func (a Alert) CreateSearch(c context.Context) (string, string, interface{}, err
167180

168181
return searchIndex, searchType, search, nil
169182
}
183+
184+
func parseThrottlePeriod(throttlePeriodStr string) (uint, error) {
185+
var multi int
186+
suffix := throttlePeriodStr[len(throttlePeriodStr)-1:]
187+
switch suffix {
188+
case "s":
189+
multi = 1
190+
case "m":
191+
multi = 60
192+
case "h":
193+
multi = 3600
194+
default:
195+
return 0, fmt.Errorf("only support int[s,m,h], now is %s", throttlePeriodStr)
196+
}
197+
throttlePeriod, err := strconv.Atoi(throttlePeriodStr[:len(throttlePeriodStr)-1])
198+
if err != nil {
199+
return 0, err
200+
}
201+
return uint(throttlePeriod * multi), nil
202+
}

cmd/esalert/main.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,9 @@ func main() {
8484
func alertSpin(a alert.Alert) {
8585
for {
8686
now := time.Now()
87+
nowUnix := uint(now.Unix())
8788
next := a.Jobber.Next(now)
88-
if now == next {
89+
if now == next && nowUnix - a.LastActionTime > a.ThrottlePeriod {
8990
go a.Run()
9091
time.Sleep(time.Second)
9192
} else {

context/context.go

+1
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ type Context struct {
1616
StartedTS uint64
1717
search.Result `luautil:",inline"`
1818
time.Time `luautil:"-"`
19+
Metadata map[string]interface{}
1920
}

luautil/luautil.go

+10
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"reflect"
1111
"strconv"
1212
"strings"
13+
"text/template"
1314

1415
"esalert/config"
1516
"esalert/context"
@@ -30,6 +31,15 @@ func (l *LuaRunner) Do(c context.Context) (interface{}, bool) {
3031
if l.File != "" {
3132
return RunFile(c, l.File)
3233
} else if l.Inline != "" {
34+
t, err := template.New("").Parse(l.Inline)
35+
if err != nil {
36+
return nil, false
37+
}
38+
buf := bytes.NewBuffer(make([]byte, 0, 1024))
39+
if err := t.Execute(buf, c); err != nil {
40+
return nil, false
41+
}
42+
l.Inline = buf.String()
3343
return RunInline(c, l.Inline)
3444
}
3545
return nil, false

0 commit comments

Comments
 (0)