Allow regexp processor to mix different tags (#5863)

This commit is contained in:
Adrián López 2019-06-15 00:23:54 +02:00 committed by Daniel Nelson
parent 0ff9c8ef88
commit d3af8fd873
3 changed files with 22 additions and 0 deletions

View File

@ -2,6 +2,8 @@
The `regex` plugin transforms tag and field values with regex pattern. If `result_key` parameter is present, it can produce new tags and fields from existing ones. The `regex` plugin transforms tag and field values with regex pattern. If `result_key` parameter is present, it can produce new tags and fields from existing ones.
For tags transforms, if `append` is set to `true`, it will append the transformation to the existing tag value, instead of overwriting it.
### Configuration: ### Configuration:
```toml ```toml

View File

@ -18,6 +18,7 @@ type converter struct {
Pattern string Pattern string
Replacement string Replacement string
ResultKey string ResultKey string
Append bool
} }
const sampleConfig = ` const sampleConfig = `
@ -70,6 +71,11 @@ func (r *Regex) Apply(in ...telegraf.Metric) []telegraf.Metric {
for _, converter := range r.Tags { for _, converter := range r.Tags {
if value, ok := metric.GetTag(converter.Key); ok { if value, ok := metric.GetTag(converter.Key); ok {
if key, newValue := r.convert(converter, value); newValue != "" { if key, newValue := r.convert(converter, value); newValue != "" {
if converter.Append {
if v, ok := metric.GetTag(key); ok {
newValue = v + newValue
}
}
metric.AddTag(key, newValue) metric.AddTag(key, newValue)
} }
} }

View File

@ -108,6 +108,20 @@ func TestTagConversions(t *testing.T) {
"resp_code": "2xx", "resp_code": "2xx",
}, },
}, },
{
message: "Should append to existing tag",
converter: converter{
Key: "verb",
Pattern: "^(.*)$",
Replacement: " (${1})",
ResultKey: "resp_code",
Append: true,
},
expectedTags: map[string]string{
"verb": "GET",
"resp_code": "200 (GET)",
},
},
{ {
message: "Should add new tag", message: "Should add new tag",
converter: converter{ converter: converter{