forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.go
120 lines (93 loc) · 2.27 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
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
package collector
import (
"encoding/json"
"strings"
"github.com/elastic/beats/libbeat/common"
)
type DropWizardEvent struct {
key string
value common.MapStr
tags common.MapStr
tagHash string
}
// NewPromEvent creates a prometheus event based on the given string
func eventMapping(metrics map[string]interface{}) map[string]common.MapStr {
eventList := map[string]common.MapStr{}
for _, metricSet := range metrics {
switch t := metricSet.(type) {
case map[string]interface{}:
for key, value := range t {
name, tags := splitTagsFromMetricName(key)
valueMap := common.MapStr{}
metric, _ := value.(map[string]interface{})
for k, v := range metric {
switch v.(type) {
case string:
valueMap[k] = v
case json.Number:
valueMap[k] = convertValue(v.(json.Number))
}
}
dropEvent := DropWizardEvent{
key: name,
value: valueMap,
}
if len(tags) != 0 {
dropEvent.tags = tags
dropEvent.tagHash = tags.String()
} else {
dropEvent.tagHash = "_"
}
if _, ok := eventList[dropEvent.tagHash]; !ok {
eventList[dropEvent.tagHash] = common.MapStr{}
// Add labels
if len(dropEvent.tags) > 0 {
eventList[dropEvent.tagHash]["tags"] = dropEvent.tags
}
}
eventList[dropEvent.tagHash][dropEvent.key] = dropEvent.value
}
default:
continue
}
}
return eventList
}
func splitTagsFromMetricName(metricName string) (string, common.MapStr) {
if metricName == "" {
return "", nil
}
index := strings.Index(metricName, "{")
if index == -1 {
return metricName, nil
}
key := metricName[:index]
tags := common.MapStr{}
tagStr := metricName[index+1 : len(metricName)-1]
for {
dobreak := false
ind := strings.Index(tagStr, ",")
eqPos := strings.Index(tagStr, "=")
if ind == -1 {
tags[tagStr[:eqPos]] = tagStr[eqPos+1:]
dobreak = true
} else {
tags[tagStr[:eqPos]] = tagStr[eqPos+1 : ind]
tagStr = tagStr[ind+2:]
}
if dobreak == true {
break
}
}
return key, tags
}
// convertValue takes the input string and converts it to int of float
func convertValue(value json.Number) interface{} {
if i, err := value.Int64(); err == nil {
return i
}
if f, err := value.Float64(); err == nil {
return f
}
return value.String()
}