Adding upstream version 1.34.4.
Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
parent
e393c3af3f
commit
4978089aab
4963 changed files with 677545 additions and 0 deletions
46
plugins/processors/rename/README.md
Normal file
46
plugins/processors/rename/README.md
Normal file
|
@ -0,0 +1,46 @@
|
|||
# Rename Processor Plugin
|
||||
|
||||
The `rename` processor renames measurements, fields, and tags.
|
||||
|
||||
## Global configuration options <!-- @/docs/includes/plugin_config.md -->
|
||||
|
||||
In addition to the plugin-specific configuration settings, plugins support
|
||||
additional global and plugin configuration settings. These settings are used to
|
||||
modify metrics, tags, and field or create aliases and configure ordering, etc.
|
||||
See the [CONFIGURATION.md][CONFIGURATION.md] for more details.
|
||||
|
||||
[CONFIGURATION.md]: ../../../docs/CONFIGURATION.md#plugins
|
||||
|
||||
## Configuration
|
||||
|
||||
```toml @sample.conf
|
||||
# Rename measurements, tags, and fields that pass through this filter.
|
||||
[[processors.rename]]
|
||||
## Specify one sub-table per rename operation.
|
||||
[[processors.rename.replace]]
|
||||
measurement = "network_interface_throughput"
|
||||
dest = "throughput"
|
||||
|
||||
[[processors.rename.replace]]
|
||||
tag = "hostname"
|
||||
dest = "host"
|
||||
|
||||
[[processors.rename.replace]]
|
||||
field = "lower"
|
||||
dest = "min"
|
||||
|
||||
[[processors.rename.replace]]
|
||||
field = "upper"
|
||||
dest = "max"
|
||||
```
|
||||
|
||||
## Tags
|
||||
|
||||
No tags are applied by this processor, though it can alter them by renaming.
|
||||
|
||||
## Example
|
||||
|
||||
```diff
|
||||
- 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
|
||||
```
|
68
plugins/processors/rename/rename.go
Normal file
68
plugins/processors/rename/rename.go
Normal file
|
@ -0,0 +1,68 @@
|
|||
//go:generate ../../../tools/readme_config_includer/generator
|
||||
package rename
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
|
||||
"github.com/influxdata/telegraf"
|
||||
"github.com/influxdata/telegraf/plugins/processors"
|
||||
)
|
||||
|
||||
//go:embed sample.conf
|
||||
var sampleConfig string
|
||||
|
||||
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 (*Rename) SampleConfig() string {
|
||||
return sampleConfig
|
||||
}
|
||||
|
||||
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{}
|
||||
})
|
||||
}
|
129
plugins/processors/rename/rename_test.go
Normal file
129
plugins/processors/rename/rename_test.go
Normal file
|
@ -0,0 +1,129 @@
|
|||
package rename
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/influxdata/telegraf"
|
||||
"github.com/influxdata/telegraf/metric"
|
||||
"github.com/influxdata/telegraf/testutil"
|
||||
)
|
||||
|
||||
func newMetric(name string, tags map[string]string, fields map[string]interface{}) telegraf.Metric {
|
||||
if tags == nil {
|
||||
tags = map[string]string{}
|
||||
}
|
||||
if fields == nil {
|
||||
fields = map[string]interface{}{}
|
||||
}
|
||||
m := metric.New(name, tags, fields, time.Now())
|
||||
return m
|
||||
}
|
||||
|
||||
func TestMeasurementRename(t *testing.T) {
|
||||
r := Rename{
|
||||
Replaces: []Replace{
|
||||
{Measurement: "foo", Dest: "bar"},
|
||||
{Measurement: "baz", Dest: "quux"},
|
||||
},
|
||||
}
|
||||
m1 := newMetric("foo", nil, nil)
|
||||
m2 := newMetric("bar", nil, nil)
|
||||
m3 := newMetric("baz", nil, nil)
|
||||
results := r.Apply(m1, m2, m3)
|
||||
require.Equal(t, "bar", results[0].Name(), "Should change name from 'foo' to 'bar'")
|
||||
require.Equal(t, "bar", results[1].Name(), "Should not name from 'bar'")
|
||||
require.Equal(t, "quux", results[2].Name(), "Should change name from 'baz' to 'quux'")
|
||||
}
|
||||
|
||||
func TestTagRename(t *testing.T) {
|
||||
r := Rename{
|
||||
Replaces: []Replace{
|
||||
{Tag: "hostname", Dest: "host"},
|
||||
},
|
||||
}
|
||||
m := newMetric("foo", map[string]string{"hostname": "localhost", "region": "east-1"}, nil)
|
||||
results := r.Apply(m)
|
||||
|
||||
require.Equal(t, map[string]string{"host": "localhost", "region": "east-1"}, results[0].Tags(), "should change tag 'hostname' to 'host'")
|
||||
}
|
||||
|
||||
func TestFieldRename(t *testing.T) {
|
||||
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)
|
||||
|
||||
require.Equal(t, map[string]interface{}{"time": int64(1250), "snakes": true}, results[0].Fields(), "should change field 'time_msec' to 'time'")
|
||||
}
|
||||
|
||||
func TestTracking(t *testing.T) {
|
||||
inputRaw := []telegraf.Metric{
|
||||
metric.New("foo", map[string]string{}, map[string]interface{}{"value": 42}, time.Unix(0, 0)),
|
||||
metric.New("bar", map[string]string{}, map[string]interface{}{"value": 99}, time.Unix(0, 0)),
|
||||
metric.New("baz", map[string]string{}, map[string]interface{}{"value": 11}, time.Unix(0, 0)),
|
||||
}
|
||||
|
||||
var mu sync.Mutex
|
||||
delivered := make([]telegraf.DeliveryInfo, 0, len(inputRaw))
|
||||
notify := func(di telegraf.DeliveryInfo) {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
delivered = append(delivered, di)
|
||||
}
|
||||
|
||||
input := make([]telegraf.Metric, 0, len(inputRaw))
|
||||
for _, m := range inputRaw {
|
||||
tm, _ := metric.WithTracking(m, notify)
|
||||
input = append(input, tm)
|
||||
}
|
||||
|
||||
expected := []telegraf.Metric{
|
||||
metric.New(
|
||||
"foo",
|
||||
map[string]string{},
|
||||
map[string]interface{}{"new_value": 42},
|
||||
time.Unix(0, 0),
|
||||
),
|
||||
metric.New(
|
||||
"bar",
|
||||
map[string]string{},
|
||||
map[string]interface{}{"new_value": 99},
|
||||
time.Unix(0, 0),
|
||||
),
|
||||
metric.New(
|
||||
"baz",
|
||||
map[string]string{},
|
||||
map[string]interface{}{"new_value": 11},
|
||||
time.Unix(0, 0),
|
||||
),
|
||||
}
|
||||
|
||||
plugin := &Rename{
|
||||
Replaces: []Replace{
|
||||
{Field: "value", Dest: "new_value"},
|
||||
},
|
||||
}
|
||||
|
||||
// Process expected metrics and compare with resulting metrics
|
||||
actual := plugin.Apply(input...)
|
||||
testutil.RequireMetricsEqual(t, expected, actual)
|
||||
|
||||
// Simulate output acknowledging delivery
|
||||
for _, m := range actual {
|
||||
m.Accept()
|
||||
}
|
||||
|
||||
// Check delivery
|
||||
require.Eventuallyf(t, func() bool {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
return len(input) == len(delivered)
|
||||
}, time.Second, 100*time.Millisecond, "%d delivered but %d expected", len(delivered), len(expected))
|
||||
}
|
18
plugins/processors/rename/sample.conf
Normal file
18
plugins/processors/rename/sample.conf
Normal file
|
@ -0,0 +1,18 @@
|
|||
# Rename measurements, tags, and fields that pass through this filter.
|
||||
[[processors.rename]]
|
||||
## Specify one sub-table per rename operation.
|
||||
[[processors.rename.replace]]
|
||||
measurement = "network_interface_throughput"
|
||||
dest = "throughput"
|
||||
|
||||
[[processors.rename.replace]]
|
||||
tag = "hostname"
|
||||
dest = "host"
|
||||
|
||||
[[processors.rename.replace]]
|
||||
field = "lower"
|
||||
dest = "min"
|
||||
|
||||
[[processors.rename.replace]]
|
||||
field = "upper"
|
||||
dest = "max"
|
Loading…
Add table
Add a link
Reference in a new issue