forked from Syncano/rabbitmq_exporter
-
Notifications
You must be signed in to change notification settings - Fork 197
/
jsonmap.go
146 lines (128 loc) · 3.36 KB
/
jsonmap.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package main
import (
"bytes"
"encoding/json"
"fmt"
"strconv"
"time"
log "github.com/sirupsen/logrus"
)
type rabbitJSONReply struct {
body []byte
keys map[string]interface{}
}
func makeJSONReply(body []byte) (RabbitReply, error) {
return &rabbitJSONReply{body, nil}, nil
}
//MakeStatsInfo creates a slice of StatsInfo from json input. Only keys with float values are mapped into `metrics`.
func (rep *rabbitJSONReply) MakeStatsInfo(labels []string) []StatsInfo {
var statistics []StatsInfo
var jsonArr []map[string]interface{}
decoder := json.NewDecoder(bytes.NewBuffer(rep.body))
if decoder == nil {
log.Error("JSON decoder not iniatilized")
return make([]StatsInfo, 0)
}
if err := decoder.Decode(&jsonArr); err != nil {
log.WithField("error", err).Error("Error while decoding json")
return make([]StatsInfo, 0)
}
for _, el := range jsonArr {
field := ""
if _, fieldName := el["name"]; fieldName {
field = "name"
}
if _, fieldID := el["id"]; fieldID {
field = "id"
}
if field != "" {
log.WithFields(log.Fields{"element": el, "vhost": el["vhost"], field: el[field]}).Debug("Iterate over array")
statsinfo := StatsInfo{}
statsinfo.labels = make(map[string]string)
for _, label := range labels {
statsinfo.labels[label] = ""
if tmp, ok := el[label]; ok {
if v, ok := tmp.(string); ok {
statsinfo.labels[label] = v
} else if v, ok := tmp.(bool); ok {
statsinfo.labels[label] = strconv.FormatBool(v)
}
}
}
statsinfo.metrics = make(MetricMap)
addFields(&statsinfo.metrics, "", el)
statistics = append(statistics, statsinfo)
}
}
return statistics
}
//MakeMap creates a map from json input. Only keys with float values are mapped.
func (rep *rabbitJSONReply) MakeMap() MetricMap {
flMap := make(MetricMap)
var output map[string]interface{}
decoder := json.NewDecoder(bytes.NewBuffer(rep.body))
if decoder == nil {
log.Error("JSON decoder not iniatilized")
return flMap
}
if err := decoder.Decode(&output); err != nil {
log.WithField("error", err).Error("Error while decoding json")
return flMap
}
addFields(&flMap, "", output)
return flMap
}
func addFields(toMap *MetricMap, basename string, source map[string]interface{}) {
prefix := ""
if basename != "" {
prefix = basename + "."
}
for k, v := range source {
switch value := v.(type) {
case float64:
(*toMap)[prefix+k] = value
case []interface{}:
(*toMap)[prefix+k+"_len"] = float64(len(value))
case map[string]interface{}:
addFields(toMap, prefix+k, value)
case bool:
if value {
(*toMap)[prefix+k] = 1
} else {
(*toMap)[prefix+k] = 0
}
}
}
}
func (rep *rabbitJSONReply) GetString(key string) (string, bool) {
if rep.keys == nil {
keys := make(map[string]interface{})
decoder := json.NewDecoder(bytes.NewBuffer(rep.body))
if decoder == nil {
log.Error("JSON decoder not iniatilized")
return "", false
}
err := decoder.Decode(&keys)
if err != nil {
return "", false
}
rep.keys = keys
}
val, ok := rep.keys[key]
if !ok {
return "", false
}
value, ok := val.(string)
return value, ok
}
func parseTime(s string) (time.Time, error) {
t, err := time.Parse("2006-01-02 15:04:05", s)
if err == nil {
return t, nil
}
t, err = time.Parse("2006-01-02T15:04:05.999-07:00", s)
if err == nil {
return t, nil
}
return t, fmt.Errorf("time format does not match expectations")
}