Use operation subtables in enum and rename processors (#4672)

This commit is contained in:
Daniel Nelson
2018-09-11 16:03:47 -07:00
committed by GitHub
parent 9d72d078a3
commit eff7f0f083
6 changed files with 93 additions and 101 deletions

View File

@@ -5,28 +5,23 @@ The `rename` processor renames measurements, fields, and tags.
### Configuration:
```toml
## Measurement, tag, and field renamings are stored in separate sub-tables.
## Specify one sub-table per rename operation.
[[processors.rename]]
[[processors.rename.measurement]]
## measurement to change
from = "network_interface_throughput"
to = "throughput"
## Specify one sub-table per rename operation.
[[processors.rename.replace]]
measurement = "network_interface_throughput"
dest = "throughput"
[[processors.rename.tag]]
## tag to change
from = "hostname"
to = "host"
[[processors.rename.replace]]
tag = "hostname"
dest = "host"
[[processors.rename.field]]
## field to change
from = "lower"
to = "min"
[[processors.rename.replace]]
field = "lower"
dest = "min"
[[processors.rename.field]]
## field to change
from = "upper"
to = "max"
[[processors.rename.replace]]
field = "upper"
dest = "max"
```
### Tags:
@@ -36,6 +31,6 @@ No tags are applied by this processor, though it can alter them by renaming.
### Example processing:
```diff
- network_interface_throughput,hostname=backend.example.com,units=kbps lower=10i,upper=1000i,mean=500i 1502489900000000000
+ throughput,host=backend.example.com,units=kbps min=10i,max=1000i,mean=500i 1502489900000000000
- network_interface_throughput,hostname=backend.example.com lower=10i,upper=1000i,mean=500i 1502489900000000000
+ throughput,host=backend.example.com min=10i,max=1000i,mean=500i 1502489900000000000
```

View File

@@ -6,38 +6,17 @@ import (
)
const sampleConfig = `
## Measurement, tag, and field renamings are stored in separate sub-tables.
## Specify one sub-table per rename operation.
# [[processors.rename.measurement]]
# ## measurement to change
# from = "kilobytes_per_second"
# to = "kbps"
# [[processors.rename.tag]]
# ## tag to change
# from = "host"
# to = "hostname"
# [[processors.rename.field]]
# ## field to change
# from = "lower"
# to = "min"
# [[processors.rename.field]]
# ## field to change
# from = "upper"
# to = "max"
`
type renamer struct {
From string
To string
type Replace struct {
Measurement string `toml:"measurement"`
Tag string `toml:"tag"`
Field string `toml:"field"`
Dest string `toml:"dest"`
}
type Rename struct {
Measurement []renamer
Tag []renamer
Field []renamer
Replaces []Replace `toml:"replace"`
}
func (r *Rename) SampleConfig() string {
@@ -50,24 +29,32 @@ func (r *Rename) Description() string {
func (r *Rename) Apply(in ...telegraf.Metric) []telegraf.Metric {
for _, point := range in {
for _, measurementRenamer := range r.Measurement {
if point.Name() == measurementRenamer.From {
point.SetName(measurementRenamer.To)
break
for _, replace := range r.Replaces {
if replace.Dest == "" {
continue
}
}
for _, tagRenamer := range r.Tag {
if value, ok := point.GetTag(tagRenamer.From); ok {
point.RemoveTag(tagRenamer.From)
point.AddTag(tagRenamer.To, value)
if replace.Measurement != "" {
if value := point.Name(); value == replace.Measurement {
point.SetName(replace.Dest)
}
continue
}
}
for _, fieldRenamer := range r.Field {
if value, ok := point.GetField(fieldRenamer.From); ok {
point.RemoveField(fieldRenamer.From)
point.AddField(fieldRenamer.To, value)
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
}
}
}

View File

@@ -21,10 +21,11 @@ func newMetric(name string, tags map[string]string, fields map[string]interface{
}
func TestMeasurementRename(t *testing.T) {
r := Rename{}
r.Measurement = []renamer{
{From: "foo", To: "bar"},
{From: "baz", To: "quux"},
r := Rename{
Replaces: []Replace{
{Measurement: "foo", Dest: "bar"},
{Measurement: "baz", Dest: "quux"},
},
}
m1 := newMetric("foo", nil, nil)
m2 := newMetric("bar", nil, nil)
@@ -36,9 +37,10 @@ func TestMeasurementRename(t *testing.T) {
}
func TestTagRename(t *testing.T) {
r := Rename{}
r.Tag = []renamer{
{From: "hostname", To: "host"},
r := Rename{
Replaces: []Replace{
{Tag: "hostname", Dest: "host"},
},
}
m := newMetric("foo", map[string]string{"hostname": "localhost", "region": "east-1"}, nil)
results := r.Apply(m)
@@ -47,9 +49,10 @@ func TestTagRename(t *testing.T) {
}
func TestFieldRename(t *testing.T) {
r := Rename{}
r.Field = []renamer{
{From: "time_msec", To: "time"},
r := Rename{
Replaces: []Replace{
{Field: "time_msec", Dest: "time"},
},
}
m := newMetric("foo", nil, map[string]interface{}{"time_msec": int64(1250), "snakes": true})
results := r.Apply(m)