forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pivot.go
54 lines (45 loc) · 985 Bytes
/
pivot.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package pivot
import (
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/processors"
)
const (
description = "Rotate a single valued metric into a multi field metric"
sampleConfig = `
## Tag to use for naming the new field.
tag_key = "name"
## Field to use as the value of the new field.
value_key = "value"
`
)
type Pivot struct {
TagKey string `toml:"tag_key"`
ValueKey string `toml:"value_key"`
}
func (p *Pivot) SampleConfig() string {
return sampleConfig
}
func (p *Pivot) Description() string {
return description
}
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{}
})
}