forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.go
165 lines (140 loc) · 3.64 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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package server
import (
"errors"
"math"
"strconv"
"strings"
"sync"
"time"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/metricbeat/mb"
)
type template struct {
Namespace string
Delimiter string
Parts []string
Tags map[string]string
}
type metricProcessor struct {
templates *tree
defaultTemplate template
sync.RWMutex
}
func NewMetricProcessor(templates []TemplateConfig, defaultTemplate TemplateConfig) *metricProcessor {
templateTree := NewTree(getTemplateFromConfig(defaultTemplate))
for _, t := range templates {
templateTree.Insert(t.Filter, getTemplateFromConfig(t))
}
return &metricProcessor{
templates: templateTree,
defaultTemplate: getTemplateFromConfig(defaultTemplate),
}
}
func getTemplateFromConfig(config TemplateConfig) template {
return template{
Namespace: config.Namespace,
Tags: config.Tags,
Delimiter: config.Delimiter,
Parts: strings.Split(config.Template, "."),
}
}
func (m *metricProcessor) AddTemplate(t TemplateConfig) {
m.Lock()
template := getTemplateFromConfig(t)
m.templates.Insert(t.Filter, template)
m.Unlock()
}
func (m *metricProcessor) RemoveTemplate(template TemplateConfig) {
m.Lock()
m.templates.Delete(template.Filter)
m.Unlock()
}
func (m *metricProcessor) Process(message string) (common.MapStr, error) {
metric, timestamp, value, err := m.splitMetric(message)
if err != nil {
return nil, err
}
parts := strings.Split(metric, ".")
t := m.FindTemplate(parts)
var name, namespace string
var tags common.MapStr
if t == nil {
name, tags = m.defaultTemplate.Apply(parts)
namespace = m.defaultTemplate.Namespace
} else {
name, tags = t.Apply(parts)
namespace = t.Namespace
}
event := common.MapStr{
"@timestamp": timestamp,
name: value,
mb.NamespaceKey: namespace,
}
if len(tags) != 0 {
event["tag"] = tags
}
return event, nil
}
func (m *metricProcessor) FindTemplate(metric []string) *template {
return m.templates.Search(metric)
}
func (m *metricProcessor) splitMetric(metric string) (string, common.Time, float64, error) {
var metricName string
var timestamp common.Time
var value float64
parts := strings.Fields(metric)
currentTime := common.Time(time.Now())
if len(parts) < 2 {
return "", currentTime, 0, errors.New("Message not in expected format")
} else {
metricName = parts[0]
val, err := strconv.ParseFloat(parts[1], 64)
if err != nil {
return "", currentTime, 0, errors.New("Unable to parse metric value")
} else {
value = val
}
}
if len(parts) == 3 {
if parts[2] == "N" {
timestamp = currentTime
}
ts, err := strconv.ParseFloat(parts[2], 64)
if err != nil {
return "", currentTime, 0, errors.New("Unable to parse timestamp")
}
if ts != -1 {
timestamp = common.Time(time.Unix(int64(ts), int64((ts-math.Floor(ts))*float64(time.Second))))
} else {
timestamp = currentTime
}
} else {
timestamp = currentTime
}
return metricName, timestamp, value, nil
}
func (t *template) Apply(parts []string) (string, common.MapStr) {
tags := make(common.MapStr)
metric := make([]string, 0)
for tagKey, tagVal := range t.Tags {
tags[tagKey] = tagVal
}
tagsMap := make(map[string][]string)
for i := 0; i < len(t.Parts); i++ {
if t.Parts[i] == "metric" {
metric = append(metric, parts[i])
} else if t.Parts[i] == "metric*" {
metric = append(metric, parts[i:]...)
} else if t.Parts[i] != "" {
tagsMap[t.Parts[i]] = append(tagsMap[t.Parts[i]], parts[i])
}
}
for key, value := range tagsMap {
tags[key] = strings.Join(value, t.Delimiter)
}
if len(metric) == 0 {
return "", tags
} else {
return strings.Join(metric, t.Delimiter), tags
}
}