handler.go
1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package handler
import (
"time"
jsoniter "github.com/json-iterator/go"
"github.com/tal-tech/go-stash/stash/es"
"github.com/tal-tech/go-stash/stash/filter"
)
const (
timestampFormat = "2006-01-02T15:04:05.000Z"
timestampKey = "@timestamp"
)
type MessageHandler struct {
writer *es.Writer
filters []filter.FilterFunc
}
func NewHandler(writer *es.Writer) *MessageHandler {
return &MessageHandler{
writer: writer,
}
}
func (mh *MessageHandler) AddFilters(filters ...filter.FilterFunc) {
for _, f := range filters {
mh.filters = append(mh.filters, f)
}
}
func (mh *MessageHandler) Consume(_, val string) error {
m := make(map[string]interface{})
if err := jsoniter.Unmarshal([]byte(val), &m); err != nil {
return err
}
for _, proc := range mh.filters {
if m = proc(m); m == nil {
return nil
}
}
bs, err := jsoniter.Marshal(m)
if err != nil {
return err
}
return mh.writer.Write(mh.getTime(m), string(bs))
}
func (mh *MessageHandler) getTime(m map[string]interface{}) time.Time {
if ti, ok := m[timestampKey]; ok {
if ts, ok := ti.(string); ok {
if t, err := time.Parse(timestampFormat, ts); err == nil {
return t
}
}
}
return time.Now()
}