Skip to content

Commit

Permalink
Use prefix base detection for ints in grok parser (#6434)
Browse files Browse the repository at this point in the history
  • Loading branch information
groner authored and danielnelson committed Sep 23, 2019
1 parent 24d5a93 commit f080b58
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion plugins/parsers/grok/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
25 changes: 25 additions & 0 deletions plugins/parsers/grok/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}

Expand Down

0 comments on commit f080b58

Please sign in to comment.