newJsonParser function is now unexported, must use NewParser(config)

This commit is contained in:
Max U 2018-06-28 15:35:20 -07:00
parent 7f2b2a08ae
commit 4db667573a
11 changed files with 81 additions and 51 deletions

View File

@ -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"},

View File

@ -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

View File

@ -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
}

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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()

View File

@ -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()

View File

@ -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()
}

View File

@ -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,