From 1d031590559ecb2d468661b20e0b17c43dc0df43 Mon Sep 17 00:00:00 2001 From: Alexander Shepelin Date: Sat, 7 Jul 2018 01:02:25 +0300 Subject: [PATCH] Don't emit empty tags and fields when pattern doesn't match and result_key specified --- plugins/processors/regex/regex.go | 8 ++++++-- plugins/processors/regex/regex_test.go | 3 +-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/plugins/processors/regex/regex.go b/plugins/processors/regex/regex.go index 3282406c8599b..f73ed06b61cd1 100644 --- a/plugins/processors/regex/regex.go +++ b/plugins/processors/regex/regex.go @@ -67,7 +67,9 @@ 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) + } } } @@ -75,7 +77,9 @@ func (r *Regex) Apply(in ...telegraf.Metric) []telegraf.Metric { 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) + } } } } diff --git a/plugins/processors/regex/regex_test.go b/plugins/processors/regex/regex_test.go index e7c15e5aaef52..bae46d5d875dd 100644 --- a/plugins/processors/regex/regex_test.go +++ b/plugins/processors/regex/regex_test.go @@ -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", @@ -231,7 +231,6 @@ func TestNoMatches(t *testing.T) { }, expectedFields: map[string]interface{}{ "request": "/users/42/", - "new_field": "", }, }, }