Add support for decimal timestamps to ts-epoch modifier (#3358)
This commit is contained in:
@@ -253,12 +253,30 @@ func (p *Parser) ParseLine(line string) (telegraf.Metric, error) {
|
||||
case STRING:
|
||||
fields[k] = strings.Trim(v, `"`)
|
||||
case EPOCH:
|
||||
iv, err := strconv.ParseInt(v, 10, 64)
|
||||
if err != nil {
|
||||
log.Printf("E! Error parsing %s to int: %s", v, err)
|
||||
} else {
|
||||
timestamp = time.Unix(iv, 0)
|
||||
parts := strings.SplitN(v, ".", 2)
|
||||
if len(parts) == 0 {
|
||||
log.Printf("E! Error parsing %s to timestamp: %s", v, err)
|
||||
break
|
||||
}
|
||||
|
||||
sec, err := strconv.ParseInt(parts[0], 10, 64)
|
||||
if err != nil {
|
||||
log.Printf("E! Error parsing %s to timestamp: %s", v, err)
|
||||
break
|
||||
}
|
||||
ts := time.Unix(sec, 0)
|
||||
|
||||
if len(parts) == 2 {
|
||||
padded := fmt.Sprintf("%-9s", parts[1])
|
||||
nsString := strings.Replace(padded[:9], " ", "0", -1)
|
||||
nanosec, err := strconv.ParseInt(nsString, 10, 64)
|
||||
if err != nil {
|
||||
log.Printf("E! Error parsing %s to timestamp: %s", v, err)
|
||||
break
|
||||
}
|
||||
ts = ts.Add(time.Duration(nanosec) * time.Nanosecond)
|
||||
}
|
||||
timestamp = ts
|
||||
case EPOCH_NANO:
|
||||
iv, err := strconv.ParseInt(v, 10, 64)
|
||||
if err != nil {
|
||||
|
||||
@@ -385,6 +385,77 @@ func TestParseEpoch(t *testing.T) {
|
||||
assert.Equal(t, time.Unix(1466004605, 0), metricA.Time())
|
||||
}
|
||||
|
||||
func TestParseEpochDecimal(t *testing.T) {
|
||||
var tests = []struct {
|
||||
name string
|
||||
line string
|
||||
noMatch bool
|
||||
err error
|
||||
tags map[string]string
|
||||
fields map[string]interface{}
|
||||
time time.Time
|
||||
}{
|
||||
{
|
||||
name: "ns precision",
|
||||
line: "1466004605.359052000 value=42",
|
||||
tags: map[string]string{},
|
||||
fields: map[string]interface{}{
|
||||
"value": int64(42),
|
||||
},
|
||||
time: time.Unix(0, 1466004605359052000),
|
||||
},
|
||||
{
|
||||
name: "ms precision",
|
||||
line: "1466004605.359 value=42",
|
||||
tags: map[string]string{},
|
||||
fields: map[string]interface{}{
|
||||
"value": int64(42),
|
||||
},
|
||||
time: time.Unix(0, 1466004605359000000),
|
||||
},
|
||||
{
|
||||
name: "second precision",
|
||||
line: "1466004605 value=42",
|
||||
tags: map[string]string{},
|
||||
fields: map[string]interface{}{
|
||||
"value": int64(42),
|
||||
},
|
||||
time: time.Unix(0, 1466004605000000000),
|
||||
},
|
||||
{
|
||||
name: "sub ns precision",
|
||||
line: "1466004605.123456789123 value=42",
|
||||
tags: map[string]string{},
|
||||
fields: map[string]interface{}{
|
||||
"value": int64(42),
|
||||
},
|
||||
time: time.Unix(0, 1466004605123456789),
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
parser := &Parser{
|
||||
Patterns: []string{"%{NUMBER:ts:ts-epoch} value=%{NUMBER:value:int}"},
|
||||
}
|
||||
assert.NoError(t, parser.Compile())
|
||||
m, err := parser.ParseLine(tt.line)
|
||||
|
||||
if tt.noMatch {
|
||||
require.Nil(t, m)
|
||||
require.Nil(t, err)
|
||||
return
|
||||
}
|
||||
|
||||
require.Equal(t, tt.err, err)
|
||||
|
||||
require.NotNil(t, m)
|
||||
require.Equal(t, tt.tags, m.Tags())
|
||||
require.Equal(t, tt.fields, m.Fields())
|
||||
require.Equal(t, tt.time, m.Time())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseEpochErrors(t *testing.T) {
|
||||
p := &Parser{
|
||||
Patterns: []string{"%{MYAPP}"},
|
||||
|
||||
Reference in New Issue
Block a user