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 package logparser
import ( import (
"fmt"
"log" "log"
"reflect"
"strings" "strings"
"sync" "sync"
@ -43,7 +45,7 @@ type LogParserPlugin struct {
done chan struct{} done chan struct{}
wg sync.WaitGroup wg sync.WaitGroup
acc telegraf.Accumulator acc telegraf.Accumulator
parsers []parsers.Parser parsers []LogParser
sync.Mutex sync.Mutex
@ -135,7 +137,7 @@ func (l *LogParserPlugin) Start(acc telegraf.Accumulator) error {
l.tailers = make(map[string]*tail.Tail) l.tailers = make(map[string]*tail.Tail)
// Looks for fields which implement LogParser interface // Looks for fields which implement LogParser interface
l.parsers = []parsers.Parser{} l.parsers = []LogParser{}
config := &parsers.Config{ config := &parsers.Config{
Patterns: l.Patterns, Patterns: l.Patterns,
NamedPatterns: l.NamedPatterns, NamedPatterns: l.NamedPatterns,
@ -144,12 +146,33 @@ func (l *LogParserPlugin) Start(acc telegraf.Accumulator) error {
TimeZone: l.TimeZone, TimeZone: l.TimeZone,
DataFormat: "grok", DataFormat: "grok",
} }
var err error var err error
l.GrokParser, err = parsers.NewParser(config) l.GrokParser, err = parsers.NewParser(config)
if err != nil { if err != nil {
return err 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) l.wg.Add(1)
go l.parser() go l.parser()

View File

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