Skip to content

Commit

Permalink
Add logging options for json_decode parse errors (#597)
Browse files Browse the repository at this point in the history
* Add logging options for json_decode parse errors

* Update docs
  • Loading branch information
HeadHunter483 authored Mar 14, 2024
1 parent 766777f commit f78c15f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
11 changes: 11 additions & 0 deletions plugin/action/json_decode/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,16 @@ A prefix to add to decoded object keys.

<br>

**`log_json_parse_error_mode`** *`string`* *`default=off`* *`options=off|erronly|withnode`*

Defines how to handle logging of json parse error.
* `off` – do not log json parse errors
* `erronly` – log only errors without any other data
* `withnode` – log errors with json node represented as string

Defaults to `off`.

<br>


<br>*Generated using [__insane-doc__](https://github.com/vitkovskii/insane-doc)*
38 changes: 37 additions & 1 deletion plugin/action/json_decode/json_decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/ozontech/file.d/cfg"
"github.com/ozontech/file.d/fd"
"github.com/ozontech/file.d/pipeline"
"go.uber.org/zap"
)

/*{ introduction
Expand All @@ -13,8 +14,18 @@ If the decoded JSON isn't an object, the event will be skipped.

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

type logJsonParseErrorMode int

const (
// ! "jsonParseErrorMode" #1 /`([a-z]+)`/
logJsonParseErrorOff logJsonParseErrorMode = iota // * `off` – do not log json parse errors
logJsonParseErrorErrOnly // * `erronly` – log only errors without any other data
logJsonParseErrorWithNode // * `withnode` – log errors with json node represented as string
)

// ! config-params
// ^ config-params
type Config struct {
Expand All @@ -28,6 +39,15 @@ type Config struct {
// >
// > A prefix to add to decoded object keys.
Prefix string `json:"prefix" default:""` // *

// > @3@4@5@6
// >
// > Defines how to handle logging of json parse error.
// > @jsonParseErrorMode|comment-list
// >
// > Defaults to `off`.
LogJSONParseErrorMode string `json:"log_json_parse_error_mode" default:"off" options:"off|erronly|withnode"` // *
LogJSONParseErrorMode_ logJsonParseErrorMode
}

func init() {
Expand All @@ -41,8 +61,19 @@ func factory() (pipeline.AnyPlugin, pipeline.AnyConfig) {
return &Plugin{}, &Config{}
}

func (p *Plugin) Start(config pipeline.AnyConfig, _ *pipeline.ActionPluginParams) {
func (p *Plugin) Start(config pipeline.AnyConfig, params *pipeline.ActionPluginParams) {
p.config = config.(*Config)
p.logger = params.Logger.Desugar()
switch p.config.LogJSONParseErrorMode {
case "off":
p.config.LogJSONParseErrorMode_ = logJsonParseErrorOff
case "erronly":
p.config.LogJSONParseErrorMode_ = logJsonParseErrorErrOnly
case "withnode":
p.config.LogJSONParseErrorMode_ = logJsonParseErrorWithNode
default:
p.config.LogJSONParseErrorMode_ = logJsonParseErrorOff
}
}

func (p *Plugin) Stop() {
Expand All @@ -56,6 +87,11 @@ func (p *Plugin) Do(event *pipeline.Event) pipeline.ActionResult {

node, err := event.SubparseJSON(jsonNode.AsBytes())
if err != nil {
if p.config.LogJSONParseErrorMode_ == logJsonParseErrorErrOnly {
p.logger.Error("failed to parse json", zap.Error(err))
} else if p.config.LogJSONParseErrorMode_ == logJsonParseErrorWithNode {
p.logger.Error("failed to parse json", zap.Error(err), zap.String("node", jsonNode.AsString()))
}
return pipeline.ActionPass
}

Expand Down

0 comments on commit f78c15f

Please sign in to comment.