Add pivot and unpivot processors (#5991)
This commit is contained in:
26
plugins/processors/unpivot/README.md
Normal file
26
plugins/processors/unpivot/README.md
Normal file
@@ -0,0 +1,26 @@
|
||||
# Unpivot Processor
|
||||
|
||||
You can use the `unpivot` processor to rotate a multi field series into single valued metrics. This transformation often results in data that is more easy to aggregate across fields.
|
||||
|
||||
To perform the reverse operation use the [pivot] processor.
|
||||
|
||||
### Configuration
|
||||
|
||||
```toml
|
||||
[[processors.unpivot]]
|
||||
## Tag to use for the name.
|
||||
tag_key = "name"
|
||||
## Field to use for the name of the value.
|
||||
value_key = "value"
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```diff
|
||||
- cpu,cpu=cpu0 time_idle=42i,time_user=43i
|
||||
+ cpu,cpu=cpu0,name=time_idle value=42i
|
||||
+ cpu,cpu=cpu0,name=time_user value=43i
|
||||
```
|
||||
|
||||
[pivot]: /plugins/processors/pivot/README.md
|
||||
|
||||
71
plugins/processors/unpivot/unpivot.go
Normal file
71
plugins/processors/unpivot/unpivot.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package unpivot
|
||||
|
||||
import (
|
||||
"github.com/influxdata/telegraf"
|
||||
"github.com/influxdata/telegraf/plugins/processors"
|
||||
)
|
||||
|
||||
const (
|
||||
description = "Rotate multi field metric into several single field metrics"
|
||||
sampleConfig = `
|
||||
## Tag to use for the name.
|
||||
tag_key = "name"
|
||||
## Field to use for the name of the value.
|
||||
value_key = "value"
|
||||
`
|
||||
)
|
||||
|
||||
type Unpivot struct {
|
||||
TagKey string `toml:"tag_key"`
|
||||
ValueKey string `toml:"value_key"`
|
||||
}
|
||||
|
||||
func (p *Unpivot) SampleConfig() string {
|
||||
return sampleConfig
|
||||
}
|
||||
|
||||
func (p *Unpivot) Description() string {
|
||||
return description
|
||||
}
|
||||
|
||||
func copyWithoutFields(metric telegraf.Metric) telegraf.Metric {
|
||||
m := metric.Copy()
|
||||
|
||||
fieldKeys := make([]string, 0, len(m.FieldList()))
|
||||
for _, field := range m.FieldList() {
|
||||
fieldKeys = append(fieldKeys, field.Key)
|
||||
}
|
||||
|
||||
for _, fk := range fieldKeys {
|
||||
m.RemoveField(fk)
|
||||
}
|
||||
|
||||
return m
|
||||
}
|
||||
|
||||
func (p *Unpivot) Apply(metrics ...telegraf.Metric) []telegraf.Metric {
|
||||
fieldCount := 0
|
||||
for _, m := range metrics {
|
||||
fieldCount += len(m.FieldList())
|
||||
}
|
||||
|
||||
results := make([]telegraf.Metric, 0, fieldCount)
|
||||
|
||||
for _, m := range metrics {
|
||||
base := copyWithoutFields(m)
|
||||
for _, field := range m.FieldList() {
|
||||
newMetric := base.Copy()
|
||||
newMetric.AddField(p.ValueKey, field.Value)
|
||||
newMetric.AddTag(p.TagKey, field.Key)
|
||||
results = append(results, newMetric)
|
||||
}
|
||||
m.Accept()
|
||||
}
|
||||
return results
|
||||
}
|
||||
|
||||
func init() {
|
||||
processors.Add("unpivot", func() telegraf.Processor {
|
||||
return &Unpivot{}
|
||||
})
|
||||
}
|
||||
90
plugins/processors/unpivot/unpivot_test.go
Normal file
90
plugins/processors/unpivot/unpivot_test.go
Normal file
@@ -0,0 +1,90 @@
|
||||
package unpivot
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/influxdata/telegraf"
|
||||
"github.com/influxdata/telegraf/testutil"
|
||||
)
|
||||
|
||||
func TestUnpivot(t *testing.T) {
|
||||
now := time.Now()
|
||||
tests := []struct {
|
||||
name string
|
||||
unpivot *Unpivot
|
||||
metrics []telegraf.Metric
|
||||
expected []telegraf.Metric
|
||||
}{
|
||||
{
|
||||
name: "simple",
|
||||
unpivot: &Unpivot{
|
||||
TagKey: "name",
|
||||
ValueKey: "value",
|
||||
},
|
||||
metrics: []telegraf.Metric{
|
||||
testutil.MustMetric("cpu",
|
||||
map[string]string{},
|
||||
map[string]interface{}{
|
||||
"idle_time": int64(42),
|
||||
},
|
||||
now,
|
||||
),
|
||||
},
|
||||
expected: []telegraf.Metric{
|
||||
testutil.MustMetric("cpu",
|
||||
map[string]string{
|
||||
"name": "idle_time",
|
||||
},
|
||||
map[string]interface{}{
|
||||
"value": int64(42),
|
||||
},
|
||||
now,
|
||||
),
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "multi fields",
|
||||
unpivot: &Unpivot{
|
||||
TagKey: "name",
|
||||
ValueKey: "value",
|
||||
},
|
||||
metrics: []telegraf.Metric{
|
||||
testutil.MustMetric("cpu",
|
||||
map[string]string{},
|
||||
map[string]interface{}{
|
||||
"idle_time": int64(42),
|
||||
"idle_user": int64(43),
|
||||
},
|
||||
now,
|
||||
),
|
||||
},
|
||||
expected: []telegraf.Metric{
|
||||
testutil.MustMetric("cpu",
|
||||
map[string]string{
|
||||
"name": "idle_time",
|
||||
},
|
||||
map[string]interface{}{
|
||||
"value": int64(42),
|
||||
},
|
||||
now,
|
||||
),
|
||||
testutil.MustMetric("cpu",
|
||||
map[string]string{
|
||||
"name": "idle_user",
|
||||
},
|
||||
map[string]interface{}{
|
||||
"value": int64(43),
|
||||
},
|
||||
now,
|
||||
),
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
actual := tt.unpivot.Apply(tt.metrics...)
|
||||
testutil.RequireMetricsEqual(t, tt.expected, actual, testutil.SortMetrics())
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user