internal: FlattenJSON, flatten arrays as well

With HTTP JSON or Elasticsearch, one can also process values nested in arrays.
This commit is contained in:
Philip Silva 2016-01-12 12:12:49 +01:00 committed by Cameron Sparr
parent fb837ca66d
commit 3c89847489
4 changed files with 34 additions and 12 deletions

View File

@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"os"
"strconv"
"strings"
"time"
)
@ -49,9 +50,17 @@ func (f *JSONFlattener) FlattenJSON(
return err
}
}
case []interface{}:
for i, v := range t {
k := strconv.Itoa(i)
err := f.FlattenJSON(fieldname+"_"+k+"_", v)
if err != nil {
return nil
}
}
case float64:
f.Fields[fieldname] = t
case bool, string, []interface{}, nil:
case bool, string, nil:
// ignored types
return nil
default:

View File

@ -562,6 +562,9 @@ var indicesExpected = map[string]interface{}{
}
var osExpected = map[string]interface{}{
"load_average_0": float64(0.01),
"load_average_1": float64(0.04),
"load_average_2": float64(0.05),
"swap_used_in_bytes": float64(0),
"swap_free_in_bytes": float64(487997440),
"timestamp": float64(1436460392944),
@ -724,6 +727,9 @@ var threadPoolExpected = map[string]interface{}{
}
var fsExpected = map[string]interface{}{
"data_0_total_in_bytes": float64(19507089408),
"data_0_free_in_bytes": float64(16909316096),
"data_0_available_in_bytes": float64(15894814720),
"timestamp": float64(1436460392946),
"total_free_in_bytes": float64(16909316096),
"total_available_in_bytes": float64(15894814720),

View File

@ -59,13 +59,17 @@ func TestExec(t *testing.T) {
var acc testutil.Accumulator
err := e.Gather(&acc)
require.NoError(t, err)
assert.Equal(t, acc.NFields(), 4, "non-numeric measurements should be ignored")
assert.Equal(t, acc.NFields(), 8, "non-numeric measurements should be ignored")
fields := map[string]interface{}{
"num_processes": float64(82),
"cpu_used": float64(8234),
"cpu_free": float64(32),
"percent": float64(0.81),
"users_0": float64(0),
"users_1": float64(1),
"users_2": float64(2),
"users_3": float64(3),
}
acc.AssertContainsFields(t, "exec", fields)
}

View File

@ -19,12 +19,12 @@ const validJSON = `
},
"ignored_null": null,
"integer": 4,
"ignored_list": [3, 4],
"list": [3, 4],
"ignored_parent": {
"another_ignored_list": [4],
"another_ignored_null": null,
"ignored_string": "hello, world!"
}
},
"another_list": [4]
}`
const validJSONTags = `
@ -36,6 +36,9 @@ const validJSONTags = `
var expectedFields = map[string]interface{}{
"parent_child": float64(3),
"list_0": float64(3),
"list_1": float64(4),
"another_list_0": float64(4),
"integer": float64(4),
}
@ -123,7 +126,7 @@ func TestHttpJson200(t *testing.T) {
var acc testutil.Accumulator
err := service.Gather(&acc)
require.NoError(t, err)
assert.Equal(t, 4, acc.NFields())
assert.Equal(t, 10, acc.NFields())
for _, srv := range service.Servers {
tags := map[string]string{"server": srv}
mname := "httpjson_" + service.Name