forked from inthecloud247/heka-clever-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_decoder.go
88 lines (76 loc) · 2.33 KB
/
json_decoder.go
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package heka_clever_plugins
import (
"encoding/json"
"fmt"
"github.com/mozilla-services/heka/message"
"github.com/mozilla-services/heka/pipeline"
"strings"
)
type JsonDecoderConfig struct {
MessageFields pipeline.MessageTemplate `toml:"message_fields"`
}
type JsonDecoder struct {
dRunner pipeline.DecoderRunner
messageFields pipeline.MessageTemplate
}
func (kvd *JsonDecoder) ConfigStruct() interface{} {
return new(JsonDecoderConfig)
}
func (kvd *JsonDecoder) Init(config interface{}) (err error) {
conf := config.(*JsonDecoderConfig)
kvd.messageFields = make(pipeline.MessageTemplate)
if conf.MessageFields != nil {
for field, action := range conf.MessageFields {
kvd.messageFields[field] = action
}
}
return
}
func (kvd *JsonDecoder) Decode(pack *pipeline.PipelinePack) (packs []*pipeline.PipelinePack, err error) {
jsonMap, jsonString, err := ParseJson(pack.Message.GetPayload(), true)
if err != nil {
return nil, err
}
// Overwrite Payload with just the JSON string
*pack.Message.Payload = jsonString
// For keys that have string values, write to message.Fields
for name, val := range jsonMap {
// null in JSON becomes nil, which panics when message.NewField interally calls val.Type()
if val != nil {
if f, err := message.NewField(name, val, ""); err == nil {
pack.Message.AddField(f)
}
}
}
if err = kvd.messageFields.PopulateMessage(pack.Message, nil); err != nil {
return
}
return []*pipeline.PipelinePack{pack}, nil
}
func (kvd *JsonDecoder) SetDecoderRunner(dr pipeline.DecoderRunner) {
kvd.dRunner = dr
}
func init() {
pipeline.RegisterPlugin("JsonDecoder", func() interface{} {
return new(JsonDecoder)
})
}
// ParseJson takes a json string and unmarshals it into a struct
func ParseJson(s string, findJsonSubstring bool) (jsonMap map[string]interface{}, jsonString string, err error) {
// Naively find start of JSON by looking for first '{'
if findJsonSubstring {
// Just parse one line
if lineBreakIdx := strings.Index(s, "\n"); lineBreakIdx > 0 {
s = s[:lineBreakIdx] + "\n"
}
if leftBraceIndex := strings.Index(s, "{"); leftBraceIndex >= 0 {
s = s[leftBraceIndex:]
} else {
return nil, "", fmt.Errorf("could not find a json substring within log line: %s", s)
}
}
if err := json.Unmarshal([]byte(s), &jsonMap); err != nil {
return nil, "", err
}
return jsonMap, s, nil
}