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
40
plugins/processors/pivot/README.md
Normal file
40
plugins/processors/pivot/README.md
Normal file
|
@ -0,0 +1,40 @@
|
|||
# Pivot Processor Plugin
|
||||
|
||||
You can use the `pivot` processor to rotate single valued metrics into a multi
|
||||
field metric. This transformation often results in data that is more easily
|
||||
to apply mathematical operators and comparisons between, and flatten into a
|
||||
more compact representation for write operations with some output data
|
||||
formats.
|
||||
|
||||
To perform the reverse operation use the [unpivot] processor.
|
||||
|
||||
## 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
|
||||
# Rotate a single valued metric into a multi field metric
|
||||
[[processors.pivot]]
|
||||
## Tag to use for naming the new field.
|
||||
tag_key = "name"
|
||||
## Field to use as the value of the new field.
|
||||
value_key = "value"
|
||||
```
|
||||
|
||||
## Example
|
||||
|
||||
```diff
|
||||
- cpu,cpu=cpu0,name=time_idle value=42i
|
||||
- cpu,cpu=cpu0,name=time_user value=43i
|
||||
+ cpu,cpu=cpu0 time_idle=42i
|
||||
+ cpu,cpu=cpu0 time_user=43i
|
||||
```
|
||||
|
||||
[unpivot]: /plugins/processors/unpivot/README.md
|
46
plugins/processors/pivot/pivot.go
Normal file
46
plugins/processors/pivot/pivot.go
Normal file
|
@ -0,0 +1,46 @@
|
|||
//go:generate ../../../tools/readme_config_includer/generator
|
||||
package pivot
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
|
||||
"github.com/influxdata/telegraf"
|
||||
"github.com/influxdata/telegraf/plugins/processors"
|
||||
)
|
||||
|
||||
//go:embed sample.conf
|
||||
var sampleConfig string
|
||||
|
||||
type Pivot struct {
|
||||
TagKey string `toml:"tag_key"`
|
||||
ValueKey string `toml:"value_key"`
|
||||
}
|
||||
|
||||
func (*Pivot) SampleConfig() string {
|
||||
return sampleConfig
|
||||
}
|
||||
|
||||
func (p *Pivot) Apply(metrics ...telegraf.Metric) []telegraf.Metric {
|
||||
for _, m := range metrics {
|
||||
key, ok := m.GetTag(p.TagKey)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
value, ok := m.GetField(p.ValueKey)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
m.RemoveTag(p.TagKey)
|
||||
m.RemoveField(p.ValueKey)
|
||||
m.AddField(key, value)
|
||||
}
|
||||
return metrics
|
||||
}
|
||||
|
||||
func init() {
|
||||
processors.Add("pivot", func() telegraf.Processor {
|
||||
return &Pivot{}
|
||||
})
|
||||
}
|
198
plugins/processors/pivot/pivot_test.go
Normal file
198
plugins/processors/pivot/pivot_test.go
Normal file
|
@ -0,0 +1,198 @@
|
|||
package pivot
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/influxdata/telegraf"
|
||||
"github.com/influxdata/telegraf/metric"
|
||||
"github.com/influxdata/telegraf/testutil"
|
||||
)
|
||||
|
||||
func TestPivot(t *testing.T) {
|
||||
now := time.Now()
|
||||
tests := []struct {
|
||||
name string
|
||||
pivot *Pivot
|
||||
metrics []telegraf.Metric
|
||||
expected []telegraf.Metric
|
||||
}{
|
||||
{
|
||||
name: "simple",
|
||||
pivot: &Pivot{
|
||||
TagKey: "name",
|
||||
ValueKey: "value",
|
||||
},
|
||||
metrics: []telegraf.Metric{
|
||||
testutil.MustMetric("cpu",
|
||||
map[string]string{
|
||||
"name": "idle_time",
|
||||
},
|
||||
map[string]interface{}{
|
||||
"value": int64(42),
|
||||
},
|
||||
now,
|
||||
),
|
||||
},
|
||||
expected: []telegraf.Metric{
|
||||
testutil.MustMetric("cpu",
|
||||
map[string]string{},
|
||||
map[string]interface{}{
|
||||
"idle_time": int64(42),
|
||||
},
|
||||
now,
|
||||
),
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "missing tag",
|
||||
pivot: &Pivot{
|
||||
TagKey: "name",
|
||||
ValueKey: "value",
|
||||
},
|
||||
metrics: []telegraf.Metric{
|
||||
testutil.MustMetric("cpu",
|
||||
map[string]string{
|
||||
"foo": "idle_time",
|
||||
},
|
||||
map[string]interface{}{
|
||||
"value": int64(42),
|
||||
},
|
||||
now,
|
||||
),
|
||||
},
|
||||
expected: []telegraf.Metric{
|
||||
testutil.MustMetric("cpu",
|
||||
map[string]string{
|
||||
"foo": "idle_time",
|
||||
},
|
||||
map[string]interface{}{
|
||||
"value": int64(42),
|
||||
},
|
||||
now,
|
||||
),
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "missing field",
|
||||
pivot: &Pivot{
|
||||
TagKey: "name",
|
||||
ValueKey: "value",
|
||||
},
|
||||
metrics: []telegraf.Metric{
|
||||
testutil.MustMetric("cpu",
|
||||
map[string]string{
|
||||
"name": "idle_time",
|
||||
},
|
||||
map[string]interface{}{
|
||||
"foo": int64(42),
|
||||
},
|
||||
now,
|
||||
),
|
||||
},
|
||||
expected: []telegraf.Metric{
|
||||
testutil.MustMetric("cpu",
|
||||
map[string]string{
|
||||
"name": "idle_time",
|
||||
},
|
||||
map[string]interface{}{
|
||||
"foo": int64(42),
|
||||
},
|
||||
now,
|
||||
),
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
actual := tt.pivot.Apply(tt.metrics...)
|
||||
testutil.RequireMetricsEqual(t, tt.expected, actual)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestTracking(t *testing.T) {
|
||||
// Setup raw input and expected output
|
||||
inputRaw := []telegraf.Metric{
|
||||
metric.New(
|
||||
"test",
|
||||
map[string]string{"name": "idle_time"},
|
||||
map[string]interface{}{"value": uint64(3)},
|
||||
time.Unix(0, 0),
|
||||
),
|
||||
metric.New(
|
||||
"test",
|
||||
map[string]string{"name": "system_time"},
|
||||
map[string]interface{}{"value": int64(4)},
|
||||
time.Unix(0, 0),
|
||||
),
|
||||
metric.New(
|
||||
"test",
|
||||
map[string]string{"name": "user_time"},
|
||||
map[string]interface{}{"value": float64(5.5)},
|
||||
time.Unix(0, 0),
|
||||
),
|
||||
}
|
||||
|
||||
expected := []telegraf.Metric{
|
||||
metric.New(
|
||||
"test",
|
||||
map[string]string{},
|
||||
map[string]interface{}{"idle_time": uint64(3)},
|
||||
time.Unix(0, 0),
|
||||
),
|
||||
metric.New(
|
||||
"test",
|
||||
map[string]string{},
|
||||
map[string]interface{}{"system_time": int64(4)},
|
||||
time.Unix(0, 0),
|
||||
),
|
||||
metric.New(
|
||||
"test",
|
||||
map[string]string{},
|
||||
map[string]interface{}{"user_time": float64(5.5)},
|
||||
time.Unix(0, 0),
|
||||
),
|
||||
}
|
||||
|
||||
// Create fake notification for testing
|
||||
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)
|
||||
}
|
||||
|
||||
// Convert raw input to tracking metric
|
||||
input := make([]telegraf.Metric, 0, len(inputRaw))
|
||||
for _, m := range inputRaw {
|
||||
tm, _ := metric.WithTracking(m, notify)
|
||||
input = append(input, tm)
|
||||
}
|
||||
|
||||
// Prepare and start the plugin
|
||||
plugin := &Pivot{
|
||||
TagKey: "name",
|
||||
ValueKey: "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))
|
||||
}
|
6
plugins/processors/pivot/sample.conf
Normal file
6
plugins/processors/pivot/sample.conf
Normal file
|
@ -0,0 +1,6 @@
|
|||
# Rotate a single valued metric into a multi field metric
|
||||
[[processors.pivot]]
|
||||
## Tag to use for naming the new field.
|
||||
tag_key = "name"
|
||||
## Field to use as the value of the new field.
|
||||
value_key = "value"
|
Loading…
Add table
Add a link
Reference in a new issue