From 276cf6e88812784a7bdc55a3385924fca5d1303e Mon Sep 17 00:00:00 2001 From: helenosheaa <38860767+helenosheaa@users.noreply.github.com> Date: Wed, 16 Dec 2020 15:35:41 -0500 Subject: [PATCH 01/10] Add support for wildcard glob matching for tags --- .gitignore | 2 +- .../random_number/random_number.go | 43 +++ examples/json-parse | 16 + plugins/parsers/json/README.md | 6 +- plugins/parsers/json/parser.go | 34 +- plugins/parsers/json/parser_test.go | 329 +++++++++++++++++- telegraf.conf | 17 + 7 files changed, 430 insertions(+), 17 deletions(-) create mode 100644 examples/input-plugins/random_number/random_number.go create mode 100644 examples/json-parse create mode 100644 telegraf.conf diff --git a/.gitignore b/.gitignore index df2b3d06643c5..9b73f5f391b33 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,4 @@ /telegraf.exe /telegraf.gz /vendor -.DS_Store +.DS_Store \ No newline at end of file diff --git a/examples/input-plugins/random_number/random_number.go b/examples/input-plugins/random_number/random_number.go new file mode 100644 index 0000000000000..a9fc000c74718 --- /dev/null +++ b/examples/input-plugins/random_number/random_number.go @@ -0,0 +1,43 @@ +package random_number + +import ( + "fmt" + "math/rand" + + "github.com/influxdata/telegraf" + "github.com/influxdata/telegraf/plugins/inputs" +) + +// RandomNumber struct should be named the same as the Plugin +type RandomNumber struct { + Min int + Max int +} + +// Description will appear directly above the plugin definition in the config file +func (r *RandomNumber) Description() string { + return `This is a random number generator plugin which takes in a min and a max` +} + +// SampleConfig will populate the sample configuration portion of the plugin's configuration +func (r *RandomNumber) SampleConfig() string { + return ` Min = 1 Max = 10000` +} + +// Init can be implemented to do one-time processing stuff like initializing variables +// func (r *RandomNumber) Init() error { +// r.randomNumber = rand.Int() +// return nil +// } + +// Gather defines what data the plugin will gather. +func (r *RandomNumber) Gather(acc telegraf.Accumulator) error { + randomNumber := rand.Intn(r.Max-r.Min) + r.Min + fmt.Println("random", randomNumber) + acc.AddFields("state", map[string]interface{}{"value": randomNumber}, nil) + return nil +} + +func init() { + inputs.Add("random_number", func() telegraf.Input { return &RandomNumber{Min: 1, Max: 100000} }) +} diff --git a/examples/json-parse b/examples/json-parse new file mode 100644 index 0000000000000..2662c6ef15c0c --- /dev/null +++ b/examples/json-parse @@ -0,0 +1,16 @@ +{ + "fields": { + "value": 2 + }, + "name": "metrics_name.test", + "tags": { + "InstanceId": "i-123", + "app": "test", + "availability-zone": "ap-southeast-1z", + "cluster": "dev", + "env": "development", + "host": "ip-10.compute.internal", + "metric_type": "counter" + } +} + diff --git a/plugins/parsers/json/README.md b/plugins/parsers/json/README.md index d39a9d6bf77d9..307aab94c5483 100644 --- a/plugins/parsers/json/README.md +++ b/plugins/parsers/json/README.md @@ -30,10 +30,12 @@ ignored unless specified in the `tag_key` or `json_string_fields` options. json_query = "" ## Tag keys is an array of keys that should be added as tags. Matching keys - ## are no longer saved as fields. + ## are no longer saved as fields. Now supports wildcard glob matching. tag_keys = [ "my_tag_1", - "my_tag_2" + "my_tag_2", + "tags_*", + "tag*" ] ## Array of glob pattern strings keys that should be added as string fields. diff --git a/plugins/parsers/json/parser.go b/plugins/parsers/json/parser.go index bd9dee869170f..f678592e97c8c 100644 --- a/plugins/parsers/json/parser.go +++ b/plugins/parsers/json/parser.go @@ -36,7 +36,7 @@ type Config struct { type Parser struct { metricName string - tagKeys []string + tagKeys filter.Filter stringFields filter.Filter nameKey string query string @@ -49,13 +49,20 @@ type Parser struct { func New(config *Config) (*Parser, error) { stringFilter, err := filter.Compile(config.StringFields) + if err != nil { - return nil, err + return nil, nil + } + + tagKeyFilter, err := filter.Compile(config.TagKeys) + + if err != nil { + return nil, nil } return &Parser{ metricName: config.MetricName, - tagKeys: config.TagKeys, + tagKeys: tagKeyFilter, nameKey: config.NameKey, stringFields: stringFilter, query: config.Query, @@ -150,27 +157,32 @@ func (p *Parser) parseObject(data map[string]interface{}, timestamp time.Time) ( //will delete any strings/bools that shouldn't be fields //assumes that any non-numeric values in TagKeys should be displayed as tags func (p *Parser) switchFieldToTag(tags map[string]string, fields map[string]interface{}) (map[string]string, map[string]interface{}) { - for _, name := range p.tagKeys { - //switch any fields in tagkeys into tags - if fields[name] == nil { + + for name, value := range fields { + if p.tagKeys == nil { continue } - switch value := fields[name].(type) { + // skip switch statement if tagkey doesn't match fieldname + if !p.tagKeys.Match(name) { + continue + } + //switch any fields in tagkeys into tags + switch t := value.(type) { case string: - tags[name] = value + tags[name] = t delete(fields, name) case bool: - tags[name] = strconv.FormatBool(value) + tags[name] = strconv.FormatBool(t) delete(fields, name) case float64: - tags[name] = strconv.FormatFloat(value, 'f', -1, 64) + tags[name] = strconv.FormatFloat(t, 'f', -1, 64) delete(fields, name) default: log.Printf("E! [parsers.json] Unrecognized type %T", value) } } - //remove any additional string/bool values from fields + // remove any additional string/bool values from fields for fk := range fields { switch fields[fk].(type) { case string, bool: diff --git a/plugins/parsers/json/parser_test.go b/plugins/parsers/json/parser_test.go index 31c507e7517f7..b8117a8ea8289 100644 --- a/plugins/parsers/json/parser_test.go +++ b/plugins/parsers/json/parser_test.go @@ -27,7 +27,11 @@ const validJSONTags = ` "c": 6 }, "mytag": "foobar", - "othertag": "baz" + "othertag": "baz", + "tags_object": { + "mytag": "foobar", + "othertag": "baz" + } } ` @@ -39,7 +43,16 @@ const validJSONArrayTags = ` "c": 6 }, "mytag": "foo", - "othertag": "baz" + "othertag": "baz", + "tags_array": [ + { + "mytag": "foo" + }, + { + "othertag": "baz" + } + ], + "anothert": "foo" }, { "a": 7, @@ -47,7 +60,16 @@ const validJSONArrayTags = ` "c": 8 }, "mytag": "bar", - "othertag": "baz" + "othertag": "baz", + "tags_array": [ + { + "mytag": "bar" + }, + { + "othertag": "baz" + } + ], + "anothert": "bar" } ] ` @@ -948,3 +970,304 @@ func TestParse(t *testing.T) { }) } } + +func TestParseWithWildcardTagKeys(t *testing.T) { + // Test that wildcard matching with tags nested within object works + parser, err := New(&Config{ + MetricName: "json_test", + TagKeys: []string{"tags_object_*"}, + }) + require.NoError(t, err) + metrics, err := parser.Parse([]byte(validJSONTags)) + require.NoError(t, err) + require.Len(t, metrics, 1) + require.Equal(t, "json_test", metrics[0].Name()) + require.Equal(t, map[string]interface{}{ + "a": float64(5), + "b_c": float64(6), + }, metrics[0].Fields()) + require.Equal(t, map[string]string{ + "tags_object_mytag": "foobar", + "tags_object_othertag": "baz", + }, metrics[0].Tags()) + + // Test that wildcard matching with keys containing tag works + parser, err = New(&Config{ + MetricName: "json_test", + TagKeys: []string{"*tag"}, + }) + require.NoError(t, err) + metrics, err = parser.Parse([]byte(validJSONTags)) + require.NoError(t, err) + require.Len(t, metrics, 1) + require.Equal(t, "json_test", metrics[0].Name()) + require.Equal(t, map[string]interface{}{ + "a": float64(5), + "b_c": float64(6), + }, metrics[0].Fields()) + require.Equal(t, map[string]string{ + "mytag": "foobar", + "othertag": "baz", + "tags_object_mytag": "foobar", + "tags_object_othertag": "baz", + }, metrics[0].Tags()) + + // Test that strings not matching tag keys are still also ignored + parser, err = New(&Config{ + MetricName: "json_test", + TagKeys: []string{"wrongtagkey", "tags_object_*"}, + }) + require.NoError(t, err) + + metrics, err = parser.Parse([]byte(validJSONTags)) + require.NoError(t, err) + require.Len(t, metrics, 1) + require.Equal(t, "json_test", metrics[0].Name()) + require.Equal(t, map[string]interface{}{ + "a": float64(5), + "b_c": float64(6), + }, metrics[0].Fields()) + require.NotContains(t, map[string]string{"wrongtagkey": ""}, metrics[0].Tags()) + require.Equal(t, map[string]string{ + "tags_object_mytag": "foobar", + "tags_object_othertag": "baz", + }, metrics[0].Tags()) + + // test that single tag key is also found and applied + parser, err = New(&Config{ + MetricName: "json_test", + TagKeys: []string{"mytag", "tags_object_*"}, + }) + require.NoError(t, err) + + metrics, err = parser.Parse([]byte(validJSONTags)) + require.NoError(t, err) + require.Len(t, metrics, 1) + require.Equal(t, "json_test", metrics[0].Name()) + require.Equal(t, map[string]interface{}{ + "a": float64(5), + "b_c": float64(6), + }, metrics[0].Fields()) + require.Equal(t, map[string]string{ + "mytag": "foobar", + "tags_object_mytag": "foobar", + "tags_object_othertag": "baz", + }, metrics[0].Tags()) +} + +func TestParseLineWithWildcardTagKeys(t *testing.T) { + // Test that wildcard matching with tags nested within object works + parser, err := New(&Config{ + MetricName: "json_test", + TagKeys: []string{"tags_object_*"}, + }) + require.NoError(t, err) + metrics, err := parser.ParseLine(validJSONTags) + require.NoError(t, err) + require.Equal(t, "json_test", metrics.Name()) + require.Equal(t, map[string]interface{}{ + "a": float64(5), + "b_c": float64(6), + }, metrics.Fields()) + require.Equal(t, map[string]string{ + "tags_object_mytag": "foobar", + "tags_object_othertag": "baz", + }, metrics.Tags()) + + // Test that wildcard matching with keys containing tag works + parser, err = New(&Config{ + MetricName: "json_test", + TagKeys: []string{"*tag"}, + }) + require.NoError(t, err) + metrics, err = parser.ParseLine(validJSONTags) + require.NoError(t, err) + require.Equal(t, "json_test", metrics.Name()) + require.Equal(t, map[string]interface{}{ + "a": float64(5), + "b_c": float64(6), + }, metrics.Fields()) + require.Equal(t, map[string]string{ + "mytag": "foobar", + "othertag": "baz", + "tags_object_mytag": "foobar", + "tags_object_othertag": "baz", + }, metrics.Tags()) + + // Test that strings not matching tag keys are still also ignored + parser, err = New(&Config{ + MetricName: "json_test", + TagKeys: []string{"wrongtagkey", "tags_object_*"}, + }) + require.NoError(t, err) + + metrics, err = parser.ParseLine(validJSONTags) + require.NoError(t, err) + require.Equal(t, "json_test", metrics.Name()) + require.Equal(t, map[string]interface{}{ + "a": float64(5), + "b_c": float64(6), + }, metrics.Fields()) + require.NotContains(t, map[string]string{"wrongtagkey": ""}, metrics.Tags()) + require.Equal(t, map[string]string{ + "tags_object_mytag": "foobar", + "tags_object_othertag": "baz", + }, metrics.Tags()) + + // test that single tag key is also found and applied + parser, err = New(&Config{ + MetricName: "json_test", + TagKeys: []string{"mytag", "tags_object_*"}, + }) + require.NoError(t, err) + + metrics, err = parser.ParseLine(validJSONTags) + require.NoError(t, err) + require.Equal(t, "json_test", metrics.Name()) + require.Equal(t, map[string]interface{}{ + "a": float64(5), + "b_c": float64(6), + }, metrics.Fields()) + require.Equal(t, map[string]string{ + "mytag": "foobar", + "tags_object_mytag": "foobar", + "tags_object_othertag": "baz", + }, metrics.Tags()) + +} + +func TestParseArrayWithWildcardTagKeys(t *testing.T) { + // Test that wildcard matching with keys containing tag within array works + parser, err := New(&Config{ + MetricName: "json_array_test", + TagKeys: []string{"*tag"}, + }) + require.NoError(t, err) + metrics, err := parser.Parse([]byte(validJSONArrayTags)) + require.NoError(t, err) + require.Len(t, metrics, 2) + require.Equal(t, "json_array_test", metrics[0].Name()) + require.Equal(t, map[string]interface{}{ + "a": float64(5), + "b_c": float64(6), + }, metrics[0].Fields()) + require.Equal(t, map[string]string{ + "mytag": "foo", + "othertag": "baz", + "tags_array_0_mytag": "foo", + "tags_array_1_othertag": "baz", + }, metrics[0].Tags()) + + require.Equal(t, "json_array_test", metrics[1].Name()) + require.Equal(t, map[string]interface{}{ + "a": float64(7), + "b_c": float64(8), + }, metrics[1].Fields()) + require.Equal(t, map[string]string{ + "mytag": "bar", + "othertag": "baz", + "tags_array_0_mytag": "bar", + "tags_array_1_othertag": "baz", + }, metrics[1].Tags()) + + // test that wildcard matching with tags nested array within object works + parser, err = New(&Config{ + MetricName: "json_array_test", + TagKeys: []string{"tags_array_*"}, + }) + require.NoError(t, err) + metrics, err = parser.Parse([]byte(validJSONArrayTags)) + require.NoError(t, err) + require.Len(t, metrics, 2) + require.Equal(t, "json_array_test", metrics[0].Name()) + require.Equal(t, map[string]interface{}{ + "a": float64(5), + "b_c": float64(6), + }, metrics[0].Fields()) + require.Equal(t, map[string]string{ + "tags_array_0_mytag": "foo", + "tags_array_1_othertag": "baz", + }, metrics[0].Tags()) + + require.Equal(t, "json_array_test", metrics[1].Name()) + require.Equal(t, map[string]interface{}{ + "a": float64(7), + "b_c": float64(8), + }, metrics[1].Fields()) + require.Equal(t, map[string]string{ + "tags_array_0_mytag": "bar", + "tags_array_1_othertag": "baz", + }, metrics[1].Tags()) + + // Test that strings not matching tag keys are still also ignored + parser, err = New(&Config{ + MetricName: "json_array_test", + TagKeys: []string{"mytag", "*tag"}, + }) + require.NoError(t, err) + metrics, err = parser.Parse([]byte(validJSONArrayTags)) + require.NoError(t, err) + require.Len(t, metrics, 2) + require.Equal(t, "json_array_test", metrics[0].Name()) + require.Equal(t, map[string]interface{}{ + "a": float64(5), + "b_c": float64(6), + }, metrics[0].Fields()) + require.NotContains(t, map[string]string{"notme": ""}, metrics[0].Tags()) + require.Equal(t, map[string]string{ + "mytag": "foo", + "othertag": "baz", + "tags_array_0_mytag": "foo", + "tags_array_1_othertag": "baz", + }, metrics[0].Tags()) + + require.Equal(t, "json_array_test", metrics[1].Name()) + require.Equal(t, map[string]interface{}{ + "a": float64(7), + "b_c": float64(8), + }, metrics[1].Fields()) + require.NotContains(t, map[string]string{"notme": ""}, metrics[1].Tags()) + require.Equal(t, map[string]string{ + "mytag": "bar", + "othertag": "baz", + "tags_array_0_mytag": "bar", + "tags_array_1_othertag": "baz", + }, metrics[1].Tags()) + + // test that single tag key is also found and applied + parser, err = New(&Config{ + MetricName: "json_array_test", + TagKeys: []string{"anothert", "*tag"}, + }) + require.NoError(t, err) + + metrics, err = parser.Parse([]byte(validJSONArrayTags)) + require.NoError(t, err) + require.Len(t, metrics, 2) + require.Equal(t, "json_array_test", metrics[0].Name()) + require.Equal(t, map[string]interface{}{ + "a": float64(5), + "b_c": float64(6), + }, metrics[0].Fields()) + require.Equal(t, map[string]string{ + "anothert": "foo", + "mytag": "foo", + "othertag": "baz", + "tags_array_0_mytag": "foo", + "tags_array_1_othertag": "baz", + }, metrics[0].Tags()) + + require.Equal(t, "json_array_test", metrics[1].Name()) + require.Equal(t, map[string]interface{}{ + "a": float64(7), + "b_c": float64(8), + }, metrics[1].Fields()) + require.Equal(t, map[string]string{ + "anothert": "bar", + "mytag": "bar", + "othertag": "baz", + "tags_array_0_mytag": "bar", + "tags_array_1_othertag": "baz", + }, metrics[1].Tags()) + +} diff --git a/telegraf.conf b/telegraf.conf new file mode 100644 index 0000000000000..2528bcfa13edb --- /dev/null +++ b/telegraf.conf @@ -0,0 +1,17 @@ +[agent] + interval = "1s" + # debug = true + omit_hostname = true + +[[outputs.file]] + files = ["stdout", "./examples/output"] + data_format = "influx" + + +[[inputs.file]] + files= ["./examples/json-parse"] + data_format = "json" + json_name_key = "name" + json_string_fields = ["fields"] + tag_keys = ["tags_*"] + From ea6a0ec463dc203388dcf8631167b47b98036789 Mon Sep 17 00:00:00 2001 From: helenosheaa <38860767+helenosheaa@users.noreply.github.com> Date: Wed, 16 Dec 2020 15:36:45 -0500 Subject: [PATCH 02/10] Removed config --- telegraf.conf | 17 ----------------- 1 file changed, 17 deletions(-) delete mode 100644 telegraf.conf diff --git a/telegraf.conf b/telegraf.conf deleted file mode 100644 index 2528bcfa13edb..0000000000000 --- a/telegraf.conf +++ /dev/null @@ -1,17 +0,0 @@ -[agent] - interval = "1s" - # debug = true - omit_hostname = true - -[[outputs.file]] - files = ["stdout", "./examples/output"] - data_format = "influx" - - -[[inputs.file]] - files= ["./examples/json-parse"] - data_format = "json" - json_name_key = "name" - json_string_fields = ["fields"] - tag_keys = ["tags_*"] - From d88ec6ea57754923f14641ccc0a0039371f48efa Mon Sep 17 00:00:00 2001 From: helenosheaa <38860767+helenosheaa@users.noreply.github.com> Date: Wed, 16 Dec 2020 15:39:50 -0500 Subject: [PATCH 03/10] Removed examples --- .../random_number/random_number.go | 43 ------------------- examples/json-parse | 16 ------- 2 files changed, 59 deletions(-) delete mode 100644 examples/input-plugins/random_number/random_number.go delete mode 100644 examples/json-parse diff --git a/examples/input-plugins/random_number/random_number.go b/examples/input-plugins/random_number/random_number.go deleted file mode 100644 index a9fc000c74718..0000000000000 --- a/examples/input-plugins/random_number/random_number.go +++ /dev/null @@ -1,43 +0,0 @@ -package random_number - -import ( - "fmt" - "math/rand" - - "github.com/influxdata/telegraf" - "github.com/influxdata/telegraf/plugins/inputs" -) - -// RandomNumber struct should be named the same as the Plugin -type RandomNumber struct { - Min int - Max int -} - -// Description will appear directly above the plugin definition in the config file -func (r *RandomNumber) Description() string { - return `This is a random number generator plugin which takes in a min and a max` -} - -// SampleConfig will populate the sample configuration portion of the plugin's configuration -func (r *RandomNumber) SampleConfig() string { - return ` Min = 1 Max = 10000` -} - -// Init can be implemented to do one-time processing stuff like initializing variables -// func (r *RandomNumber) Init() error { -// r.randomNumber = rand.Int() -// return nil -// } - -// Gather defines what data the plugin will gather. -func (r *RandomNumber) Gather(acc telegraf.Accumulator) error { - randomNumber := rand.Intn(r.Max-r.Min) + r.Min - fmt.Println("random", randomNumber) - acc.AddFields("state", map[string]interface{}{"value": randomNumber}, nil) - return nil -} - -func init() { - inputs.Add("random_number", func() telegraf.Input { return &RandomNumber{Min: 1, Max: 100000} }) -} diff --git a/examples/json-parse b/examples/json-parse deleted file mode 100644 index 2662c6ef15c0c..0000000000000 --- a/examples/json-parse +++ /dev/null @@ -1,16 +0,0 @@ -{ - "fields": { - "value": 2 - }, - "name": "metrics_name.test", - "tags": { - "InstanceId": "i-123", - "app": "test", - "availability-zone": "ap-southeast-1z", - "cluster": "dev", - "env": "development", - "host": "ip-10.compute.internal", - "metric_type": "counter" - } -} - From f54f4472d23ac61fe266f61a1882dc016de0300c Mon Sep 17 00:00:00 2001 From: helenosheaa <38860767+helenosheaa@users.noreply.github.com> Date: Wed, 16 Dec 2020 15:44:51 -0500 Subject: [PATCH 04/10] return err --- plugins/parsers/json/parser.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/parsers/json/parser.go b/plugins/parsers/json/parser.go index f678592e97c8c..239f4c261fa94 100644 --- a/plugins/parsers/json/parser.go +++ b/plugins/parsers/json/parser.go @@ -51,13 +51,13 @@ func New(config *Config) (*Parser, error) { stringFilter, err := filter.Compile(config.StringFields) if err != nil { - return nil, nil + return nil, err } tagKeyFilter, err := filter.Compile(config.TagKeys) if err != nil { - return nil, nil + return nil, err } return &Parser{ From 6adb5e3fd8363f2f30bdd9c2f691eac543d9a6d7 Mon Sep 17 00:00:00 2001 From: helenosheaa <38860767+helenosheaa@users.noreply.github.com> Date: Wed, 16 Dec 2020 17:39:35 -0500 Subject: [PATCH 05/10] fixed spacing issues --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 9b73f5f391b33..df2b3d06643c5 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,4 @@ /telegraf.exe /telegraf.gz /vendor -.DS_Store \ No newline at end of file +.DS_Store From 7d0ef3167db0c2851f5fa5c084d68dd8806af669 Mon Sep 17 00:00:00 2001 From: helenosheaa <38860767+helenosheaa@users.noreply.github.com> Date: Wed, 16 Dec 2020 17:41:04 -0500 Subject: [PATCH 06/10] fixed spacing issues --- plugins/parsers/json/README.md | 2 +- plugins/parsers/json/parser.go | 4 +--- plugins/parsers/json/parser_test.go | 6 +++--- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/plugins/parsers/json/README.md b/plugins/parsers/json/README.md index 307aab94c5483..18a7f0ac2503a 100644 --- a/plugins/parsers/json/README.md +++ b/plugins/parsers/json/README.md @@ -30,7 +30,7 @@ ignored unless specified in the `tag_key` or `json_string_fields` options. json_query = "" ## Tag keys is an array of keys that should be added as tags. Matching keys - ## are no longer saved as fields. Now supports wildcard glob matching. + ## are no longer saved as fields. Supports wildcard glob matching. tag_keys = [ "my_tag_1", "my_tag_2", diff --git a/plugins/parsers/json/parser.go b/plugins/parsers/json/parser.go index 239f4c261fa94..a1bac37b92e27 100644 --- a/plugins/parsers/json/parser.go +++ b/plugins/parsers/json/parser.go @@ -49,13 +49,11 @@ type Parser struct { func New(config *Config) (*Parser, error) { stringFilter, err := filter.Compile(config.StringFields) - if err != nil { return nil, err } tagKeyFilter, err := filter.Compile(config.TagKeys) - if err != nil { return nil, err } @@ -162,7 +160,7 @@ func (p *Parser) switchFieldToTag(tags map[string]string, fields map[string]inte if p.tagKeys == nil { continue } - // skip switch statement if tagkey doesn't match fieldname + //skip switch statement if tagkey doesn't match fieldname if !p.tagKeys.Match(name) { continue } diff --git a/plugins/parsers/json/parser_test.go b/plugins/parsers/json/parser_test.go index b8117a8ea8289..8b2b2c1de1adf 100644 --- a/plugins/parsers/json/parser_test.go +++ b/plugins/parsers/json/parser_test.go @@ -24,13 +24,13 @@ const validJSONTags = ` { "a": 5, "b": { - "c": 6 + "c": 6 }, "mytag": "foobar", "othertag": "baz", "tags_object": { - "mytag": "foobar", - "othertag": "baz" + "mytag": "foobar", + "othertag": "baz" } } ` From 32e413edcade322a8a9a40f8c7a622ec47326e8e Mon Sep 17 00:00:00 2001 From: helenosheaa <38860767+helenosheaa@users.noreply.github.com> Date: Thu, 17 Dec 2020 11:02:16 -0500 Subject: [PATCH 07/10] refactored tests --- plugins/parsers/json/parser.go | 2 +- plugins/parsers/json/parser_test.go | 656 ++++++++++++++++------------ 2 files changed, 373 insertions(+), 285 deletions(-) diff --git a/plugins/parsers/json/parser.go b/plugins/parsers/json/parser.go index a1bac37b92e27..5228b1bef2894 100644 --- a/plugins/parsers/json/parser.go +++ b/plugins/parsers/json/parser.go @@ -180,7 +180,7 @@ func (p *Parser) switchFieldToTag(tags map[string]string, fields map[string]inte } } - // remove any additional string/bool values from fields + //remove any additional string/bool values from fields for fk := range fields { switch fields[fk].(type) { case string, bool: diff --git a/plugins/parsers/json/parser_test.go b/plugins/parsers/json/parser_test.go index 8b2b2c1de1adf..d435254246b42 100644 --- a/plugins/parsers/json/parser_test.go +++ b/plugins/parsers/json/parser_test.go @@ -972,302 +972,390 @@ func TestParse(t *testing.T) { } func TestParseWithWildcardTagKeys(t *testing.T) { - // Test that wildcard matching with tags nested within object works - parser, err := New(&Config{ - MetricName: "json_test", - TagKeys: []string{"tags_object_*"}, - }) - require.NoError(t, err) - metrics, err := parser.Parse([]byte(validJSONTags)) - require.NoError(t, err) - require.Len(t, metrics, 1) - require.Equal(t, "json_test", metrics[0].Name()) - require.Equal(t, map[string]interface{}{ - "a": float64(5), - "b_c": float64(6), - }, metrics[0].Fields()) - require.Equal(t, map[string]string{ - "tags_object_mytag": "foobar", - "tags_object_othertag": "baz", - }, metrics[0].Tags()) - - // Test that wildcard matching with keys containing tag works - parser, err = New(&Config{ - MetricName: "json_test", - TagKeys: []string{"*tag"}, - }) - require.NoError(t, err) - metrics, err = parser.Parse([]byte(validJSONTags)) - require.NoError(t, err) - require.Len(t, metrics, 1) - require.Equal(t, "json_test", metrics[0].Name()) - require.Equal(t, map[string]interface{}{ - "a": float64(5), - "b_c": float64(6), - }, metrics[0].Fields()) - require.Equal(t, map[string]string{ - "mytag": "foobar", - "othertag": "baz", - "tags_object_mytag": "foobar", - "tags_object_othertag": "baz", - }, metrics[0].Tags()) - - // Test that strings not matching tag keys are still also ignored - parser, err = New(&Config{ - MetricName: "json_test", - TagKeys: []string{"wrongtagkey", "tags_object_*"}, - }) - require.NoError(t, err) - - metrics, err = parser.Parse([]byte(validJSONTags)) - require.NoError(t, err) - require.Len(t, metrics, 1) - require.Equal(t, "json_test", metrics[0].Name()) - require.Equal(t, map[string]interface{}{ - "a": float64(5), - "b_c": float64(6), - }, metrics[0].Fields()) - require.NotContains(t, map[string]string{"wrongtagkey": ""}, metrics[0].Tags()) - require.Equal(t, map[string]string{ - "tags_object_mytag": "foobar", - "tags_object_othertag": "baz", - }, metrics[0].Tags()) - - // test that single tag key is also found and applied - parser, err = New(&Config{ - MetricName: "json_test", - TagKeys: []string{"mytag", "tags_object_*"}, - }) - require.NoError(t, err) + var tests = []struct { + name string + config *Config + input []byte + expected []telegraf.Metric + }{ + { + name: "", + config: &Config{ + MetricName: "json_test", + TagKeys: []string{"tags_object_*"}, + }, + input: []byte(validJSONTags), + expected: []telegraf.Metric{ + testutil.MustMetric( + "json_test", + map[string]string{ + "tags_object_mytag": "foobar", + "tags_object_othertag": "baz", + }, + map[string]interface{}{ + "a": float64(5), + "b_c": float64(6), + }, + time.Unix(0, 0), + ), + }, + }, + { + name: "", + config: &Config{ + MetricName: "json_test", + TagKeys: []string{"*tag"}, + }, + input: []byte(validJSONTags), + expected: []telegraf.Metric{ + testutil.MustMetric( + "json_test", + map[string]string{ + "mytag": "foobar", + "othertag": "baz", + "tags_object_mytag": "foobar", + "tags_object_othertag": "baz", + }, + map[string]interface{}{ + "a": float64(5), + "b_c": float64(6), + }, + time.Unix(0, 0), + ), + }, + }, + { + name: "", + config: &Config{ + MetricName: "json_test", + TagKeys: []string{"wrongtagkey", "tags_object_*"}, + }, + input: []byte(validJSONTags), + expected: []telegraf.Metric{ + testutil.MustMetric( + "json_test", + map[string]string{ + "tags_object_mytag": "foobar", + "tags_object_othertag": "baz", + }, + map[string]interface{}{ + "a": float64(5), + "b_c": float64(6), + }, + time.Unix(0, 0), + ), + }, + }, + { + name: "", + config: &Config{ + MetricName: "json_test", + TagKeys: []string{"mytag", "tags_object_*"}, + }, + input: []byte(validJSONTags), + expected: []telegraf.Metric{ + testutil.MustMetric( + "json_test", + map[string]string{ + "mytag": "foobar", + "tags_object_mytag": "foobar", + "tags_object_othertag": "baz", + }, + map[string]interface{}{ + "a": float64(5), + "b_c": float64(6), + }, + time.Unix(0, 0), + ), + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + parser, err := New(tt.config) + require.NoError(t, err) - metrics, err = parser.Parse([]byte(validJSONTags)) - require.NoError(t, err) - require.Len(t, metrics, 1) - require.Equal(t, "json_test", metrics[0].Name()) - require.Equal(t, map[string]interface{}{ - "a": float64(5), - "b_c": float64(6), - }, metrics[0].Fields()) - require.Equal(t, map[string]string{ - "mytag": "foobar", - "tags_object_mytag": "foobar", - "tags_object_othertag": "baz", - }, metrics[0].Tags()) + actual, err := parser.Parse(tt.input) + require.NoError(t, err) + testutil.RequireMetricsEqual(t, tt.expected, actual, testutil.IgnoreTime()) + }) + } } func TestParseLineWithWildcardTagKeys(t *testing.T) { - // Test that wildcard matching with tags nested within object works - parser, err := New(&Config{ - MetricName: "json_test", - TagKeys: []string{"tags_object_*"}, - }) - require.NoError(t, err) - metrics, err := parser.ParseLine(validJSONTags) - require.NoError(t, err) - require.Equal(t, "json_test", metrics.Name()) - require.Equal(t, map[string]interface{}{ - "a": float64(5), - "b_c": float64(6), - }, metrics.Fields()) - require.Equal(t, map[string]string{ - "tags_object_mytag": "foobar", - "tags_object_othertag": "baz", - }, metrics.Tags()) - - // Test that wildcard matching with keys containing tag works - parser, err = New(&Config{ - MetricName: "json_test", - TagKeys: []string{"*tag"}, - }) - require.NoError(t, err) - metrics, err = parser.ParseLine(validJSONTags) - require.NoError(t, err) - require.Equal(t, "json_test", metrics.Name()) - require.Equal(t, map[string]interface{}{ - "a": float64(5), - "b_c": float64(6), - }, metrics.Fields()) - require.Equal(t, map[string]string{ - "mytag": "foobar", - "othertag": "baz", - "tags_object_mytag": "foobar", - "tags_object_othertag": "baz", - }, metrics.Tags()) - - // Test that strings not matching tag keys are still also ignored - parser, err = New(&Config{ - MetricName: "json_test", - TagKeys: []string{"wrongtagkey", "tags_object_*"}, - }) - require.NoError(t, err) - - metrics, err = parser.ParseLine(validJSONTags) - require.NoError(t, err) - require.Equal(t, "json_test", metrics.Name()) - require.Equal(t, map[string]interface{}{ - "a": float64(5), - "b_c": float64(6), - }, metrics.Fields()) - require.NotContains(t, map[string]string{"wrongtagkey": ""}, metrics.Tags()) - require.Equal(t, map[string]string{ - "tags_object_mytag": "foobar", - "tags_object_othertag": "baz", - }, metrics.Tags()) + var tests = []struct { + name string + config *Config + input string + expected telegraf.Metric + }{ + { + name: "", + config: &Config{ + MetricName: "json_test", + TagKeys: []string{"tags_object_*"}, + }, + input: validJSONTags, + expected: testutil.MustMetric( + "json_test", + map[string]string{ + "tags_object_mytag": "foobar", + "tags_object_othertag": "baz", + }, + map[string]interface{}{ + "a": float64(5), + "b_c": float64(6), + }, + time.Unix(0, 0), + ), + }, + { + name: "", + config: &Config{ + MetricName: "json_test", + TagKeys: []string{"*tag"}, + }, + input: validJSONTags, + expected: testutil.MustMetric( + "json_test", + map[string]string{ + "mytag": "foobar", + "othertag": "baz", + "tags_object_mytag": "foobar", + "tags_object_othertag": "baz", + }, + map[string]interface{}{ + "a": float64(5), + "b_c": float64(6), + }, + time.Unix(0, 0), + ), + }, + { + name: "", + config: &Config{ + MetricName: "json_test", + TagKeys: []string{"wrongtagkey", "tags_object_*"}, + }, + input: validJSONTags, + expected: testutil.MustMetric( + "json_test", + map[string]string{ + "tags_object_mytag": "foobar", + "tags_object_othertag": "baz", + }, + map[string]interface{}{ + "a": float64(5), + "b_c": float64(6), + }, + time.Unix(0, 0), + ), + }, + { + name: "", + config: &Config{ + MetricName: "json_test", + TagKeys: []string{"mytag", "tags_object_*"}, + }, + input: validJSONTags, + expected: testutil.MustMetric( + "json_test", + map[string]string{ + "mytag": "foobar", + "tags_object_mytag": "foobar", + "tags_object_othertag": "baz", + }, + map[string]interface{}{ + "a": float64(5), + "b_c": float64(6), + }, + time.Unix(0, 0), + ), + }, + } - // test that single tag key is also found and applied - parser, err = New(&Config{ - MetricName: "json_test", - TagKeys: []string{"mytag", "tags_object_*"}, - }) - require.NoError(t, err) + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + parser, err := New(tt.config) + require.NoError(t, err) - metrics, err = parser.ParseLine(validJSONTags) - require.NoError(t, err) - require.Equal(t, "json_test", metrics.Name()) - require.Equal(t, map[string]interface{}{ - "a": float64(5), - "b_c": float64(6), - }, metrics.Fields()) - require.Equal(t, map[string]string{ - "mytag": "foobar", - "tags_object_mytag": "foobar", - "tags_object_othertag": "baz", - }, metrics.Tags()) + actual, err := parser.ParseLine(tt.input) + require.NoError(t, err) + testutil.RequireMetricEqual(t, tt.expected, actual, testutil.IgnoreTime()) + }) + } } func TestParseArrayWithWildcardTagKeys(t *testing.T) { - // Test that wildcard matching with keys containing tag within array works - parser, err := New(&Config{ - MetricName: "json_array_test", - TagKeys: []string{"*tag"}, - }) - require.NoError(t, err) - metrics, err := parser.Parse([]byte(validJSONArrayTags)) - require.NoError(t, err) - require.Len(t, metrics, 2) - require.Equal(t, "json_array_test", metrics[0].Name()) - require.Equal(t, map[string]interface{}{ - "a": float64(5), - "b_c": float64(6), - }, metrics[0].Fields()) - require.Equal(t, map[string]string{ - "mytag": "foo", - "othertag": "baz", - "tags_array_0_mytag": "foo", - "tags_array_1_othertag": "baz", - }, metrics[0].Tags()) - - require.Equal(t, "json_array_test", metrics[1].Name()) - require.Equal(t, map[string]interface{}{ - "a": float64(7), - "b_c": float64(8), - }, metrics[1].Fields()) - require.Equal(t, map[string]string{ - "mytag": "bar", - "othertag": "baz", - "tags_array_0_mytag": "bar", - "tags_array_1_othertag": "baz", - }, metrics[1].Tags()) - - // test that wildcard matching with tags nested array within object works - parser, err = New(&Config{ - MetricName: "json_array_test", - TagKeys: []string{"tags_array_*"}, - }) - require.NoError(t, err) - metrics, err = parser.Parse([]byte(validJSONArrayTags)) - require.NoError(t, err) - require.Len(t, metrics, 2) - require.Equal(t, "json_array_test", metrics[0].Name()) - require.Equal(t, map[string]interface{}{ - "a": float64(5), - "b_c": float64(6), - }, metrics[0].Fields()) - require.Equal(t, map[string]string{ - "tags_array_0_mytag": "foo", - "tags_array_1_othertag": "baz", - }, metrics[0].Tags()) - - require.Equal(t, "json_array_test", metrics[1].Name()) - require.Equal(t, map[string]interface{}{ - "a": float64(7), - "b_c": float64(8), - }, metrics[1].Fields()) - require.Equal(t, map[string]string{ - "tags_array_0_mytag": "bar", - "tags_array_1_othertag": "baz", - }, metrics[1].Tags()) - - // Test that strings not matching tag keys are still also ignored - parser, err = New(&Config{ - MetricName: "json_array_test", - TagKeys: []string{"mytag", "*tag"}, - }) - require.NoError(t, err) - metrics, err = parser.Parse([]byte(validJSONArrayTags)) - require.NoError(t, err) - require.Len(t, metrics, 2) - require.Equal(t, "json_array_test", metrics[0].Name()) - require.Equal(t, map[string]interface{}{ - "a": float64(5), - "b_c": float64(6), - }, metrics[0].Fields()) - require.NotContains(t, map[string]string{"notme": ""}, metrics[0].Tags()) - require.Equal(t, map[string]string{ - "mytag": "foo", - "othertag": "baz", - "tags_array_0_mytag": "foo", - "tags_array_1_othertag": "baz", - }, metrics[0].Tags()) - - require.Equal(t, "json_array_test", metrics[1].Name()) - require.Equal(t, map[string]interface{}{ - "a": float64(7), - "b_c": float64(8), - }, metrics[1].Fields()) - require.NotContains(t, map[string]string{"notme": ""}, metrics[1].Tags()) - require.Equal(t, map[string]string{ - "mytag": "bar", - "othertag": "baz", - "tags_array_0_mytag": "bar", - "tags_array_1_othertag": "baz", - }, metrics[1].Tags()) + var tests = []struct { + name string + config *Config + input []byte + expected []telegraf.Metric + }{ + { + name: "", + config: &Config{ + MetricName: "json_array_test", + TagKeys: []string{"*tag"}, + }, + input: []byte(validJSONArrayTags), + expected: []telegraf.Metric{ + testutil.MustMetric( + "json_array_test", + map[string]string{ + "mytag": "foo", + "othertag": "baz", + "tags_array_0_mytag": "foo", + "tags_array_1_othertag": "baz", + }, + map[string]interface{}{ + "a": float64(5), + "b_c": float64(6), + }, + time.Unix(0, 0), + ), + testutil.MustMetric( + "json_array_test", + map[string]string{ + "mytag": "bar", + "othertag": "baz", + "tags_array_0_mytag": "bar", + "tags_array_1_othertag": "baz", + }, + map[string]interface{}{ + "a": float64(7), + "b_c": float64(8), + }, + time.Unix(0, 0), + ), + }, + }, + { + name: "", + config: &Config{ + MetricName: "json_array_test", + TagKeys: []string{"tags_array_*"}, + }, + input: []byte(validJSONArrayTags), + expected: []telegraf.Metric{ + testutil.MustMetric( + "json_array_test", + map[string]string{ + "tags_array_0_mytag": "foo", + "tags_array_1_othertag": "baz", + }, + map[string]interface{}{ + "a": float64(5), + "b_c": float64(6), + }, + time.Unix(0, 0), + ), + testutil.MustMetric( + "json_array_test", + map[string]string{ + "tags_array_0_mytag": "bar", + "tags_array_1_othertag": "baz", + }, + map[string]interface{}{ + "a": float64(7), + "b_c": float64(8), + }, + time.Unix(0, 0), + ), + }, + }, + { + name: "", + config: &Config{ + MetricName: "json_array_test", + TagKeys: []string{"mytag", "*tag"}, + }, + input: []byte(validJSONArrayTags), + expected: []telegraf.Metric{ + testutil.MustMetric( + "json_array_test", + map[string]string{ + "mytag": "foo", + "othertag": "baz", + "tags_array_0_mytag": "foo", + "tags_array_1_othertag": "baz", + }, + map[string]interface{}{ + "a": float64(5), + "b_c": float64(6), + }, + time.Unix(0, 0), + ), + testutil.MustMetric( + "json_array_test", + map[string]string{ + "mytag": "bar", + "othertag": "baz", + "tags_array_0_mytag": "bar", + "tags_array_1_othertag": "baz", + }, + map[string]interface{}{ + "a": float64(7), + "b_c": float64(8), + }, + time.Unix(0, 0), + ), + }, + }, + { + name: "", + config: &Config{ + MetricName: "json_array_test", + TagKeys: []string{"anothert", "*tag"}, + }, + input: []byte(validJSONArrayTags), + expected: []telegraf.Metric{ + testutil.MustMetric( + "json_array_test", + map[string]string{ + "anothert": "foo", + "mytag": "foo", + "othertag": "baz", + "tags_array_0_mytag": "foo", + "tags_array_1_othertag": "baz", + }, + map[string]interface{}{ + "a": float64(5), + "b_c": float64(6), + }, + time.Unix(0, 0), + ), + testutil.MustMetric( + "json_array_test", + map[string]string{ + "anothert": "bar", + "mytag": "bar", + "othertag": "baz", + "tags_array_0_mytag": "bar", + "tags_array_1_othertag": "baz", + }, + map[string]interface{}{ + "a": float64(7), + "b_c": float64(8), + }, + time.Unix(0, 0), + ), + }, + }, + } - // test that single tag key is also found and applied - parser, err = New(&Config{ - MetricName: "json_array_test", - TagKeys: []string{"anothert", "*tag"}, - }) - require.NoError(t, err) + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + parser, err := New(tt.config) + require.NoError(t, err) - metrics, err = parser.Parse([]byte(validJSONArrayTags)) - require.NoError(t, err) - require.Len(t, metrics, 2) - require.Equal(t, "json_array_test", metrics[0].Name()) - require.Equal(t, map[string]interface{}{ - "a": float64(5), - "b_c": float64(6), - }, metrics[0].Fields()) - require.Equal(t, map[string]string{ - "anothert": "foo", - "mytag": "foo", - "othertag": "baz", - "tags_array_0_mytag": "foo", - "tags_array_1_othertag": "baz", - }, metrics[0].Tags()) + actual, err := parser.Parse(tt.input) + require.NoError(t, err) - require.Equal(t, "json_array_test", metrics[1].Name()) - require.Equal(t, map[string]interface{}{ - "a": float64(7), - "b_c": float64(8), - }, metrics[1].Fields()) - require.Equal(t, map[string]string{ - "anothert": "bar", - "mytag": "bar", - "othertag": "baz", - "tags_array_0_mytag": "bar", - "tags_array_1_othertag": "baz", - }, metrics[1].Tags()) + testutil.RequireMetricsEqual(t, tt.expected, actual, testutil.IgnoreTime()) + }) + } } From ce9b3fa2cf4f0bcf61a8f761be1abe951fc92836 Mon Sep 17 00:00:00 2001 From: helenosheaa <38860767+helenosheaa@users.noreply.github.com> Date: Thu, 17 Dec 2020 11:08:06 -0500 Subject: [PATCH 08/10] add test names --- plugins/parsers/json/parser_test.go | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/plugins/parsers/json/parser_test.go b/plugins/parsers/json/parser_test.go index d435254246b42..7e052270bfa2a 100644 --- a/plugins/parsers/json/parser_test.go +++ b/plugins/parsers/json/parser_test.go @@ -979,7 +979,7 @@ func TestParseWithWildcardTagKeys(t *testing.T) { expected []telegraf.Metric }{ { - name: "", + name: "wildcard matching with tags nested within object", config: &Config{ MetricName: "json_test", TagKeys: []string{"tags_object_*"}, @@ -1001,7 +1001,7 @@ func TestParseWithWildcardTagKeys(t *testing.T) { }, }, { - name: "", + name: "wildcard matching with keys containing tag", config: &Config{ MetricName: "json_test", TagKeys: []string{"*tag"}, @@ -1025,7 +1025,7 @@ func TestParseWithWildcardTagKeys(t *testing.T) { }, }, { - name: "", + name: "strings not matching tag keys are still also ignored", config: &Config{ MetricName: "json_test", TagKeys: []string{"wrongtagkey", "tags_object_*"}, @@ -1047,7 +1047,7 @@ func TestParseWithWildcardTagKeys(t *testing.T) { }, }, { - name: "", + name: "single tag key is also found and applied", config: &Config{ MetricName: "json_test", TagKeys: []string{"mytag", "tags_object_*"}, @@ -1090,7 +1090,7 @@ func TestParseLineWithWildcardTagKeys(t *testing.T) { expected telegraf.Metric }{ { - name: "", + name: "wildcard matching with tags nested within object", config: &Config{ MetricName: "json_test", TagKeys: []string{"tags_object_*"}, @@ -1110,7 +1110,7 @@ func TestParseLineWithWildcardTagKeys(t *testing.T) { ), }, { - name: "", + name: "wildcard matching with keys containing tag", config: &Config{ MetricName: "json_test", TagKeys: []string{"*tag"}, @@ -1132,7 +1132,7 @@ func TestParseLineWithWildcardTagKeys(t *testing.T) { ), }, { - name: "", + name: "strings not matching tag keys are ignored", config: &Config{ MetricName: "json_test", TagKeys: []string{"wrongtagkey", "tags_object_*"}, @@ -1152,7 +1152,7 @@ func TestParseLineWithWildcardTagKeys(t *testing.T) { ), }, { - name: "", + name: "single tag key is also found and applied", config: &Config{ MetricName: "json_test", TagKeys: []string{"mytag", "tags_object_*"}, @@ -1195,7 +1195,7 @@ func TestParseArrayWithWildcardTagKeys(t *testing.T) { expected []telegraf.Metric }{ { - name: "", + name: "wildcard matching with keys containing tag within array works", config: &Config{ MetricName: "json_array_test", TagKeys: []string{"*tag"}, @@ -1233,7 +1233,7 @@ func TestParseArrayWithWildcardTagKeys(t *testing.T) { }, }, { - name: "", + name: " wildcard matching with tags nested array within object works", config: &Config{ MetricName: "json_array_test", TagKeys: []string{"tags_array_*"}, @@ -1267,7 +1267,7 @@ func TestParseArrayWithWildcardTagKeys(t *testing.T) { }, }, { - name: "", + name: "strings not matching tag keys are still also ignored", config: &Config{ MetricName: "json_array_test", TagKeys: []string{"mytag", "*tag"}, @@ -1305,7 +1305,7 @@ func TestParseArrayWithWildcardTagKeys(t *testing.T) { }, }, { - name: "", + name: "single tag key is also found and applied", config: &Config{ MetricName: "json_array_test", TagKeys: []string{"anothert", "*tag"}, From 0862c7cad234a836377761d432105f8ee5685710 Mon Sep 17 00:00:00 2001 From: helenosheaa <38860767+helenosheaa@users.noreply.github.com> Date: Thu, 17 Dec 2020 11:12:18 -0500 Subject: [PATCH 09/10] make comments consistent --- plugins/parsers/json/parser.go | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/plugins/parsers/json/parser.go b/plugins/parsers/json/parser.go index 5228b1bef2894..e8a748e7052db 100644 --- a/plugins/parsers/json/parser.go +++ b/plugins/parsers/json/parser.go @@ -109,7 +109,7 @@ func (p *Parser) parseObject(data map[string]interface{}, timestamp time.Time) ( name := p.metricName - //checks if json_name_key is set + // checks if json_name_key is set if p.nameKey != "" { switch field := f.Fields[p.nameKey].(type) { case string: @@ -117,7 +117,7 @@ func (p *Parser) parseObject(data map[string]interface{}, timestamp time.Time) ( } } - //if time key is specified, set timestamp to it + // if time key is specified, set timestamp to it if p.timeKey != "" { if p.timeFormat == "" { err := fmt.Errorf("use of 'json_time_key' requires 'json_time_format'") @@ -136,7 +136,7 @@ func (p *Parser) parseObject(data map[string]interface{}, timestamp time.Time) ( delete(f.Fields, p.timeKey) - //if the year is 0, set to current year + // if the year is 0, set to current year if timestamp.Year() == 0 { timestamp = timestamp.AddDate(time.Now().Year(), 0, 0) } @@ -150,21 +150,21 @@ func (p *Parser) parseObject(data map[string]interface{}, timestamp time.Time) ( return []telegraf.Metric{metric}, nil } -//will take in field map with strings and bools, -//search for TagKeys that match fieldnames and add them to tags -//will delete any strings/bools that shouldn't be fields -//assumes that any non-numeric values in TagKeys should be displayed as tags +// will take in field map with strings and bools, +// search for TagKeys that match fieldnames and add them to tags +// will delete any strings/bools that shouldn't be fields +// assumes that any non-numeric values in TagKeys should be displayed as tags func (p *Parser) switchFieldToTag(tags map[string]string, fields map[string]interface{}) (map[string]string, map[string]interface{}) { for name, value := range fields { if p.tagKeys == nil { continue } - //skip switch statement if tagkey doesn't match fieldname + // skip switch statement if tagkey doesn't match fieldname if !p.tagKeys.Match(name) { continue } - //switch any fields in tagkeys into tags + // switch any fields in tagkeys into tags switch t := value.(type) { case string: tags[name] = t @@ -180,7 +180,7 @@ func (p *Parser) switchFieldToTag(tags map[string]string, fields map[string]inte } } - //remove any additional string/bool values from fields + // remove any additional string/bool values from fields for fk := range fields { switch fields[fk].(type) { case string, bool: From a870f899f6b8246d050c8c2a20724ec9be5ded8a Mon Sep 17 00:00:00 2001 From: helenosheaa <38860767+helenosheaa@users.noreply.github.com> Date: Thu, 17 Dec 2020 17:57:01 -0500 Subject: [PATCH 10/10] fixed spacing issues --- plugins/parsers/json/parser_test.go | 52 ++++++++++++++--------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/plugins/parsers/json/parser_test.go b/plugins/parsers/json/parser_test.go index 7e052270bfa2a..525c8fd2804c2 100644 --- a/plugins/parsers/json/parser_test.go +++ b/plugins/parsers/json/parser_test.go @@ -27,11 +27,11 @@ const validJSONTags = ` "c": 6 }, "mytag": "foobar", - "othertag": "baz", - "tags_object": { - "mytag": "foobar", - "othertag": "baz" - } + "othertag": "baz", + "tags_object": { + "mytag": "foobar", + "othertag": "baz" + } } ` @@ -43,16 +43,16 @@ const validJSONArrayTags = ` "c": 6 }, "mytag": "foo", - "othertag": "baz", - "tags_array": [ - { - "mytag": "foo" - }, - { - "othertag": "baz" - } - ], - "anothert": "foo" + "othertag": "baz", + "tags_array": [ + { + "mytag": "foo" + }, + { + "othertag": "baz" + } + ], + "anothert": "foo" }, { "a": 7, @@ -60,17 +60,17 @@ const validJSONArrayTags = ` "c": 8 }, "mytag": "bar", - "othertag": "baz", - "tags_array": [ - { - "mytag": "bar" - }, - { - "othertag": "baz" - } - ], - "anothert": "bar" -} + "othertag": "baz", + "tags_array": [ + { + "mytag": "bar" + }, + { + "othertag": "baz" + } + ], + "anothert": "bar" + } ] `