Skip to content

Commit

Permalink
Don't emit empty tags and fields when pattern doesn't match and resul…
Browse files Browse the repository at this point in the history
…t_key specified
  • Loading branch information
44px committed Jul 6, 2018
1 parent a0ece79 commit 1d03159
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
8 changes: 6 additions & 2 deletions plugins/processors/regex/regex.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,19 @@ func (r *Regex) Apply(in ...telegraf.Metric) []telegraf.Metric {
for _, metric := range in {
for _, converter := range r.Tags {
if value, ok := metric.GetTag(converter.Key); ok {
metric.AddTag(r.convert(converter, value))
if key, newValue := r.convert(converter, value); newValue != "" {
metric.AddTag(key, newValue)
}
}
}

for _, converter := range r.Fields {
if value, ok := metric.GetField(converter.Key); ok {
switch value := value.(type) {
case string:
metric.AddField(r.convert(converter, value))
if key, newValue := r.convert(converter, value); newValue != "" {
metric.AddField(key, newValue)
}
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions plugins/processors/regex/regex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ func TestNoMatches(t *testing.T) {
},
},
{
message: "Should emit empty string when result_key given but regex doesn't match",
message: "Should not emit new tag/field when result_key given but regex doesn't match",
converter: converter{
Key: "request",
Pattern: "not_match",
Expand All @@ -231,7 +231,6 @@ func TestNoMatches(t *testing.T) {
},
expectedFields: map[string]interface{}{
"request": "/users/42/",
"new_field": "",
},
},
}
Expand Down

0 comments on commit 1d03159

Please sign in to comment.