Skip to content

Commit

Permalink
Collect JSON values recursively
Browse files Browse the repository at this point in the history
  • Loading branch information
artefactop authored and Nick White committed Jan 31, 2017
1 parent 52f0363 commit dd27344
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ in their config file.
- [#2061](https://github.com/influxdata/telegraf/issues/2061): Fix panic when file stat info cannot be collected due to permissions or other issue(s).
- [#2045](https://github.com/influxdata/telegraf/issues/2045): Graylog output should set short_message field.
- [#1904](https://github.com/influxdata/telegraf/issues/1904): Hddtemp always put the value in the field temperature.
- [#1693](https://github.com/influxdata/telegraf/issues/1693): Properly collect nested jolokia struct data.

## v1.1.2 [2016-12-12]

Expand Down
27 changes: 11 additions & 16 deletions plugins/inputs/jolokia/jolokia.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,16 @@ func (j *Jolokia) prepareRequest(server Server, metric Metric) (*http.Request, e
return req, nil
}

func extractValues(measurement string, value interface{}, fields map[string]interface{}) {
if mapValues, ok := value.(map[string]interface{}); ok {
for k2, v2 := range mapValues {
extractValues(measurement+"_"+k2, v2, fields)
}
} else {
fields[measurement] = value
}
}

func (j *Jolokia) Gather(acc telegraf.Accumulator) error {
servers := j.Servers
metrics := j.Metrics
Expand All @@ -244,23 +254,8 @@ func (j *Jolokia) Gather(acc telegraf.Accumulator) error {
if err != nil {
fmt.Printf("Error handling response: %s\n", err)
} else {

if values, ok := out["value"]; ok {
switch t := values.(type) {
case map[string]interface{}:
for k, v := range t {
switch t2 := v.(type) {
case map[string]interface{}:
for k2, v2 := range t2 {
fields[measurement+"_"+k+"_"+k2] = v2
}
case interface{}:
fields[measurement+"_"+k] = t2
}
}
case interface{}:
fields[measurement] = t
}
extractValues(measurement, values, fields)
} else {
fmt.Printf("Missing key 'value' in output response\n")
}
Expand Down
63 changes: 63 additions & 0 deletions plugins/inputs/jolokia/jolokia_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,37 @@ import (
_ "github.com/stretchr/testify/require"
)

const validThreeLevelMultiValueJSON = `
{
"request":{
"mbean":"java.lang:type=*",
"type":"read"
},
"value":{
"java.lang:type=Memory":{
"ObjectPendingFinalizationCount":0,
"Verbose":false,
"HeapMemoryUsage":{
"init":134217728,
"committed":173015040,
"max":1908932608,
"used":16840016
},
"NonHeapMemoryUsage":{
"init":2555904,
"committed":51380224,
"max":-1,
"used":49944048
},
"ObjectName":{
"objectName":"java.lang:type=Memory"
}
}
},
"timestamp":1446129191,
"status":200
}`

const validMultiValueJSON = `
{
"request":{
Expand Down Expand Up @@ -103,6 +134,38 @@ func TestHttpJsonMultiValue(t *testing.T) {
acc.AssertContainsTaggedFields(t, "jolokia", fields, tags)
}

// Test that the proper values are ignored or collected
func TestHttpJsonThreeLevelMultiValue(t *testing.T) {
jolokia := genJolokiaClientStub(validThreeLevelMultiValueJSON, 200, Servers, []Metric{HeapMetric})

var acc testutil.Accumulator
err := jolokia.Gather(&acc)

assert.Nil(t, err)
assert.Equal(t, 1, len(acc.Metrics))

fields := map[string]interface{}{
"heap_memory_usage_java.lang:type=Memory_ObjectPendingFinalizationCount": 0.0,
"heap_memory_usage_java.lang:type=Memory_Verbose": false,
"heap_memory_usage_java.lang:type=Memory_HeapMemoryUsage_init": 134217728.0,
"heap_memory_usage_java.lang:type=Memory_HeapMemoryUsage_max": 1908932608.0,
"heap_memory_usage_java.lang:type=Memory_HeapMemoryUsage_used": 16840016.0,
"heap_memory_usage_java.lang:type=Memory_HeapMemoryUsage_committed": 173015040.0,
"heap_memory_usage_java.lang:type=Memory_NonHeapMemoryUsage_init": 2555904.0,
"heap_memory_usage_java.lang:type=Memory_NonHeapMemoryUsage_committed": 51380224.0,
"heap_memory_usage_java.lang:type=Memory_NonHeapMemoryUsage_max": -1.0,
"heap_memory_usage_java.lang:type=Memory_NonHeapMemoryUsage_used": 49944048.0,
"heap_memory_usage_java.lang:type=Memory_ObjectName_objectName": "java.lang:type=Memory",
}

tags := map[string]string{
"jolokia_host": "127.0.0.1",
"jolokia_port": "8080",
"jolokia_name": "as1",
}
acc.AssertContainsTaggedFields(t, "jolokia", fields, tags)
}

// Test that the proper values are ignored or collected
func TestHttpJsonOn404(t *testing.T) {

Expand Down

0 comments on commit dd27344

Please sign in to comment.