forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rename.go
69 lines (57 loc) · 1.33 KB
/
rename.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
package rename
import (
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/processors"
)
const sampleConfig = `
`
type Replace struct {
Measurement string `toml:"measurement"`
Tag string `toml:"tag"`
Field string `toml:"field"`
Dest string `toml:"dest"`
}
type Rename struct {
Replaces []Replace `toml:"replace"`
}
func (r *Rename) SampleConfig() string {
return sampleConfig
}
func (r *Rename) Description() string {
return "Rename measurements, tags, and fields that pass through this filter."
}
func (r *Rename) Apply(in ...telegraf.Metric) []telegraf.Metric {
for _, point := range in {
for _, replace := range r.Replaces {
if replace.Dest == "" {
continue
}
if replace.Measurement != "" {
if value := point.Name(); value == replace.Measurement {
point.SetName(replace.Dest)
}
continue
}
if replace.Tag != "" {
if value, ok := point.GetTag(replace.Tag); ok {
point.RemoveTag(replace.Tag)
point.AddTag(replace.Dest, value)
}
continue
}
if replace.Field != "" {
if value, ok := point.GetField(replace.Field); ok {
point.RemoveField(replace.Field)
point.AddField(replace.Dest, value)
}
continue
}
}
}
return in
}
func init() {
processors.Add("rename", func() telegraf.Processor {
return &Rename{}
})
}