Use prefix base detection for ints in grok parser (#6434)

This commit is contained in:
Kai Groner 2019-09-23 13:26:17 -04:00 committed by Daniel Nelson
parent 24d5a93e63
commit f080b58834
2 changed files with 26 additions and 1 deletions

View File

@ -248,7 +248,7 @@ func (p *Parser) ParseLine(line string) (telegraf.Metric, error) {
case MEASUREMENT:
p.Measurement = v
case INT:
iv, err := strconv.ParseInt(v, 10, 64)
iv, err := strconv.ParseInt(v, 0, 64)
if err != nil {
log.Printf("E! Error parsing %s to int: %s", v, err)
} else {

View File

@ -649,6 +649,31 @@ func TestParseErrors_WrongTimeLayout(t *testing.T) {
testutil.MustMetric("grok", map[string]string{}, map[string]interface{}{}, time.Unix(0, 0)))
}
func TestParseInteger_Base16(t *testing.T) {
p := &Parser{
Patterns: []string{"%{TEST_LOG_C}"},
CustomPatterns: `
DURATION %{NUMBER}[nuµm]?s
BASE10OR16NUM (?:%{BASE10NUM}|%{BASE16NUM})
TEST_LOG_C %{NUMBER:myfloat} %{BASE10OR16NUM:response_code:int} %{IPORHOST:clientip} %{DURATION:rt}
`,
}
assert.NoError(t, p.Compile())
metricA, err := p.ParseLine(`1.25 0xc8 192.168.1.1 5.432µs`)
require.NotNil(t, metricA)
assert.NoError(t, err)
assert.Equal(t,
map[string]interface{}{
"clientip": "192.168.1.1",
"response_code": int64(200),
"myfloat": "1.25",
"rt": "5.432µs",
},
metricA.Fields())
assert.Equal(t, map[string]string{}, metricA.Tags())
}
func TestTsModder(t *testing.T) {
tsm := &tsModder{}