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
Changes from 1 commit
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
27 changes: 16 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,12 @@ func (p *Plugin) Do(event *pipeline.Event) pipeline.ActionResult {
}

// place decoded object under root
event.Root.MergeWith(node)
fields := node.AsFields()
for _, child := range fields {
childField := child.AsString()
x := event.Root.AddFieldNoAlloc(event.Root, childField)
x.MutateToNode(child.AsFieldValue())
}

return pipeline.ActionPass
}
Loading