Skip to content

Commit 9d8b1b1

Browse files
KarstenSchnitterdanielnelson
authored andcommitted
Add override processor (influxdata#3773)
This plugin can perform the standard metric modifications using override semantics.
1 parent b9ddbbd commit 9d8b1b1

File tree

4 files changed

+171
-0
lines changed

4 files changed

+171
-0
lines changed

plugins/processors/all/all.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package all
22

33
import (
4+
_ "github.com/influxdata/telegraf/plugins/processors/override"
45
_ "github.com/influxdata/telegraf/plugins/processors/printer"
56
)

plugins/processors/override/README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Override Processor Plugin
2+
3+
The override processor plugin allows overriding all modifications that are
4+
supported by input plugins and aggregators:
5+
6+
* name_override
7+
* name_prefix
8+
* name_suffix
9+
* tags
10+
11+
All metrics passing through this processor will be modified accordingly. Values
12+
of *name_override*, *name_prefix*, *name_suffix* and already present *tags* with
13+
conflicting keys will be overwritten. Absent *tags* will be created.
14+
15+
Use-case of this plugin encompass ensuring certain tags or naming conventions
16+
are adhered to irrespective of input plugin configurations, e.g. by
17+
`taginclude`.
18+
19+
### Configuration:
20+
21+
```toml
22+
# Add a global tag to all metrics
23+
[[processors.override]]
24+
name_override = "new name_override"
25+
name_prefix = "new name_prefix"
26+
name_suffix = ":new name_suffix"
27+
[processors.tags.add]
28+
additional_tag = "tag_value"
29+
existing_tag = "new tag_value"
30+
```
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package override
2+
3+
import (
4+
"github.com/influxdata/telegraf"
5+
"github.com/influxdata/telegraf/plugins/processors"
6+
)
7+
8+
var sampleConfig = `
9+
## NOTE This processor will override names, name prefixes, name suffixes and
10+
## values of tags, that are already present in the metric passed through this
11+
## filter.
12+
13+
## All modifications on inputs and aggregators can be overridden:
14+
# name_override = "new name"
15+
# name_prefix = "new name_prefix"
16+
# name_suffix = "new name_suffix"
17+
18+
## Tags to be added (all values must be strings)
19+
# [processors.overide.tags]
20+
# additional_tag = "tag_value"
21+
`
22+
23+
type Override struct {
24+
NameOverride string
25+
NamePrefix string
26+
NameSuffix string
27+
Tags map[string]string
28+
}
29+
30+
func (p *Override) SampleConfig() string {
31+
return sampleConfig
32+
}
33+
34+
func (p *Override) Description() string {
35+
return "Add all configured tags to all metrics that pass through this filter."
36+
}
37+
38+
func (p *Override) Apply(in ...telegraf.Metric) []telegraf.Metric {
39+
for _, metric := range in {
40+
if len(p.NameOverride) > 0 {
41+
metric.SetName(p.NameOverride)
42+
}
43+
if len(p.NamePrefix) > 0 {
44+
metric.SetPrefix(p.NamePrefix)
45+
}
46+
if len(p.NameSuffix) > 0 {
47+
metric.SetSuffix(p.NameSuffix)
48+
}
49+
for key, value := range p.Tags {
50+
metric.AddTag(key, value)
51+
}
52+
}
53+
return in
54+
}
55+
56+
func init() {
57+
processors.Add("override", func() telegraf.Processor {
58+
return &Override{}
59+
})
60+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package override
2+
3+
import (
4+
"testing"
5+
"time"
6+
7+
"github.com/influxdata/telegraf"
8+
"github.com/influxdata/telegraf/metric"
9+
"github.com/stretchr/testify/assert"
10+
)
11+
12+
func createTestMetric() telegraf.Metric {
13+
metric, _ := metric.New("m1",
14+
map[string]string{"metric_tag": "from_metric"},
15+
map[string]interface{}{"value": int64(1)},
16+
time.Now(),
17+
)
18+
return metric
19+
}
20+
21+
func calculateProcessedTags(processor Override, metric telegraf.Metric) map[string]string {
22+
processed := processor.Apply(metric)
23+
return processed[0].Tags()
24+
}
25+
26+
func TestRetainsTags(t *testing.T) {
27+
processor := Override{}
28+
29+
tags := calculateProcessedTags(processor, createTestMetric())
30+
31+
value, present := tags["metric_tag"]
32+
assert.True(t, present, "Tag of metric was not present")
33+
assert.Equal(t, "from_metric", value, "Value of Tag was changed")
34+
}
35+
36+
func TestAddTags(t *testing.T) {
37+
processor := Override{Tags: map[string]string{"added_tag": "from_config", "another_tag": ""}}
38+
39+
tags := calculateProcessedTags(processor, createTestMetric())
40+
41+
value, present := tags["added_tag"]
42+
assert.True(t, present, "Additional Tag of metric was not present")
43+
assert.Equal(t, "from_config", value, "Value of Tag was changed")
44+
assert.Equal(t, 3, len(tags), "Should have one previous and two added tags.")
45+
}
46+
47+
func TestOverwritesPresentTagValues(t *testing.T) {
48+
processor := Override{Tags: map[string]string{"metric_tag": "from_config"}}
49+
50+
tags := calculateProcessedTags(processor, createTestMetric())
51+
52+
value, present := tags["metric_tag"]
53+
assert.True(t, present, "Tag of metric was not present")
54+
assert.Equal(t, 1, len(tags), "Should only have one tag.")
55+
assert.Equal(t, "from_config", value, "Value of Tag was not changed")
56+
}
57+
58+
func TestOverridesName(t *testing.T) {
59+
processor := Override{NameOverride: "overridden"}
60+
61+
processed := processor.Apply(createTestMetric())
62+
63+
assert.Equal(t, "overridden", processed[0].Name(), "Name was not overridden")
64+
}
65+
66+
func TestNamePrefix(t *testing.T) {
67+
processor := Override{NamePrefix: "Pre-"}
68+
69+
processed := processor.Apply(createTestMetric())
70+
71+
assert.Equal(t, "Pre-m1", processed[0].Name(), "Prefix was not applied")
72+
}
73+
74+
func TestNameSuffix(t *testing.T) {
75+
processor := Override{NameSuffix: "-suff"}
76+
77+
processed := processor.Apply(createTestMetric())
78+
79+
assert.Equal(t, "m1-suff", processed[0].Name(), "Suffix was not applied")
80+
}

0 commit comments

Comments
 (0)