Fix parsing of multiple files with different headers (#6318). (#8400)

This commit is contained in:
Sven Rebhan 2020-11-13 16:36:08 +01:00 committed by GitHub
parent fb463bcc17
commit 049daf7892
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 10 additions and 3 deletions

View File

@ -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 {