-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
WriteJSONKeys reduces code duplication between the Filebeat json parser and the decode_json_fields processor. Fixed bug with decode_json_fields where the `when` condition was not allowed.
- Loading branch information
1 parent
8907a3e
commit 10e1ff2
Showing
3 changed files
with
55 additions
and
80 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
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,51 @@ | ||
package jsontransform | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/elastic/beats/libbeat/common" | ||
"github.com/elastic/beats/libbeat/logp" | ||
) | ||
|
||
func WriteJSONKeys(event common.MapStr, keys map[string]interface{}, overwriteKeys bool, errorKey string) { | ||
for k, v := range keys { | ||
if overwriteKeys { | ||
if k == "@timestamp" { | ||
vstr, ok := v.(string) | ||
if !ok { | ||
logp.Err("JSON: Won't overwrite @timestamp because value is not string") | ||
event[errorKey] = "@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[errorKey] = fmt.Sprintf("@timestamp not overwritten (parse error on %s)", vstr) | ||
continue | ||
} | ||
event[k] = common.Time(ts) | ||
} else if k == "type" { | ||
vstr, ok := v.(string) | ||
if !ok { | ||
logp.Err("JSON: Won't overwrite type because value is not string") | ||
event[errorKey] = "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[errorKey] = fmt.Sprintf("type not overwritten (invalid value [%s])", vstr) | ||
continue | ||
} | ||
event[k] = vstr | ||
} else { | ||
event[k] = v | ||
} | ||
} else if _, exists := event[k]; !exists { | ||
event[k] = v | ||
} | ||
|
||
} | ||
} |
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