Exclude csv_timestamp_column and csv_measurement_column from fields (#7572)

This commit is contained in:
Harshit Bansal 2020-05-27 02:46:48 +05:30 committed by GitHub
parent 2561aac6ef
commit 092059c066
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 69 additions and 8 deletions

View File

@ -212,7 +212,6 @@ cpu,42
},
map[string]interface{}{
"time_idle": 42,
"measurement": "cpu",
},
time.Unix(0, 0)),
testutil.MustMetric("cpu",
@ -221,7 +220,6 @@ cpu,42
},
map[string]interface{}{
"time_idle": 42,
"measurement": "cpu",
},
time.Unix(0, 0)),
}

View File

@ -56,11 +56,13 @@ values.
## will be added as fields.
csv_tag_columns = []
## The column to extract the name of the metric from
## The column to extract the name of the metric from. Will not be
## included as field in metric.
csv_measurement_column = ""
## The column to extract time information for the metric
## `csv_timestamp_format` must be specified if this is used
## `csv_timestamp_format` must be specified if this is used.
## Will not be included as field in metric.
csv_timestamp_column = ""
## The format of time data extracted from `csv_timestamp_column`

View File

@ -216,6 +216,10 @@ outer:
return nil, err
}
// Exclude `TimestampColumn` and `MeasurementColumn`
delete(recordFields, p.TimestampColumn)
delete(recordFields, p.MeasurementColumn)
m, err := metric.New(measurementName, tags, recordFields, metricTime)
if err != nil {
return nil, err

View File

@ -281,10 +281,10 @@ hello,80,test_name2`
expectedFields := map[string]interface{}{
"line2": int64(80),
"line3": "test_name2",
}
metrics, err := p.Parse([]byte(testCSV))
require.NoError(t, err)
require.Equal(t, "test_name2", metrics[0].Name())
require.Equal(t, expectedFields, metrics[0].Fields())
}
@ -364,7 +364,64 @@ func TestTimestampUnixFloatPrecision(t *testing.T) {
map[string]string{},
map[string]interface{}{
"value": 42,
"time": 1551129661.954561233,
},
time.Unix(1551129661, 954561233),
),
}
metrics, err := p.Parse([]byte(data))
require.NoError(t, err)
testutil.RequireMetricsEqual(t, expected, metrics)
}
func TestSkipMeasurementColumn(t *testing.T) {
p := Parser{
MetricName: "csv",
HeaderRowCount: 1,
TimestampColumn: "timestamp",
TimestampFormat: "unix",
TimeFunc: DefaultTime,
TrimSpace: true,
}
data := `id,value,timestamp
1,5,1551129661.954561233`
expected := []telegraf.Metric{
testutil.MustMetric(
"csv",
map[string]string{},
map[string]interface{}{
"id": 1,
"value": 5,
},
time.Unix(1551129661, 954561233),
),
}
metrics, err := p.Parse([]byte(data))
require.NoError(t, err)
testutil.RequireMetricsEqual(t, expected, metrics)
}
func TestSkipTimestampColumn(t *testing.T) {
p := Parser{
MetricName: "csv",
HeaderRowCount: 1,
TimestampColumn: "timestamp",
TimestampFormat: "unix",
TimeFunc: DefaultTime,
TrimSpace: true,
}
data := `id,value,timestamp
1,5,1551129661.954561233`
expected := []telegraf.Metric{
testutil.MustMetric(
"csv",
map[string]string{},
map[string]interface{}{
"id": 1,
"value": 5,
},
time.Unix(1551129661, 954561233),
),