logparser is linked to grok parser

This commit is contained in:
Max U 2018-07-06 11:22:14 -07:00
parent 67db14332f
commit 8a9da28734
2 changed files with 30 additions and 14 deletions

View File

@ -3,7 +3,9 @@
package logparser
import (
"fmt"
"log"
"reflect"
"strings"
"sync"
@ -43,7 +45,7 @@ type LogParserPlugin struct {
done chan struct{}
wg sync.WaitGroup
acc telegraf.Accumulator
parsers []parsers.Parser
parsers []LogParser
sync.Mutex
@ -135,7 +137,7 @@ func (l *LogParserPlugin) Start(acc telegraf.Accumulator) error {
l.tailers = make(map[string]*tail.Tail)
// Looks for fields which implement LogParser interface
l.parsers = []parsers.Parser{}
l.parsers = []LogParser{}
config := &parsers.Config{
Patterns: l.Patterns,
NamedPatterns: l.NamedPatterns,
@ -144,12 +146,33 @@ func (l *LogParserPlugin) Start(acc telegraf.Accumulator) error {
TimeZone: l.TimeZone,
DataFormat: "grok",
}
var err error
l.GrokParser, err = parsers.NewParser(config)
if err != nil {
return err
}
s := reflect.ValueOf(l).Elem()
for i := 0; i < s.NumField(); i++ {
f := s.Field(i)
if !f.CanInterface() {
continue
}
if lpPlugin, ok := f.Interface().(LogParser); ok {
if reflect.ValueOf(lpPlugin).IsNil() {
continue
}
l.parsers = append(l.parsers, lpPlugin)
}
}
if len(l.parsers) == 0 {
return fmt.Errorf("logparser input plugin: no parser defined")
}
l.wg.Add(1)
go l.parser()

View File

@ -10,8 +10,6 @@ import (
"github.com/influxdata/telegraf/testutil"
"github.com/influxdata/telegraf/plugins/parsers"
"github.com/stretchr/testify/assert"
)
@ -27,21 +25,16 @@ func TestStartNoParsers(t *testing.T) {
func TestGrokParseLogFilesNonExistPattern(t *testing.T) {
thisdir := getCurrentDir()
c := &parsers.Config{
Patterns: []string{"%{FOOBAR}"},
CustomPatternFiles: []string{thisdir + "grok/testdata/test-patterns"},
DataFormat: "grok",
}
p, err := parsers.NewParser(c)
logparser := &LogParserPlugin{
FromBeginning: true,
Files: []string{thisdir + "grok/testdata/*.log"},
GrokParser: p,
FromBeginning: true,
Files: []string{thisdir + "grok/testdata/*.log"},
Patterns: []string{"%{FOOBAR}"},
CustomPatternFiles: []string{thisdir + "grok/testdata/test-patterns"},
}
acc := testutil.Accumulator{}
err = logparser.Start(&acc)
err := logparser.Start(&acc)
assert.Error(t, err)
}