Don't set values when pattern doesn't match in regex processor (#4396)
(cherry picked from commit 35d2f90d6c
)
This commit is contained in:
parent
858335b1c4
commit
63d07f4272
|
@ -67,7 +67,9 @@ func (r *Regex) Apply(in ...telegraf.Metric) []telegraf.Metric {
|
||||||
for _, metric := range in {
|
for _, metric := range in {
|
||||||
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 {
|
||||||
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 {
|
if value, ok := metric.GetField(converter.Key); ok {
|
||||||
switch value := value.(type) {
|
switch value := value.(type) {
|
||||||
case string:
|
case string:
|
||||||
metric.AddField(r.convert(converter, value))
|
if key, newValue := r.convert(converter, value); newValue != "" {
|
||||||
|
metric.AddField(key, newValue)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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{
|
converter: converter{
|
||||||
Key: "request",
|
Key: "request",
|
||||||
Pattern: "not_match",
|
Pattern: "not_match",
|
||||||
|
@ -231,7 +231,6 @@ func TestNoMatches(t *testing.T) {
|
||||||
},
|
},
|
||||||
expectedFields: map[string]interface{}{
|
expectedFields: map[string]interface{}{
|
||||||
"request": "/users/42/",
|
"request": "/users/42/",
|
||||||
"new_field": "",
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue