Fix syslog timestamp parsing with single digit day of month (#4334)
This commit is contained in:
parent
beaef8e3da
commit
9bd5e10133
|
@ -293,7 +293,7 @@ func (p *Parser) ParseLine(line string) (telegraf.Metric, error) {
|
||||||
timestamp = time.Unix(0, iv)
|
timestamp = time.Unix(0, iv)
|
||||||
}
|
}
|
||||||
case SYSLOG_TIMESTAMP:
|
case SYSLOG_TIMESTAMP:
|
||||||
ts, err := time.ParseInLocation("Jan 02 15:04:05", v, p.loc)
|
ts, err := time.ParseInLocation(time.Stamp, v, p.loc)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
if ts.Year() == 0 {
|
if ts.Year() == 0 {
|
||||||
ts = ts.AddDate(timestamp.Year(), 0, 0)
|
ts = ts.AddDate(timestamp.Year(), 0, 0)
|
||||||
|
|
|
@ -971,16 +971,41 @@ func TestNewlineInPatterns(t *testing.T) {
|
||||||
require.NotNil(t, m)
|
require.NotNil(t, m)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSyslogTimestampParser(t *testing.T) {
|
func TestSyslogTimestamp(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
line string
|
||||||
|
expected time.Time
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "two digit day of month",
|
||||||
|
line: "Sep 25 09:01:55 value=42",
|
||||||
|
expected: time.Date(2018, time.September, 25, 9, 1, 55, 0, time.UTC),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "one digit day of month single space",
|
||||||
|
line: "Sep 2 09:01:55 value=42",
|
||||||
|
expected: time.Date(2018, time.September, 2, 9, 1, 55, 0, time.UTC),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "one digit day of month double space",
|
||||||
|
line: "Sep 2 09:01:55 value=42",
|
||||||
|
expected: time.Date(2018, time.September, 2, 9, 1, 55, 0, time.UTC),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
p := &Parser{
|
p := &Parser{
|
||||||
Patterns: []string{`%{SYSLOGTIMESTAMP:timestamp:ts-syslog} value=%{NUMBER:value:int}`},
|
Patterns: []string{`%{SYSLOGTIMESTAMP:timestamp:ts-syslog} value=%{NUMBER:value:int}`},
|
||||||
timeFunc: func() time.Time { return time.Date(2018, time.April, 1, 0, 0, 0, 0, nil) },
|
timeFunc: func() time.Time { return time.Date(2017, time.April, 1, 0, 0, 0, 0, time.UTC) },
|
||||||
}
|
}
|
||||||
require.NoError(t, p.Compile())
|
require.NoError(t, p.Compile())
|
||||||
m, err := p.ParseLine("Sep 25 09:01:55 value=42")
|
m, err := p.ParseLine(tt.line)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.NotNil(t, m)
|
require.NotNil(t, m)
|
||||||
require.Equal(t, 2018, m.Time().Year())
|
require.Equal(t, tt.expected, m.Time())
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestReplaceTimestampComma(t *testing.T) {
|
func TestReplaceTimestampComma(t *testing.T) {
|
||||||
|
|
Loading…
Reference in New Issue