From c6519c7793566dea49f6e5612146b664ab172b38 Mon Sep 17 00:00:00 2001 From: Daniel Nelson Date: Tue, 21 Jan 2020 10:10:02 -0800 Subject: [PATCH] Hook up json_strict option with default of true (#6909) --- internal/config/config.go | 17 ++++++++++++++++- internal/config/config_test.go | 1 + plugins/parsers/json/README.md | 2 +- 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/internal/config/config.go b/internal/config/config.go index 586acce71..6e05ce45b 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -1411,7 +1411,9 @@ func buildParser(name string, tbl *ast.Table) (parsers.Parser, error) { } func getParserConfig(name string, tbl *ast.Table) (*parsers.Config, error) { - c := &parsers.Config{} + c := &parsers.Config{ + JSONStrict: true, + } if node, ok := tbl.Fields["data_format"]; ok { if kv, ok := node.(*ast.KeyValue); ok { @@ -1512,6 +1514,18 @@ func getParserConfig(name string, tbl *ast.Table) (*parsers.Config, error) { } } + if node, ok := tbl.Fields["json_strict"]; ok { + if kv, ok := node.(*ast.KeyValue); ok { + if b, ok := kv.Value.(*ast.Boolean); ok { + var err error + c.JSONStrict, err = b.Boolean() + if err != nil { + return nil, err + } + } + } + } + if node, ok := tbl.Fields["data_type"]; ok { if kv, ok := node.(*ast.KeyValue); ok { if str, ok := kv.Value.(*ast.String); ok { @@ -1808,6 +1822,7 @@ func getParserConfig(name string, tbl *ast.Table) (*parsers.Config, error) { delete(tbl.Fields, "json_time_format") delete(tbl.Fields, "json_time_key") delete(tbl.Fields, "json_timezone") + delete(tbl.Fields, "json_strict") delete(tbl.Fields, "data_type") delete(tbl.Fields, "collectd_auth_file") delete(tbl.Fields, "collectd_security_level") diff --git a/internal/config/config_test.go b/internal/config/config_test.go index 7559bf9fe..9d42177cd 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -149,6 +149,7 @@ func TestConfig_LoadDirectory(t *testing.T) { p, err := parsers.NewParser(&parsers.Config{ MetricName: "exec", DataFormat: "json", + JSONStrict: true, }) assert.NoError(t, err) ex.SetParser(p) diff --git a/plugins/parsers/json/README.md b/plugins/parsers/json/README.md index b4975bcd3..b318f32e0 100644 --- a/plugins/parsers/json/README.md +++ b/plugins/parsers/json/README.md @@ -20,7 +20,7 @@ ignored unless specified in the `tag_key` or `json_string_fields` options. ## When strict is true and a JSON array is being parsed, all objects within the ## array must be valid - strict = false + json_strict = true ## Query is a GJSON path that specifies a specific chunk of JSON to be ## parsed, if not specified the whole document will be parsed.