From 4db667573a625fedd2fcbc213048a0440eb8352f Mon Sep 17 00:00:00 2001 From: Max U Date: Thu, 28 Jun 2018 15:35:20 -0700 Subject: [PATCH] newJsonParser function is now unexported, must use NewParser(config) --- plugins/inputs/exec/exec_test.go | 18 ++++++++-- plugins/inputs/http/http_test.go | 27 +++++++++++---- plugins/inputs/httpjson/httpjson.go | 9 ++++- .../kafka_consumer/kafka_consumer_test.go | 6 +++- .../kafka_consumer_legacy_test.go | 7 +++- .../mqtt_consumer/mqtt_consumer_test.go | 7 +++- .../nats_consumer/nats_consumer_test.go | 7 +++- .../inputs/tcp_listener/tcp_listener_test.go | 7 +++- .../inputs/udp_listener/udp_listener_test.go | 7 +++- plugins/parsers/json/parser_test.go | 33 ------------------- plugins/parsers/registry.go | 4 +-- 11 files changed, 81 insertions(+), 51 deletions(-) diff --git a/plugins/inputs/exec/exec_test.go b/plugins/inputs/exec/exec_test.go index 5c9f1e19c..9a1376d76 100644 --- a/plugins/inputs/exec/exec_test.go +++ b/plugins/inputs/exec/exec_test.go @@ -93,7 +93,11 @@ func (r runnerMock) Run(e *Exec, command string, acc telegraf.Accumulator) ([]by } func TestExec(t *testing.T) { - parser, _ := parsers.NewJSONParser("exec", []string{}, nil, nil) + config := &parsers.Config{ + DataFormat: "json", + MetricName: "exec", + } + parser, _ := parsers.NewParser(config) e := &Exec{ runner: newRunnerMock([]byte(validJson), nil), Commands: []string{"testcommand arg1"}, @@ -119,7 +123,11 @@ func TestExec(t *testing.T) { } func TestExecMalformed(t *testing.T) { - parser, _ := parsers.NewJSONParser("exec", []string{}, nil, nil) + config := &parsers.Config{ + DataFormat: "json", + MetricName: "exec", + } + parser, _ := parsers.NewParser(config) e := &Exec{ runner: newRunnerMock([]byte(malformedJson), nil), Commands: []string{"badcommand arg1"}, @@ -132,7 +140,11 @@ func TestExecMalformed(t *testing.T) { } func TestCommandError(t *testing.T) { - parser, _ := parsers.NewJSONParser("exec", []string{}, nil, nil) + config := &parsers.Config{ + DataFormat: "json", + MetricName: "exec", + } + parser, _ := parsers.NewParser(config) e := &Exec{ runner: newRunnerMock(nil, fmt.Errorf("exit status code 1")), Commands: []string{"badcommand"}, diff --git a/plugins/inputs/http/http_test.go b/plugins/inputs/http/http_test.go index 52dfb78fe..4d1448226 100644 --- a/plugins/inputs/http/http_test.go +++ b/plugins/inputs/http/http_test.go @@ -26,7 +26,11 @@ func TestHTTPwithJSONFormat(t *testing.T) { URLs: []string{url}, } metricName := "metricName" - p, _ := parsers.NewJSONParser(metricName, nil, nil, nil) + config := &parsers.Config{ + DataFormat: "json", + MetricName: "metricName", + } + p, _ := parsers.NewParser(config) plugin.SetParser(p) var acc testutil.Accumulator @@ -63,8 +67,12 @@ func TestHTTPHeaders(t *testing.T) { URLs: []string{url}, Headers: map[string]string{header: headerValue}, } - metricName := "metricName" - p, _ := parsers.NewJSONParser(metricName, nil, nil, nil) + + config := &parsers.Config{ + DataFormat: "json", + MetricName: "metricName", + } + p, _ := parsers.NewParser(config) plugin.SetParser(p) var acc testutil.Accumulator @@ -83,7 +91,11 @@ func TestInvalidStatusCode(t *testing.T) { } metricName := "metricName" - p, _ := parsers.NewJSONParser(metricName, nil, nil, nil) + config := &parsers.Config{ + DataFormat: "json", + MetricName: metricName, + } + p, _ := parsers.NewParser(config) plugin.SetParser(p) var acc testutil.Accumulator @@ -105,8 +117,11 @@ func TestMethod(t *testing.T) { Method: "POST", } - metricName := "metricName" - p, _ := parsers.NewJSONParser(metricName, nil, nil, nil) + config := &parsers.Config{ + DataFormat: "json", + MetricName: "metricName", + } + p, _ := parsers.NewParser(config) plugin.SetParser(p) var acc testutil.Accumulator diff --git a/plugins/inputs/httpjson/httpjson.go b/plugins/inputs/httpjson/httpjson.go index 6a8f10c94..dfe0f3549 100644 --- a/plugins/inputs/httpjson/httpjson.go +++ b/plugins/inputs/httpjson/httpjson.go @@ -181,7 +181,14 @@ func (h *HttpJson) gatherServer( "server": serverURL, } - parser, err := parsers.NewJSONParser(msrmnt_name, h.TagKeys, nil, tags) + config := &parsers.Config{ + DataFormat: "json", + MetricName: msrmnt_name, + TagKeys: h.TagKeys, + DefaultTags: tags, + } + + parser, err := parsers.NewParser(config) if err != nil { return err } diff --git a/plugins/inputs/kafka_consumer/kafka_consumer_test.go b/plugins/inputs/kafka_consumer/kafka_consumer_test.go index 82e545370..13b298cf7 100644 --- a/plugins/inputs/kafka_consumer/kafka_consumer_test.go +++ b/plugins/inputs/kafka_consumer/kafka_consumer_test.go @@ -125,7 +125,11 @@ func TestRunParserAndGatherJSON(t *testing.T) { k.acc = &acc defer close(k.done) - k.parser, _ = parsers.NewJSONParser("kafka_json_test", []string{}, nil, nil) + config := &parsers.Config{ + DataFormat: "json", + MetricName: "kafka_json_test", + } + k.parser, _ = parsers.NewParser(config) go k.receiver() in <- saramaMsg(testMsgJSON) acc.Wait(1) diff --git a/plugins/inputs/kafka_consumer_legacy/kafka_consumer_legacy_test.go b/plugins/inputs/kafka_consumer_legacy/kafka_consumer_legacy_test.go index ed0a53125..90b94f511 100644 --- a/plugins/inputs/kafka_consumer_legacy/kafka_consumer_legacy_test.go +++ b/plugins/inputs/kafka_consumer_legacy/kafka_consumer_legacy_test.go @@ -125,7 +125,12 @@ func TestRunParserAndGatherJSON(t *testing.T) { k.acc = &acc defer close(k.done) - k.parser, _ = parsers.NewJSONParser("kafka_json_test", []string{}, nil, nil) + config := &parsers.Config{ + DataFormat: "json", + MetricName: "kafka_json_test", + } + + k.parser, _ = parsers.NewParser(config) go k.receiver() in <- saramaMsg(testMsgJSON) acc.Wait(1) diff --git a/plugins/inputs/mqtt_consumer/mqtt_consumer_test.go b/plugins/inputs/mqtt_consumer/mqtt_consumer_test.go index 41a21ae03..73a9abcd4 100644 --- a/plugins/inputs/mqtt_consumer/mqtt_consumer_test.go +++ b/plugins/inputs/mqtt_consumer/mqtt_consumer_test.go @@ -172,7 +172,12 @@ func TestRunParserAndGatherJSON(t *testing.T) { n.acc = &acc defer close(n.done) - n.parser, _ = parsers.NewJSONParser("nats_json_test", []string{}, nil, nil) + config := &parsers.Config{ + DataFormat: "json", + MetricName: "nats_json_test", + } + + n.parser, _ = parsers.NewParser(config) go n.receiver() in <- mqttMsg(testMsgJSON) diff --git a/plugins/inputs/nats_consumer/nats_consumer_test.go b/plugins/inputs/nats_consumer/nats_consumer_test.go index b3e4d1d3b..d5386145a 100644 --- a/plugins/inputs/nats_consumer/nats_consumer_test.go +++ b/plugins/inputs/nats_consumer/nats_consumer_test.go @@ -108,7 +108,12 @@ func TestRunParserAndGatherJSON(t *testing.T) { n.acc = &acc defer close(n.done) - n.parser, _ = parsers.NewJSONParser("nats_json_test", []string{}, nil, nil) + config := &parsers.Config{ + DataFormat: "json", + MetricName: "nats_json_test", + } + + n.parser, _ = parsers.NewParser(config) n.wg.Add(1) go n.receiver() in <- natsMsg(testMsgJSON) diff --git a/plugins/inputs/tcp_listener/tcp_listener_test.go b/plugins/inputs/tcp_listener/tcp_listener_test.go index f5720b04d..0df33c8b4 100644 --- a/plugins/inputs/tcp_listener/tcp_listener_test.go +++ b/plugins/inputs/tcp_listener/tcp_listener_test.go @@ -300,7 +300,12 @@ func TestRunParserJSONMsg(t *testing.T) { listener.acc = &acc defer close(listener.done) - listener.parser, _ = parsers.NewJSONParser("udp_json_test", []string{}, nil, nil) + config := &parsers.Config{ + DataFormat: "json", + MetricName: "udp_json_test", + } + + listener.parser, _ = parsers.NewParser(config) listener.wg.Add(1) go listener.tcpParser() diff --git a/plugins/inputs/udp_listener/udp_listener_test.go b/plugins/inputs/udp_listener/udp_listener_test.go index 3028bf461..69f2f4bed 100644 --- a/plugins/inputs/udp_listener/udp_listener_test.go +++ b/plugins/inputs/udp_listener/udp_listener_test.go @@ -193,7 +193,12 @@ func TestRunParserJSONMsg(t *testing.T) { listener.acc = &acc defer close(listener.done) - listener.parser, _ = parsers.NewJSONParser("udp_json_test", []string{}, nil, nil) + config := &parsers.Config{ + DataFormat: "json", + MetricName: "udp_json_test", + } + + listener.parser, _ = parsers.NewParser(config) listener.wg.Add(1) go listener.udpParser() diff --git a/plugins/parsers/json/parser_test.go b/plugins/parsers/json/parser_test.go index d0eef0b00..d7008ec31 100644 --- a/plugins/parsers/json/parser_test.go +++ b/plugins/parsers/json/parser_test.go @@ -473,37 +473,4 @@ func TestJSONParseNestedArray(t *testing.T) { log.Printf("m[0] name: %v, tags: %v, fields: %v", metrics[0].Name(), metrics[0].Tags(), metrics[0].Fields()) require.NoError(t, err) require.Equal(t, len(parser.TagKeys), len(metrics[0].Tags())) - t.Error() -} - -func TestJSONPathFunctionality(t *testing.T) { - testString := `{ - "total_devices": 5, - "total_threads": 10, - "shares": { - "total": 5, - "accepted": 5, - "rejected": 0, - "avg_find_time": 4, - "tester": "work", - "tester2": "don't want this", - "tester3": { - "hello":"sup", - "fun":"money", - "break":9 - } - } - }` - - parser := JSONParser{ - MetricName: "json_test", - TagKeys: []string{"total_devices", "total_threads", "shares_tester3_fun"}, - FieldKeys: []string{"shares_tester", "shares_tester3_break"}, - TagPaths: map[string]string{"name_test": "$.shares.total"}, - } - - metrics, err := parser.Parse([]byte(testString)) - log.Printf("m[0] name: %v, tags: %v, fields: %v", metrics[0].Name(), metrics[0].Tags(), metrics[0].Fields()) - assert.NoError(t, err) - t.Error() } diff --git a/plugins/parsers/registry.go b/plugins/parsers/registry.go index 749219df8..a1580fe53 100644 --- a/plugins/parsers/registry.go +++ b/plugins/parsers/registry.go @@ -97,7 +97,7 @@ func NewParser(config *Config) (Parser, error) { var parser Parser switch config.DataFormat { case "json": - parser, err = NewJSONParser(config.MetricName, + parser, err = newJSONParser(config.MetricName, config.TagKeys, config.FieldKeys, config.DefaultTags) case "value": parser, err = NewValueParser(config.MetricName, @@ -128,7 +128,7 @@ func NewParser(config *Config) (Parser, error) { return parser, err } -func NewJSONParser( +func newJSONParser( metricName string, tagKeys []string, fieldKeys []string,