Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce allocations in the json_decode plugin #743

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions pipeline/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,20 @@ func CreateNestedField(root *insaneJSON.Root, path []string) *insaneJSON.Node {
}
return curr
}

// MergeToRoot like insaneJSON.Node.MergeWith, but without allocations.
func MergeToRoot(root *insaneJSON.Root, src *insaneJSON.Node) {
if root == nil || root.Node == nil || src == nil {
return
}
if !root.IsObject() || !src.IsObject() {
return
}

fields := src.AsFields()
for _, child := range fields {
childField := child.AsString()
x := root.Node.AddFieldNoAlloc(root, childField)
x.MutateToNode(child.AsFieldValue())
}
}
4 changes: 2 additions & 2 deletions plugin/action/decode/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ func (p *Plugin) decodeJson(root *insaneJSON.Root, node *insaneJSON.Node, buf []
node.Suicide()
}

root.MergeWith(jsonNode)
pipeline.MergeToRoot(root, jsonNode)
}

func (p *Plugin) decodePostgres(root *insaneJSON.Root, node *insaneJSON.Node) {
Expand Down Expand Up @@ -566,7 +566,7 @@ func (p *Plugin) decodeProtobuf(root *insaneJSON.Root, node *insaneJSON.Node, bu
node.Suicide()
}

root.MergeWith(t)
pipeline.MergeToRoot(root, t)
}

func (p *Plugin) addFieldPrefix(root *insaneJSON.Root, key string, val []byte) {
Expand Down
2 changes: 1 addition & 1 deletion plugin/action/flatten/flatten.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (p *Plugin) Do(event *pipeline.Event) pipeline.ActionResult {
}

// place decoded object under root
event.Root.MergeWith(node)
pipeline.MergeToRoot(event.Root, node)

return pipeline.ActionPass
}
22 changes: 11 additions & 11 deletions plugin/action/json_decode/json_decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ If the decoded JSON isn't an object, the event will be skipped.
}*/

type Plugin struct {
config *Config
logger *zap.Logger
config *Config
logger *zap.Logger
parseMode logJsonParseErrorMode
}

type logJsonParseErrorMode int
Expand Down Expand Up @@ -48,8 +49,7 @@ type Config struct {
// > @jsonParseErrorMode|comment-list
// >
// > Defaults to `off`.
LogJSONParseErrorMode string `json:"log_json_parse_error_mode" default:"off" options:"off|erronly|withnode"` // *
LogJSONParseErrorMode_ logJsonParseErrorMode
LogJSONParseErrorMode string `json:"log_json_parse_error_mode" default:"off" options:"off|erronly|withnode"` // *
}

func init() {
Expand All @@ -68,13 +68,13 @@ func (p *Plugin) Start(config pipeline.AnyConfig, params *pipeline.ActionPluginP
p.logger = params.Logger.Desugar()
switch p.config.LogJSONParseErrorMode {
case "off":
p.config.LogJSONParseErrorMode_ = logJsonParseErrorOff
p.parseMode = logJsonParseErrorOff
Copy link
Collaborator Author

@vadimalekseev vadimalekseev Jan 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It fixes the data race in the Start() method

case "erronly":
p.config.LogJSONParseErrorMode_ = logJsonParseErrorErrOnly
p.parseMode = logJsonParseErrorErrOnly
case "withnode":
p.config.LogJSONParseErrorMode_ = logJsonParseErrorWithNode
p.parseMode = logJsonParseErrorWithNode
default:
p.config.LogJSONParseErrorMode_ = logJsonParseErrorOff
p.parseMode = logJsonParseErrorOff
}
}

Expand All @@ -89,9 +89,9 @@ func (p *Plugin) Do(event *pipeline.Event) pipeline.ActionResult {

node, err := event.Root.DecodeBytesAdditional(jsonNode.AsBytes())
if err != nil {
if p.config.LogJSONParseErrorMode_ == logJsonParseErrorErrOnly {
if p.parseMode == logJsonParseErrorErrOnly {
p.logger.Error("failed to parse json", zap.Error(err))
} else if p.config.LogJSONParseErrorMode_ == logJsonParseErrorWithNode {
} else if p.parseMode == logJsonParseErrorWithNode {
p.logger.Error("failed to parse json", zap.Error(err), zap.String("node", jsonNode.AsString()))
}
return pipeline.ActionPass
Expand All @@ -114,7 +114,7 @@ func (p *Plugin) Do(event *pipeline.Event) pipeline.ActionResult {
}

// place decoded object under root
event.Root.MergeWith(node)
pipeline.MergeToRoot(event.Root, node)

return pipeline.ActionPass
}
2 changes: 1 addition & 1 deletion plugin/action/parse_re2/parse_re2.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (p *Plugin) Do(event *pipeline.Event) pipeline.ActionResult {
root.AddFieldNoAlloc(root, pipeline.ByteToStringUnsafe(event.Buf[bl:len(event.Buf)])).MutateToBytes(sm[i])
}

event.Root.MergeWith(root.Node)
pipeline.MergeToRoot(event.Root, root.Node)

insaneJSON.Release(root)

Expand Down