Add csv parser (#4439)
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"github.com/influxdata/telegraf"
|
||||
|
||||
"github.com/influxdata/telegraf/plugins/parsers/collectd"
|
||||
"github.com/influxdata/telegraf/plugins/parsers/csv"
|
||||
"github.com/influxdata/telegraf/plugins/parsers/dropwizard"
|
||||
"github.com/influxdata/telegraf/plugins/parsers/graphite"
|
||||
"github.com/influxdata/telegraf/plugins/parsers/grok"
|
||||
@@ -113,6 +114,19 @@ type Config struct {
|
||||
GrokCustomPatterns string
|
||||
GrokCustomPatternFiles []string
|
||||
GrokTimeZone string
|
||||
|
||||
//csv configuration
|
||||
CSVDelimiter string
|
||||
CSVComment string
|
||||
CSVTrimSpace bool
|
||||
CSVColumnNames []string
|
||||
CSVTagColumns []string
|
||||
CSVMeasurementColumn string
|
||||
CSVTimestampColumn string
|
||||
CSVTimestampFormat string
|
||||
CSVHeaderRowCount int
|
||||
CSVSkipRows int
|
||||
CSVSkipColumns int
|
||||
}
|
||||
|
||||
// NewParser returns a Parser interface based on the given config.
|
||||
@@ -162,6 +176,20 @@ func NewParser(config *Config) (Parser, error) {
|
||||
config.GrokCustomPatterns,
|
||||
config.GrokCustomPatternFiles,
|
||||
config.GrokTimeZone)
|
||||
case "csv":
|
||||
parser, err = newCSVParser(config.MetricName,
|
||||
config.CSVHeaderRowCount,
|
||||
config.CSVSkipRows,
|
||||
config.CSVSkipColumns,
|
||||
config.CSVDelimiter,
|
||||
config.CSVComment,
|
||||
config.CSVTrimSpace,
|
||||
config.CSVColumnNames,
|
||||
config.CSVTagColumns,
|
||||
config.CSVMeasurementColumn,
|
||||
config.CSVTimestampColumn,
|
||||
config.CSVTimestampFormat,
|
||||
config.DefaultTags)
|
||||
case "logfmt":
|
||||
parser, err = NewLogFmtParser(config.MetricName, config.DefaultTags)
|
||||
default:
|
||||
@@ -170,6 +198,60 @@ func NewParser(config *Config) (Parser, error) {
|
||||
return parser, err
|
||||
}
|
||||
|
||||
func newCSVParser(metricName string,
|
||||
header int,
|
||||
skipRows int,
|
||||
skipColumns int,
|
||||
delimiter string,
|
||||
comment string,
|
||||
trimSpace bool,
|
||||
dataColumns []string,
|
||||
tagColumns []string,
|
||||
nameColumn string,
|
||||
timestampColumn string,
|
||||
timestampFormat string,
|
||||
defaultTags map[string]string) (Parser, error) {
|
||||
|
||||
if header == 0 && len(dataColumns) == 0 {
|
||||
// if there is no header and no DataColumns, that's an error
|
||||
return nil, fmt.Errorf("there must be a header if `csv_data_columns` is not specified")
|
||||
}
|
||||
|
||||
if delimiter != "" {
|
||||
runeStr := []rune(delimiter)
|
||||
if len(runeStr) > 1 {
|
||||
return nil, fmt.Errorf("delimiter must be a single character, got: %s", delimiter)
|
||||
}
|
||||
delimiter = fmt.Sprintf("%v", runeStr[0])
|
||||
}
|
||||
|
||||
if comment != "" {
|
||||
runeStr := []rune(comment)
|
||||
if len(runeStr) > 1 {
|
||||
return nil, fmt.Errorf("delimiter must be a single character, got: %s", comment)
|
||||
}
|
||||
comment = fmt.Sprintf("%v", runeStr[0])
|
||||
}
|
||||
|
||||
parser := &csv.Parser{
|
||||
MetricName: metricName,
|
||||
HeaderRowCount: header,
|
||||
SkipRows: skipRows,
|
||||
SkipColumns: skipColumns,
|
||||
Delimiter: delimiter,
|
||||
Comment: comment,
|
||||
TrimSpace: trimSpace,
|
||||
ColumnNames: dataColumns,
|
||||
TagColumns: tagColumns,
|
||||
MeasurementColumn: nameColumn,
|
||||
TimestampColumn: timestampColumn,
|
||||
TimestampFormat: timestampFormat,
|
||||
DefaultTags: defaultTags,
|
||||
}
|
||||
|
||||
return parser, nil
|
||||
}
|
||||
|
||||
func newJSONParser(
|
||||
metricName string,
|
||||
tagKeys []string,
|
||||
|
||||
Reference in New Issue
Block a user