fix(processors.parser): Keep name of original metric if parser doesn't return one (#12116)
This commit is contained in:
parent
49ac03fa80
commit
7d37ddfe51
|
|
@ -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())
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
Loading…
Reference in New Issue