fix(processors.parser): Keep name of original metric if parser doesn't return one (#12116)

This commit is contained in:
Sven Rebhan 2022-10-27 17:56:12 +02:00 committed by GitHub
parent 49ac03fa80
commit 7d37ddfe51
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 53 additions and 1 deletions

View File

@ -49,7 +49,13 @@ func (p *Parser) Apply(metrics ...telegraf.Metric) []telegraf.Metric {
}
for _, m := range fromFieldMetric {
if m.Name() == "" {
// The parser get the parent plugin's name as
// default measurement name. Thus, in case the
// parsed metric does not provide a name itself,
// the parser will return 'parser' as we are in
// processors.parser. In those cases we want to
// keep the original metric name.
if m.Name() == "" || m.Name() == "parser" {
m.SetName(metric.Name())
}
}

View File

@ -10,10 +10,12 @@ import (
//Blank import to register all new-style parsers
"github.com/influxdata/telegraf/plugins/parsers"
"github.com/influxdata/telegraf/plugins/parsers/grok"
"github.com/influxdata/telegraf/plugins/parsers/influx"
"github.com/influxdata/telegraf/plugins/parsers/json"
"github.com/influxdata/telegraf/plugins/parsers/logfmt"
"github.com/influxdata/telegraf/plugins/parsers/value"
"github.com/influxdata/telegraf/testutil"
)
@ -558,6 +560,50 @@ func TestApply(t *testing.T) {
time.Unix(0, 0)),
},
},
{
name: "parser without metric name (issue #12115)",
parseFields: []string{"value"},
merge: "override",
// Create parser the config way with the name of the parent plugin.
parser: func() telegraf.Parser {
p := parsers.Parsers["value"]("parser")
vp := p.(*value.Parser)
vp.DataType = "float"
vp.FieldName = "value"
return vp
}(),
input: metric.New(
"myname",
map[string]string{},
map[string]interface{}{"value": "7.2"},
time.Unix(0, 0)),
expected: []telegraf.Metric{
metric.New(
"myname",
map[string]string{},
map[string]interface{}{"value": float64(7.2)},
time.Unix(0, 0)),
},
},
{
name: "parser with metric name (issue #12115)",
parseFields: []string{"value"},
merge: "override",
// Create parser the config way with the name of the parent plugin.
parser: parsers.Parsers["influx"]("parser"),
input: metric.New(
"myname",
map[string]string{},
map[string]interface{}{"value": "test value=7.2"},
time.Unix(0, 0)),
expected: []telegraf.Metric{
metric.New(
"test",
map[string]string{},
map[string]interface{}{"value": float64(7.2)},
time.Unix(0, 0)),
},
},
}
for _, tt := range tests {