telegraf/plugins/processors/rename/rename.go

70 lines
1.3 KiB
Go
Raw Normal View History

2018-08-13 23:38:46 +00:00
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"`
2018-08-13 23:38:46 +00:00
}
type Rename struct {
Replaces []Replace `toml:"replace"`
2018-08-13 23:38:46 +00:00
}
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
2018-08-13 23:38:46 +00:00
}
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
2018-08-13 23:38:46 +00:00
}
if replace.Field != "" {
if value, ok := point.GetField(replace.Field); ok {
point.RemoveField(replace.Field)
point.AddField(replace.Dest, value)
}
continue
2018-08-13 23:38:46 +00:00
}
}
}
return in
}
func init() {
processors.Add("rename", func() telegraf.Processor {
return &Rename{}
})
}