forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonhelper.go
78 lines (67 loc) · 2.09 KB
/
jsonhelper.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
package jsontransform
import (
"fmt"
"time"
"github.com/elastic/beats/libbeat/beat"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
)
// WriteJSONKeys writes the json keys to the given event based on the overwriteKeys option
func WriteJSONKeys(event *beat.Event, keys map[string]interface{}, overwriteKeys bool) {
if !overwriteKeys {
for k, v := range keys {
if _, exists := event.Fields[k]; !exists && k != "@timestamp" && k != "@metadata" {
event.Fields[k] = v
}
}
return
}
for k, v := range keys {
switch k {
case "@timestamp":
vstr, ok := v.(string)
if !ok {
logp.Err("JSON: Won't overwrite @timestamp because value is not string")
event.Fields["error"] = createJSONError("@timestamp not overwritten (not string)")
continue
}
// @timestamp must be of format RFC3339
ts, err := time.Parse(time.RFC3339, vstr)
if err != nil {
logp.Err("JSON: Won't overwrite @timestamp because of parsing error: %v", err)
event.Fields["error"] = createJSONError(fmt.Sprintf("@timestamp not overwritten (parse error on %s)", vstr))
continue
}
event.Timestamp = ts
case "@metadata":
switch m := v.(type) {
case map[string]string:
for meta, value := range m {
event.Meta[meta] = value
}
case map[string]interface{}:
event.Meta.Update(common.MapStr(m))
default:
event.Fields["error"] = createJSONError("failed to update @metadata")
}
case "type":
vstr, ok := v.(string)
if !ok {
logp.Err("JSON: Won't overwrite type because value is not string")
event.Fields["error"] = createJSONError("type not overwritten (not string)")
continue
}
if len(vstr) == 0 || vstr[0] == '_' {
logp.Err("JSON: Won't overwrite type because value is empty or starts with an underscore")
event.Fields["error"] = createJSONError(fmt.Sprintf("type not overwritten (invalid value [%s])", vstr))
continue
}
event.Fields[k] = vstr
default:
event.Fields[k] = v
}
}
}
func createJSONError(message string) common.MapStr {
return common.MapStr{"message": message, "type": "json"}
}