Skip to content

Commit

Permalink
feat (parser.json_v2): Support defining field/tag tables within an ob…
Browse files Browse the repository at this point in the history
…ject table (#9449)
  • Loading branch information
sspaink authored Oct 4, 2021
1 parent c1f51b0 commit df5c19c
Show file tree
Hide file tree
Showing 17 changed files with 617 additions and 86 deletions.
64 changes: 42 additions & 22 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -1421,28 +1421,8 @@ func (c *Config) getParserConfig(name string, tbl *ast.Table) (*parsers.Config,
c.getFieldString(metricConfig, "timestamp_format", &mc.TimestampFormat)
c.getFieldString(metricConfig, "timestamp_timezone", &mc.TimestampTimezone)

if fieldConfigs, ok := metricConfig.Fields["field"]; ok {
if fieldConfigs, ok := fieldConfigs.([]*ast.Table); ok {
for _, fieldconfig := range fieldConfigs {
var f json_v2.DataSet
c.getFieldString(fieldconfig, "path", &f.Path)
c.getFieldString(fieldconfig, "rename", &f.Rename)
c.getFieldString(fieldconfig, "type", &f.Type)
mc.Fields = append(mc.Fields, f)
}
}
}
if fieldConfigs, ok := metricConfig.Fields["tag"]; ok {
if fieldConfigs, ok := fieldConfigs.([]*ast.Table); ok {
for _, fieldconfig := range fieldConfigs {
var t json_v2.DataSet
c.getFieldString(fieldconfig, "path", &t.Path)
c.getFieldString(fieldconfig, "rename", &t.Rename)
t.Type = "string"
mc.Tags = append(mc.Tags, t)
}
}
}
mc.Fields = getFieldSubtable(c, metricConfig)
mc.Tags = getTagSubtable(c, metricConfig)

if objectconfigs, ok := metricConfig.Fields["object"]; ok {
if objectconfigs, ok := objectconfigs.([]*ast.Table); ok {
Expand All @@ -1458,6 +1438,10 @@ func (c *Config) getParserConfig(name string, tbl *ast.Table) (*parsers.Config,
c.getFieldStringSlice(objectConfig, "tags", &o.Tags)
c.getFieldStringMap(objectConfig, "renames", &o.Renames)
c.getFieldStringMap(objectConfig, "fields", &o.Fields)

o.FieldPaths = getFieldSubtable(c, objectConfig)
o.TagPaths = getTagSubtable(c, objectConfig)

mc.JSONObjects = append(mc.JSONObjects, o)
}
}
Expand All @@ -1477,6 +1461,42 @@ func (c *Config) getParserConfig(name string, tbl *ast.Table) (*parsers.Config,
return pc, nil
}

func getFieldSubtable(c *Config, metricConfig *ast.Table) []json_v2.DataSet {
var fields []json_v2.DataSet

if fieldConfigs, ok := metricConfig.Fields["field"]; ok {
if fieldConfigs, ok := fieldConfigs.([]*ast.Table); ok {
for _, fieldconfig := range fieldConfigs {
var f json_v2.DataSet
c.getFieldString(fieldconfig, "path", &f.Path)
c.getFieldString(fieldconfig, "rename", &f.Rename)
c.getFieldString(fieldconfig, "type", &f.Type)
fields = append(fields, f)
}
}
}

return fields
}

func getTagSubtable(c *Config, metricConfig *ast.Table) []json_v2.DataSet {
var tags []json_v2.DataSet

if fieldConfigs, ok := metricConfig.Fields["tag"]; ok {
if fieldConfigs, ok := fieldConfigs.([]*ast.Table); ok {
for _, fieldconfig := range fieldConfigs {
var t json_v2.DataSet
c.getFieldString(fieldconfig, "path", &t.Path)
c.getFieldString(fieldconfig, "rename", &t.Rename)
t.Type = "string"
tags = append(tags, t)
}
}
}

return tags
}

// buildSerializer grabs the necessary entries from the ast.Table for creating
// a serializers.Serializer object, and creates it, which can then be added onto
// an Output object.
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ require (
github.com/stretchr/testify v1.7.0
github.com/tbrandon/mbserver v0.0.0-20170611213546-993e1772cc62
github.com/testcontainers/testcontainers-go v0.11.1
github.com/tidwall/gjson v1.8.0
github.com/tidwall/gjson v1.9.0
github.com/tidwall/match v1.0.3 // indirect
github.com/tidwall/pretty v1.1.0 // indirect
github.com/tinylib/msgp v1.1.6
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1535,8 +1535,8 @@ github.com/tchap/go-patricia v2.2.6+incompatible/go.mod h1:bmLyhP68RS6kStMGxByiQ
github.com/tedsuo/ifrit v0.0.0-20180802180643-bea94bb476cc/go.mod h1:eyZnKCc955uh98WQvzOm0dgAeLnf2O0Rz0LPoC5ze+0=
github.com/testcontainers/testcontainers-go v0.11.1 h1:FiYsB83LSGbiawoV8TpAZGfcCUbtaeeg1SXqEKUxh08=
github.com/testcontainers/testcontainers-go v0.11.1/go.mod h1:/V0UVq+1e7NWYoqTPog179clf0Qp9TOyp4EcXaEFQz8=
github.com/tidwall/gjson v1.8.0 h1:Qt+orfosKn0rbNTZqHYDqBrmm3UDA4KRkv70fDzG+PQ=
github.com/tidwall/gjson v1.8.0/go.mod h1:5/xDoumyyDNerp2U36lyolv46b3uF/9Bu6OfyQ9GImk=
github.com/tidwall/gjson v1.9.0 h1:+Od7AE26jAaMgVC31cQV/Ope5iKXulNMflrlB7k+F9E=
github.com/tidwall/gjson v1.9.0/go.mod h1:5/xDoumyyDNerp2U36lyolv46b3uF/9Bu6OfyQ9GImk=
github.com/tidwall/match v1.0.3 h1:FQUVvBImDutD8wJLN6c5eMzWtjgONK9MwIBCOrUJKeE=
github.com/tidwall/match v1.0.3/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
Expand Down
80 changes: 57 additions & 23 deletions plugins/parsers/json_v2/README.md

Large diffs are not rendered by default.

Loading

1 comment on commit df5c19c

@frans-panken
Copy link

Choose a reason for hiding this comment

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

With me (telegraf Telegraf 1.20.4), the output is slightly different, as the renaming of the tags failed and other fields are added (see file)
What confused me is: I created a new input file where I pasted the objects within the two input files json1 & json2 with a new array (so basically a new file containing [,] where json1 was place before the comma and json2 after the comma). Running telegraf with this new file results in the following error: "2021-11-21T16:51:00Z E! [inputs.file] Error in plugin: GJSON Path returned null
output_multiple_json_input.txt
"

Please sign in to comment.