forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 1
/
data.go
87 lines (74 loc) · 2.03 KB
/
data.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package jmx
import (
"encoding/json"
"fmt"
"github.com/elastic/beats/libbeat/common"
"github.com/joeshaw/multierror"
)
type Entry struct {
Request struct {
Mbean string `json:"mbean"`
}
Value map[string]interface{}
}
// Map responseBody to common.MapStr
//
// A response has the following structure
// [
// {
// "request": {
// "mbean": "java.lang:type=Memory",
// "attribute": [
// "HeapMemoryUsage",
// "NonHeapMemoryUsage"
// ],
// "type": "read"
// },
// "value": {
// "HeapMemoryUsage": {
// "init": 1073741824,
// "committed": 1037959168,
// "max": 1037959168,
// "used": 227420472
// },
// "NonHeapMemoryUsage": {
// "init": 2555904,
// "committed": 53477376,
// "max": -1,
// "used": 50519768
// }
// },
// "timestamp": 1472298687,
// "status": 200
// }
// ]
func eventMapping(content []byte, mapping map[string]string) (common.MapStr, error) {
var entries []Entry
err := json.Unmarshal(content, &entries)
if err != nil {
return nil, fmt.Errorf("Cannot unmarshal json response: %s", err)
}
event := common.MapStr{}
var errs multierror.Errors
for _, v := range entries {
for attribute, value := range v.Value {
// Extend existing event
err := parseResponseEntry(v.Request.Mbean, attribute, value, event, mapping)
if err != nil {
errs = append(errs, err)
}
}
}
return event, errs.Err()
}
func parseResponseEntry(mbeanName string, attributeName string, attibuteValue interface{},
event common.MapStr, mapping map[string]string) error {
//create metric name by merging mbean and attribute fields
var metricName = mbeanName + "_" + attributeName
key, exists := mapping[metricName]
if !exists {
return fmt.Errorf("No key found for metric: '%s', skipping...", metricName)
}
_, err := event.Put(key, attibuteValue)
return err
}