Return an error if no valid patterns. (#2753)
This commit is contained in:
parent
188703e204
commit
f7f1eaef65
|
@ -118,11 +118,18 @@ func (p *Parser) Compile() error {
|
|||
|
||||
// Give Patterns fake names so that they can be treated as named
|
||||
// "custom patterns"
|
||||
p.namedPatterns = make([]string, len(p.Patterns))
|
||||
p.namedPatterns = make([]string, 0, len(p.Patterns))
|
||||
for i, pattern := range p.Patterns {
|
||||
if pattern == "" {
|
||||
continue
|
||||
}
|
||||
name := fmt.Sprintf("GROK_INTERNAL_PATTERN_%d", i)
|
||||
p.CustomPatterns += "\n" + name + " " + pattern + "\n"
|
||||
p.namedPatterns[i] = "%{" + name + "}"
|
||||
p.namedPatterns = append(p.namedPatterns, "%{"+name+"}")
|
||||
}
|
||||
|
||||
if len(p.namedPatterns) == 0 {
|
||||
return fmt.Errorf("pattern required")
|
||||
}
|
||||
|
||||
// Combine user-supplied CustomPatterns with DEFAULT_PATTERNS and parse
|
||||
|
|
|
@ -117,16 +117,11 @@ func (l *LogParserPlugin) Start(acc telegraf.Accumulator) error {
|
|||
}
|
||||
|
||||
// compile log parser patterns:
|
||||
var haveError bool
|
||||
for _, parser := range l.parsers {
|
||||
if err := parser.Compile(); err != nil {
|
||||
acc.AddError(err)
|
||||
haveError = true
|
||||
return err
|
||||
}
|
||||
}
|
||||
if haveError {
|
||||
return nil
|
||||
}
|
||||
|
||||
l.wg.Add(1)
|
||||
go l.parser()
|
||||
|
|
|
@ -38,12 +38,8 @@ func TestGrokParseLogFilesNonExistPattern(t *testing.T) {
|
|||
}
|
||||
|
||||
acc := testutil.Accumulator{}
|
||||
logparser.Start(&acc)
|
||||
if assert.NotEmpty(t, acc.Errors) {
|
||||
assert.Error(t, acc.Errors[0])
|
||||
}
|
||||
|
||||
logparser.Stop()
|
||||
err := logparser.Start(&acc)
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
func TestGrokParseLogFiles(t *testing.T) {
|
||||
|
|
Loading…
Reference in New Issue