Fix for divide by zero error by defaulting to current year if no year is given in timestamp

This commit is contained in:
Ayrdrie Palmer 2018-06-21 22:15:33 -06:00
parent 1bd41ef3ce
commit 09f884b4f0
2 changed files with 19 additions and 1 deletions

View File

@ -337,6 +337,9 @@ func (p *Parser) ParseLine(line string) (telegraf.Metric, error) {
default:
ts, err := time.ParseInLocation(t, v, p.loc)
if err == nil {
if ts.Year() == 0 {
ts = ts.AddDate(timestamp.Year(), 0, 0)
}
timestamp = ts
} else {
log.Printf("E! Error parsing %s to time layout [%s]: %s", v, t, err)
@ -466,7 +469,6 @@ func (t *tsModder) tsMod(ts time.Time) time.Time {
t.rollover = 0
return ts
}
if ts.Equal(t.last) {
t.dupe = ts
}

View File

@ -982,3 +982,19 @@ func TestSyslogTimestampParser(t *testing.T) {
require.NotNil(t, m)
require.Equal(t, 2018, m.Time().Year())
}
func TestFailDivideByZeroError(t *testing.T) {
p := &Parser{
Patterns: []string{`%{APPLE_SYSLOG_TIME_SHORT:timestamp:ts-"Jan 2 15:04:05"} %{HOSTNAME} %{APP_NAME:app_name}\[%{NUMBER:pid:int}\]%{GREEDYDATA:message}`},
CustomPatterns: `
APPLE_SYSLOG_TIME_SHORT %{MONTH} +%{MONTHDAY} %{TIME}
APP_NAME [a-zA-Z0-9\.]+
`,
}
require.NoError(t, p.Compile())
m, err := p.ParseLine("Nov 6 13:57:03 rsavage iTunes[6504]: info> Scale factor of main display = 2.0")
m, err = p.ParseLine("Nov 6 13:57:03 rsavage iTunes[6504]: objc[6504]: Class AMSupportURLConnectionDelegate is implemented in both /System/Library/PrivateFrameworks/EmbeddedOSInstall.framework/Versions/A/EmbeddedOSInstall (0x7fffa594e748) and /System/Library/PrivateFrameworks/MobileDevice.framework/MobileDevice (0x107e67008). One of the two will be used. Which one is undefined.")
require.NoError(t, err)
require.NotNil(t, m)
require.Equal(t, 2018, m.Time().Year())
}