From 049daf7892a8fdd9e9c1bb2b4e427897d0745678 Mon Sep 17 00:00:00 2001 From: Sven Rebhan <36194019+srebhan@users.noreply.github.com> Date: Fri, 13 Nov 2020 16:36:08 +0100 Subject: [PATCH] Fix parsing of multiple files with different headers (#6318). (#8400) --- plugins/parsers/csv/parser.go | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/plugins/parsers/csv/parser.go b/plugins/parsers/csv/parser.go index 76d8306ea..87e403273 100644 --- a/plugins/parsers/csv/parser.go +++ b/plugins/parsers/csv/parser.go @@ -32,6 +32,8 @@ type Config struct { Timezone string `toml:"csv_timezone"` TrimSpace bool `toml:"csv_trim_space"` + gotColumnNames bool + TimeFunc func() time.Time DefaultTags map[string]string } @@ -64,6 +66,8 @@ func NewParser(c *Config) (*Parser, error) { return nil, fmt.Errorf("csv_column_names field count doesn't match with csv_column_types") } + c.gotColumnNames = len(c.ColumnNames) > 0 + if c.TimeFunc == nil { c.TimeFunc = time.Now } @@ -102,10 +106,13 @@ func (p *Parser) Parse(buf []byte) ([]telegraf.Metric, error) { return nil, err } } - // if there is a header and nothing in DataColumns + // if there is a header and we did not get DataColumns // set DataColumns to names extracted from the header - headerNames := make([]string, 0) - if len(p.ColumnNames) == 0 { + // we always reread the header to avoid side effects + // in cases where multiple files with different + // headers are read + if !p.gotColumnNames { + headerNames := make([]string, 0) for i := 0; i < p.HeaderRowCount; i++ { header, err := csvReader.Read() if err != nil {