Skip to content

Commit

Permalink
Allow regexp processor to mix different tags (influxdata#5863)
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianlzt authored and idohalevi committed Sep 23, 2020
1 parent 62c082c commit 7964e65
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
2 changes: 2 additions & 0 deletions plugins/processors/regex/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

For tags transforms, if `append` is set to `true`, it will append the transformation to the existing tag value, instead of overwriting it.

### Configuration:

```toml
Expand Down
6 changes: 6 additions & 0 deletions plugins/processors/regex/regex.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type converter struct {
Pattern string
Replacement string
ResultKey string
Append bool
}

const sampleConfig = `
Expand Down Expand Up @@ -70,6 +71,11 @@ func (r *Regex) Apply(in ...telegraf.Metric) []telegraf.Metric {
for _, converter := range r.Tags {
if value, ok := metric.GetTag(converter.Key); ok {
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)
}
}
Expand Down
14 changes: 14 additions & 0 deletions plugins/processors/regex/regex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,20 @@ func TestTagConversions(t *testing.T) {
"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",
converter: converter{
Expand Down

0 comments on commit 7964e65

Please sign in to comment.