Fix parse of unix timestamp with more than ns precision (#5826)
This commit is contained in:
@@ -225,29 +225,25 @@ outer:
|
||||
// to the format.
|
||||
func parseTimestamp(timeFunc func() time.Time, recordFields map[string]interface{},
|
||||
timestampColumn, timestampFormat string,
|
||||
) (metricTime time.Time, err error) {
|
||||
metricTime = timeFunc()
|
||||
|
||||
) (time.Time, error) {
|
||||
if timestampColumn != "" {
|
||||
if recordFields[timestampColumn] == nil {
|
||||
err = fmt.Errorf("timestamp column: %v could not be found", timestampColumn)
|
||||
return
|
||||
return time.Time{}, fmt.Errorf("timestamp column: %v could not be found", timestampColumn)
|
||||
}
|
||||
|
||||
tStr := fmt.Sprintf("%v", recordFields[timestampColumn])
|
||||
|
||||
switch timestampFormat {
|
||||
case "":
|
||||
err = fmt.Errorf("timestamp format must be specified")
|
||||
return
|
||||
return time.Time{}, fmt.Errorf("timestamp format must be specified")
|
||||
default:
|
||||
metricTime, err = internal.ParseTimestamp(tStr, timestampFormat)
|
||||
metricTime, err := internal.ParseTimestamp(recordFields[timestampColumn], timestampFormat)
|
||||
if err != nil {
|
||||
return
|
||||
return time.Time{}, err
|
||||
}
|
||||
return metricTime, err
|
||||
}
|
||||
}
|
||||
return
|
||||
|
||||
return timeFunc(), nil
|
||||
}
|
||||
|
||||
// SetDefaultTags set the DefaultTags
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/influxdata/telegraf"
|
||||
"github.com/influxdata/telegraf/metric"
|
||||
"github.com/influxdata/telegraf/testutil"
|
||||
"github.com/stretchr/testify/require"
|
||||
@@ -322,3 +323,30 @@ func TestParseStream(t *testing.T) {
|
||||
DefaultTime(),
|
||||
), metric)
|
||||
}
|
||||
|
||||
func TestTimestampUnixFloatPrecision(t *testing.T) {
|
||||
p := Parser{
|
||||
MetricName: "csv",
|
||||
ColumnNames: []string{"time", "value"},
|
||||
TimestampColumn: "time",
|
||||
TimestampFormat: "unix",
|
||||
TimeFunc: DefaultTime,
|
||||
}
|
||||
data := `1551129661.95456123352050781250,42`
|
||||
|
||||
expected := []telegraf.Metric{
|
||||
testutil.MustMetric(
|
||||
"csv",
|
||||
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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user