Treat empty array as successful parse (#6130)

This commit is contained in:
Daniel Nelson 2019-07-17 15:03:09 -07:00 committed by GitHub
parent a07f29c02f
commit e576048e02
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 83 additions and 40 deletions

View File

@ -3,6 +3,7 @@ package json
import ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"log" "log"
"strconv" "strconv"
@ -18,6 +19,7 @@ import (
var ( var (
utf8BOM = []byte("\xef\xbb\xbf") utf8BOM = []byte("\xef\xbb\xbf")
ErrWrongType = errors.New("must be an object or an array of objects")
) )
type Config struct { type Config struct {
@ -63,32 +65,34 @@ func New(config *Config) (*Parser, error) {
}, nil }, nil
} }
func (p *Parser) parseArray(buf []byte) ([]telegraf.Metric, error) { func (p *Parser) parseArray(data []interface{}) ([]telegraf.Metric, error) {
metrics := make([]telegraf.Metric, 0) results := make([]telegraf.Metric, 0)
var jsonOut []map[string]interface{} for _, item := range data {
err := json.Unmarshal(buf, &jsonOut) switch v := item.(type) {
if err != nil { case map[string]interface{}:
err = fmt.Errorf("unable to parse out as JSON Array, %s", err) metrics, err := p.parseObject(v)
return nil, err
}
for _, item := range jsonOut {
metrics, err = p.parseObject(metrics, item)
if err != nil { if err != nil {
return nil, err return nil, err
} }
results = append(results, metrics...)
default:
return nil, ErrWrongType
} }
return metrics, nil
} }
func (p *Parser) parseObject(metrics []telegraf.Metric, jsonOut map[string]interface{}) ([]telegraf.Metric, error) { return results, nil
}
func (p *Parser) parseObject(data map[string]interface{}) ([]telegraf.Metric, error) {
tags := make(map[string]string) tags := make(map[string]string)
for k, v := range p.defaultTags { for k, v := range p.defaultTags {
tags[k] = v tags[k] = v
} }
f := JSONFlattener{} f := JSONFlattener{}
err := f.FullFlattenJSON("", jsonOut, true, true) err := f.FullFlattenJSON("", data, true, true)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -134,7 +138,7 @@ func (p *Parser) parseObject(metrics []telegraf.Metric, jsonOut map[string]inter
if err != nil { if err != nil {
return nil, err return nil, err
} }
return append(metrics, metric), nil return []telegraf.Metric{metric}, nil
} }
//will take in field map with strings and bools, //will take in field map with strings and bools,
@ -193,17 +197,20 @@ func (p *Parser) Parse(buf []byte) ([]telegraf.Metric, error) {
return make([]telegraf.Metric, 0), nil return make([]telegraf.Metric, 0), nil
} }
if !isarray(buf) { var data interface{}
metrics := make([]telegraf.Metric, 0) err := json.Unmarshal(buf, &data)
var jsonOut map[string]interface{}
err := json.Unmarshal(buf, &jsonOut)
if err != nil { if err != nil {
err = fmt.Errorf("unable to parse out as JSON, %s", err)
return nil, err return nil, err
} }
return p.parseObject(metrics, jsonOut)
switch v := data.(type) {
case map[string]interface{}:
return p.parseObject(v)
case []interface{}:
return p.parseArray(v)
default:
return nil, ErrWrongType
} }
return p.parseArray(buf)
} }
func (p *Parser) ParseLine(line string) (telegraf.Metric, error) { func (p *Parser) ParseLine(line string) (telegraf.Metric, error) {
@ -288,13 +295,3 @@ func (f *JSONFlattener) FullFlattenJSON(
} }
return nil return nil
} }
func isarray(buf []byte) bool {
ia := bytes.IndexByte(buf, '[')
ib := bytes.IndexByte(buf, '{')
if ia > -1 && ia < ib {
return true
} else {
return false
}
}

View File

@ -2,7 +2,6 @@ package json
import ( import (
"fmt" "fmt"
"log"
"testing" "testing"
"time" "time"
@ -496,7 +495,7 @@ func TestJSONParseNestedArray(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
metrics, err := parser.Parse([]byte(testString)) 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()) require.Len(t, metrics, 1)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, 3, len(metrics[0].Tags())) require.Equal(t, 3, len(metrics[0].Tags()))
} }
@ -754,7 +753,6 @@ func TestTimeErrors(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
metrics, err = parser.Parse([]byte(testString2)) metrics, err = parser.Parse([]byte(testString2))
log.Printf("err: %v", err)
require.Error(t, err) require.Error(t, err)
require.Equal(t, 0, len(metrics)) require.Equal(t, 0, len(metrics))
require.Equal(t, fmt.Errorf("JSON time key could not be found"), err) require.Equal(t, fmt.Errorf("JSON time key could not be found"), err)
@ -840,3 +838,51 @@ func TestStringFieldGlob(t *testing.T) {
testutil.RequireMetricsEqual(t, expected, actual) testutil.RequireMetricsEqual(t, expected, actual)
} }
func TestParseEmptyArray(t *testing.T) {
data := `[]`
parser, err := New(&Config{})
require.NoError(t, err)
actual, err := parser.Parse([]byte(data))
require.NoError(t, err)
expected := []telegraf.Metric{}
testutil.RequireMetricsEqual(t, expected, actual)
}
func TestParseSimpleArray(t *testing.T) {
data := `[{"answer": 42}]`
parser, err := New(&Config{
MetricName: "json",
})
require.NoError(t, err)
actual, err := parser.Parse([]byte(data))
require.NoError(t, err)
expected := []telegraf.Metric{
testutil.MustMetric(
"json",
map[string]string{},
map[string]interface{}{
"answer": 42.0,
},
time.Unix(0, 0),
),
}
testutil.RequireMetricsEqual(t, expected, actual, testutil.IgnoreTime())
}
func TestParseArrayWithWrongType(t *testing.T) {
data := `[{"answer": 42}, 123]`
parser, err := New(&Config{})
require.NoError(t, err)
_, err = parser.Parse([]byte(data))
require.Error(t, err)
}