forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
converter.go
430 lines (376 loc) · 8.83 KB
/
converter.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
package converter
import (
"fmt"
"log"
"math"
"strconv"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/filter"
"github.com/influxdata/telegraf/plugins/processors"
)
var sampleConfig = `
## Tags to convert
##
## The table key determines the target type, and the array of key-values
## select the keys to convert. The array may contain globs.
## <target-type> = [<tag-key>...]
[processors.converter.tags]
string = []
integer = []
unsigned = []
boolean = []
float = []
## Fields to convert
##
## The table key determines the target type, and the array of key-values
## select the keys to convert. The array may contain globs.
## <target-type> = [<field-key>...]
[processors.converter.fields]
tag = []
string = []
integer = []
unsigned = []
boolean = []
float = []
`
type Conversion struct {
Tag []string `toml:"tag"`
String []string `toml:"string"`
Integer []string `toml:"integer"`
Unsigned []string `toml:"unsigned"`
Boolean []string `toml:"boolean"`
Float []string `toml:"float"`
}
type Converter struct {
Tags *Conversion `toml:"tags"`
Fields *Conversion `toml:"fields"`
initialized bool
tagConversions *ConversionFilter
fieldConversions *ConversionFilter
}
type ConversionFilter struct {
Tag filter.Filter
String filter.Filter
Integer filter.Filter
Unsigned filter.Filter
Boolean filter.Filter
Float filter.Filter
}
func (p *Converter) SampleConfig() string {
return sampleConfig
}
func (p *Converter) Description() string {
return "Convert values to another metric value type"
}
func (p *Converter) Apply(metrics ...telegraf.Metric) []telegraf.Metric {
if !p.initialized {
err := p.compile()
if err != nil {
logPrintf("initialization error: %v\n", err)
return metrics
}
}
for _, metric := range metrics {
p.convertTags(metric)
p.convertFields(metric)
}
return metrics
}
func (p *Converter) compile() error {
tf, err := compileFilter(p.Tags)
if err != nil {
return err
}
ff, err := compileFilter(p.Fields)
if err != nil {
return err
}
if tf == nil && ff == nil {
return fmt.Errorf("no filters found")
}
p.tagConversions = tf
p.fieldConversions = ff
p.initialized = true
return nil
}
func compileFilter(conv *Conversion) (*ConversionFilter, error) {
if conv == nil {
return nil, nil
}
var err error
cf := &ConversionFilter{}
cf.Tag, err = filter.Compile(conv.Tag)
if err != nil {
return nil, err
}
cf.String, err = filter.Compile(conv.String)
if err != nil {
return nil, err
}
cf.Integer, err = filter.Compile(conv.Integer)
if err != nil {
return nil, err
}
cf.Unsigned, err = filter.Compile(conv.Unsigned)
if err != nil {
return nil, err
}
cf.Boolean, err = filter.Compile(conv.Boolean)
if err != nil {
return nil, err
}
cf.Float, err = filter.Compile(conv.Float)
if err != nil {
return nil, err
}
return cf, nil
}
// convertTags converts tags into fields
func (p *Converter) convertTags(metric telegraf.Metric) {
if p.tagConversions == nil {
return
}
for key, value := range metric.Tags() {
if p.tagConversions.String != nil && p.tagConversions.String.Match(key) {
metric.RemoveTag(key)
metric.AddField(key, value)
continue
}
if p.tagConversions.Integer != nil && p.tagConversions.Integer.Match(key) {
v, ok := toInteger(value)
if !ok {
metric.RemoveTag(key)
logPrintf("error converting to integer [%T]: %v\n", value, value)
continue
}
metric.RemoveTag(key)
metric.AddField(key, v)
}
if p.tagConversions.Unsigned != nil && p.tagConversions.Unsigned.Match(key) {
v, ok := toUnsigned(value)
if !ok {
metric.RemoveTag(key)
logPrintf("error converting to unsigned [%T]: %v\n", value, value)
continue
}
metric.RemoveTag(key)
metric.AddField(key, v)
continue
}
if p.tagConversions.Boolean != nil && p.tagConversions.Boolean.Match(key) {
v, ok := toBool(value)
if !ok {
metric.RemoveTag(key)
logPrintf("error converting to boolean [%T]: %v\n", value, value)
continue
}
metric.RemoveTag(key)
metric.AddField(key, v)
continue
}
if p.tagConversions.Float != nil && p.tagConversions.Float.Match(key) {
v, ok := toFloat(value)
if !ok {
metric.RemoveTag(key)
logPrintf("error converting to float [%T]: %v\n", value, value)
continue
}
metric.RemoveTag(key)
metric.AddField(key, v)
continue
}
}
}
// convertFields converts fields into tags or other field types
func (p *Converter) convertFields(metric telegraf.Metric) {
if p.fieldConversions == nil {
return
}
for key, value := range metric.Fields() {
if p.fieldConversions.Tag != nil && p.fieldConversions.Tag.Match(key) {
v, ok := toString(value)
if !ok {
metric.RemoveField(key)
logPrintf("error converting to tag [%T]: %v\n", value, value)
continue
}
metric.RemoveField(key)
metric.AddTag(key, v)
continue
}
if p.fieldConversions.Float != nil && p.fieldConversions.Float.Match(key) {
v, ok := toFloat(value)
if !ok {
metric.RemoveField(key)
logPrintf("error converting to integer [%T]: %v\n", value, value)
continue
}
metric.RemoveField(key)
metric.AddField(key, v)
continue
}
if p.fieldConversions.Integer != nil && p.fieldConversions.Integer.Match(key) {
v, ok := toInteger(value)
if !ok {
metric.RemoveField(key)
logPrintf("error converting to integer [%T]: %v\n", value, value)
continue
}
metric.RemoveField(key)
metric.AddField(key, v)
continue
}
if p.fieldConversions.Unsigned != nil && p.fieldConversions.Unsigned.Match(key) {
v, ok := toUnsigned(value)
if !ok {
metric.RemoveField(key)
logPrintf("error converting to unsigned [%T]: %v\n", value, value)
continue
}
metric.RemoveField(key)
metric.AddField(key, v)
continue
}
if p.fieldConversions.Boolean != nil && p.fieldConversions.Boolean.Match(key) {
v, ok := toBool(value)
if !ok {
metric.RemoveField(key)
logPrintf("error converting to bool [%T]: %v\n", value, value)
continue
}
metric.RemoveField(key)
metric.AddField(key, v)
continue
}
if p.fieldConversions.String != nil && p.fieldConversions.String.Match(key) {
v, ok := toString(value)
if !ok {
metric.RemoveField(key)
logPrintf("error converting to string [%T]: %v\n", value, value)
continue
}
metric.RemoveField(key)
metric.AddField(key, v)
continue
}
}
}
func toBool(v interface{}) (bool, bool) {
switch value := v.(type) {
case int64, uint64, float64:
if value != 0 {
return true, true
} else {
return false, false
}
case bool:
return value, true
case string:
result, err := strconv.ParseBool(value)
return result, err == nil
}
return false, false
}
func toInteger(v interface{}) (int64, bool) {
switch value := v.(type) {
case int64:
return value, true
case uint64:
if value <= uint64(math.MaxInt64) {
return int64(value), true
} else {
return math.MaxInt64, true
}
case float64:
if value < float64(math.MinInt64) {
return math.MinInt64, true
} else if value > float64(math.MaxInt64) {
return math.MaxInt64, true
} else {
return int64(value), true
}
case bool:
if value {
return 1, true
} else {
return 0, true
}
case string:
result, err := strconv.ParseInt(value, 10, 64)
return result, err == nil
}
return 0, false
}
func toUnsigned(v interface{}) (uint64, bool) {
switch value := v.(type) {
case uint64:
return value, true
case int64:
if value < 0 {
return 0, true
} else {
return uint64(value), true
}
case float64:
if value < 0.0 {
return 0, true
} else if value > float64(math.MaxUint64) {
return math.MaxUint64, true
} else {
return uint64(value), true
}
case bool:
if value {
return 1, true
} else {
return 0, true
}
case string:
result, err := strconv.ParseUint(value, 10, 64)
return result, err == nil
}
return 0, false
}
func toFloat(v interface{}) (float64, bool) {
switch value := v.(type) {
case int64:
return float64(value), true
case uint64:
return float64(value), true
case float64:
return value, true
case bool:
if value {
return 1.0, true
} else {
return 0.0, true
}
case string:
result, err := strconv.ParseFloat(value, 64)
return result, err == nil
}
return 0.0, false
}
func toString(v interface{}) (string, bool) {
switch value := v.(type) {
case int64:
return strconv.FormatInt(value, 10), true
case uint64:
return strconv.FormatUint(value, 10), true
case float64:
return strconv.FormatFloat(value, 'f', -1, 64), true
case bool:
return strconv.FormatBool(value), true
case string:
return value, true
}
return "", false
}
func logPrintf(format string, v ...interface{}) {
log.Printf("D! [processors.converter] "+format, v...)
}
func init() {
processors.Add("converter", func() telegraf.Processor {
return &Converter{}
})
}