Return an error if no valid patterns. (#2753)

This commit is contained in:
Daniel Nelson 2017-05-02 14:54:38 -07:00 committed by GitHub
parent 188703e204
commit f7f1eaef65
3 changed files with 12 additions and 14 deletions

View File

@ -118,11 +118,18 @@ func (p *Parser) Compile() error {
// Give Patterns fake names so that they can be treated as named // Give Patterns fake names so that they can be treated as named
// "custom patterns" // "custom patterns"
p.namedPatterns = make([]string, len(p.Patterns)) p.namedPatterns = make([]string, 0, len(p.Patterns))
for i, pattern := range p.Patterns { for i, pattern := range p.Patterns {
if pattern == "" {
continue
}
name := fmt.Sprintf("GROK_INTERNAL_PATTERN_%d", i) name := fmt.Sprintf("GROK_INTERNAL_PATTERN_%d", i)
p.CustomPatterns += "\n" + name + " " + pattern + "\n" 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 // Combine user-supplied CustomPatterns with DEFAULT_PATTERNS and parse

View File

@ -117,16 +117,11 @@ func (l *LogParserPlugin) Start(acc telegraf.Accumulator) error {
} }
// compile log parser patterns: // compile log parser patterns:
var haveError bool
for _, parser := range l.parsers { for _, parser := range l.parsers {
if err := parser.Compile(); err != nil { if err := parser.Compile(); err != nil {
acc.AddError(err) return err
haveError = true
} }
} }
if haveError {
return nil
}
l.wg.Add(1) l.wg.Add(1)
go l.parser() go l.parser()

View File

@ -38,12 +38,8 @@ func TestGrokParseLogFilesNonExistPattern(t *testing.T) {
} }
acc := testutil.Accumulator{} acc := testutil.Accumulator{}
logparser.Start(&acc) err := logparser.Start(&acc)
if assert.NotEmpty(t, acc.Errors) { assert.Error(t, err)
assert.Error(t, acc.Errors[0])
}
logparser.Stop()
} }
func TestGrokParseLogFiles(t *testing.T) { func TestGrokParseLogFiles(t *testing.T) {