2018-08-13 23:38:46 +00:00
|
|
|
package rename
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/influxdata/telegraf"
|
|
|
|
"github.com/influxdata/telegraf/plugins/processors"
|
|
|
|
)
|
|
|
|
|
|
|
|
const sampleConfig = `
|
|
|
|
`
|
|
|
|
|
2018-09-11 23:03:47 +00:00
|
|
|
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 {
|
2018-09-11 23:03:47 +00:00
|
|
|
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 {
|
2018-09-11 23:03:47 +00:00
|
|
|
for _, replace := range r.Replaces {
|
|
|
|
if replace.Dest == "" {
|
|
|
|
continue
|
2018-08-13 23:38:46 +00:00
|
|
|
}
|
|
|
|
|
2018-09-11 23:03:47 +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
|
|
|
}
|
|
|
|
|
2018-09-11 23:03:47 +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{}
|
|
|
|
})
|
|
|
|
}
|