Allow JSON data format to contain zero metrics (#3268)
This commit is contained in:
parent
2e1457a496
commit
22a9ffbb9d
|
@ -477,15 +477,13 @@ func TestHttpJsonBadJson(t *testing.T) {
|
||||||
assert.Equal(t, 0, acc.NFields())
|
assert.Equal(t, 0, acc.NFields())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test response to empty string as response objectgT
|
// Test response to empty string as response object
|
||||||
func TestHttpJsonEmptyResponse(t *testing.T) {
|
func TestHttpJsonEmptyResponse(t *testing.T) {
|
||||||
httpjson := genMockHttpJson(empty, 200)
|
httpjson := genMockHttpJson(empty, 200)
|
||||||
|
|
||||||
var acc testutil.Accumulator
|
var acc testutil.Accumulator
|
||||||
err := acc.GatherError(httpjson[0].Gather)
|
err := acc.GatherError(httpjson[0].Gather)
|
||||||
|
assert.NoError(t, err)
|
||||||
assert.Error(t, err)
|
|
||||||
assert.Equal(t, 0, acc.NFields())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test that the proper values are ignored or collected
|
// Test that the proper values are ignored or collected
|
||||||
|
|
|
@ -67,6 +67,10 @@ func (p *JSONParser) parseObject(metrics []telegraf.Metric, jsonOut map[string]i
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *JSONParser) Parse(buf []byte) ([]telegraf.Metric, error) {
|
func (p *JSONParser) Parse(buf []byte) ([]telegraf.Metric, error) {
|
||||||
|
buf = bytes.TrimSpace(buf)
|
||||||
|
if len(buf) == 0 {
|
||||||
|
return make([]telegraf.Metric, 0), nil
|
||||||
|
}
|
||||||
|
|
||||||
if !isarray(buf) {
|
if !isarray(buf) {
|
||||||
metrics := make([]telegraf.Metric, 0)
|
metrics := make([]telegraf.Metric, 0)
|
||||||
|
|
|
@ -84,6 +84,16 @@ func TestParseValidJSON(t *testing.T) {
|
||||||
"b_c": float64(6),
|
"b_c": float64(6),
|
||||||
}, metrics[0].Fields())
|
}, metrics[0].Fields())
|
||||||
assert.Equal(t, map[string]string{}, metrics[0].Tags())
|
assert.Equal(t, map[string]string{}, metrics[0].Tags())
|
||||||
|
|
||||||
|
// Test that whitespace only will parse as an empty list of metrics
|
||||||
|
metrics, err = parser.Parse([]byte("\n\t"))
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Len(t, metrics, 0)
|
||||||
|
|
||||||
|
// Test that an empty string will parse as an empty list of metrics
|
||||||
|
metrics, err = parser.Parse([]byte(""))
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Len(t, metrics, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestParseLineValidJSON(t *testing.T) {
|
func TestParseLineValidJSON(t *testing.T) {
|
||||||
|
|
Loading…
Reference in New Issue