incomplete changes to logparser plugin

This commit is contained in:
Max U 2018-07-03 15:45:15 -07:00
parent 8063b38b2d
commit bfc13a744b
2 changed files with 53 additions and 27 deletions

View File

@ -14,9 +14,8 @@ import (
"github.com/influxdata/telegraf" "github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/internal/globpath" "github.com/influxdata/telegraf/internal/globpath"
"github.com/influxdata/telegraf/plugins/inputs" "github.com/influxdata/telegraf/plugins/inputs"
"github.com/influxdata/telegraf/plugins/parsers"
// Parsers // Parsers
"github.com/influxdata/telegraf/plugins/inputs/logparser/grok"
) )
const ( const (
@ -45,11 +44,17 @@ type LogParserPlugin struct {
done chan struct{} done chan struct{}
wg sync.WaitGroup wg sync.WaitGroup
acc telegraf.Accumulator acc telegraf.Accumulator
parsers []LogParser parsers []parsers.Parser
sync.Mutex sync.Mutex
GrokParser *grok.Parser `toml:"grok"` GrokParser *parsers.Parser `toml:"grok"`
Patterns []string
NamedPatterns []string
CustomPatterns string
CustomPatternFiles []string
TimeZone string
} }
const sampleConfig = ` const sampleConfig = `
@ -131,16 +136,30 @@ 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 = []LogParser{} l.parsers = []parsers.Parser{}
config := &parsers.Config{
Patterns: l.Patterns,
NamedPatterns: l.NamedPatterns,
CustomPatterns: l.CustomPatterns,
CustomPatternFiles: l.CustomPatternFiles,
TimeZone: l.TimeZone,
DataFormat: "grok",
}
var err error
*l.GrokParser, err = parsers.NewParser(config)
if err != nil {
return err
}
s := reflect.ValueOf(l).Elem() s := reflect.ValueOf(l).Elem()
for i := 0; i < s.NumField(); i++ { for i := 0; i < s.NumField(); i++ {
f := s.Field(i) f := s.Field(i)
log.Printf("got field %v: %v", i, f)
if !f.CanInterface() { if !f.CanInterface() {
continue continue
} }
if lpPlugin, ok := f.Interface().(LogParser); ok { if lpPlugin, ok := f.Interface().(parsers.Parser); ok {
if reflect.ValueOf(lpPlugin).IsNil() { if reflect.ValueOf(lpPlugin).IsNil() {
continue continue
} }
@ -152,12 +171,12 @@ func (l *LogParserPlugin) Start(acc telegraf.Accumulator) error {
return fmt.Errorf("logparser input plugin: no parser defined") return fmt.Errorf("logparser input plugin: no parser defined")
} }
// compile log parser patterns: // //compile log parser patterns:
for _, parser := range l.parsers { // for _, parser := range l.parsers {
if err := parser.Compile(); err != nil { // if err := parser.Compile(); err != nil {
return err // return err
} // }
} // }
l.wg.Add(1) l.wg.Add(1)
go l.parser() go l.parser()
@ -247,8 +266,8 @@ func (l *LogParserPlugin) receiver(tailer *tail.Tail) {
} }
} }
// parser is launched as a goroutine to watch the l.lines channel. // parse is launched as a goroutine to watch the l.lines channel.
// when a line is available, parser parses it and adds the metric(s) to the // when a line is available, parse parses it and adds the metric(s) to the
// accumulator. // accumulator.
func (l *LogParserPlugin) parser() { func (l *LogParserPlugin) parser() {
defer l.wg.Done() defer l.wg.Done()

View File

@ -9,7 +9,7 @@ import (
"github.com/influxdata/telegraf/testutil" "github.com/influxdata/telegraf/testutil"
"github.com/influxdata/telegraf/plugins/inputs/logparser/grok" "github.com/influxdata/telegraf/plugins/parsers"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -26,39 +26,43 @@ func TestStartNoParsers(t *testing.T) {
func TestGrokParseLogFilesNonExistPattern(t *testing.T) { func TestGrokParseLogFilesNonExistPattern(t *testing.T) {
thisdir := getCurrentDir() thisdir := getCurrentDir()
p := &grok.Parser{ c := &parsers.Config{
Patterns: []string{"%{FOOBAR}"}, Patterns: []string{"%{FOOBAR}"},
CustomPatternFiles: []string{thisdir + "grok/testdata/test-patterns"}, 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, GrokParser: &p,
} }
acc := testutil.Accumulator{} acc := testutil.Accumulator{}
err := logparser.Start(&acc) err = logparser.Start(&acc)
assert.Error(t, err) assert.Error(t, err)
} }
func TestGrokParseLogFiles(t *testing.T) { func TestGrokParseLogFiles(t *testing.T) {
thisdir := getCurrentDir() thisdir := getCurrentDir()
p := &grok.Parser{ c := parsers.Config{
Patterns: []string{"%{TEST_LOG_A}", "%{TEST_LOG_B}"}, Patterns: []string{"%{TEST_LOG_A}", "%{TEST_LOG_B}"},
CustomPatternFiles: []string{thisdir + "grok/testdata/test-patterns"}, CustomPatternFiles: []string{thisdir + "grok/testdata/test-patterns"},
DataFormat: "grok",
} }
p, _ := 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, GrokParser: &p,
} }
acc := testutil.Accumulator{} acc := testutil.Accumulator{}
assert.NoError(t, logparser.Start(&acc)) assert.NoError(t, logparser.Start(&acc))
acc.Wait(2) //acc.Wait(2)
logparser.Stop() logparser.Stop()
@ -91,15 +95,17 @@ func TestGrokParseLogFilesAppearLater(t *testing.T) {
assert.NoError(t, err) assert.NoError(t, err)
thisdir := getCurrentDir() thisdir := getCurrentDir()
p := &grok.Parser{ c := &parsers.Config{
Patterns: []string{"%{TEST_LOG_A}", "%{TEST_LOG_B}"}, Patterns: []string{"%{TEST_LOG_A}", "%{TEST_LOG_B}"},
CustomPatternFiles: []string{thisdir + "grok/testdata/test-patterns"}, CustomPatternFiles: []string{thisdir + "grok/testdata/test-patterns"},
DataFormat: "grok",
} }
p, err := parsers.NewParser(c)
logparser := &LogParserPlugin{ logparser := &LogParserPlugin{
FromBeginning: true, FromBeginning: true,
Files: []string{emptydir + "/*.log"}, Files: []string{emptydir + "/*.log"},
GrokParser: p, GrokParser: &p,
} }
acc := testutil.Accumulator{} acc := testutil.Accumulator{}
@ -130,16 +136,17 @@ func TestGrokParseLogFilesAppearLater(t *testing.T) {
// pattern available for test_b.log // pattern available for test_b.log
func TestGrokParseLogFilesOneBad(t *testing.T) { func TestGrokParseLogFilesOneBad(t *testing.T) {
thisdir := getCurrentDir() thisdir := getCurrentDir()
p := &grok.Parser{ c := &parsers.Config{
Patterns: []string{"%{TEST_LOG_A}", "%{TEST_LOG_BAD}"}, Patterns: []string{"%{TEST_LOG_A}", "%{TEST_LOG_BAD}"},
CustomPatternFiles: []string{thisdir + "grok/testdata/test-patterns"}, CustomPatternFiles: []string{thisdir + "grok/testdata/test-patterns"},
DataFormat: "grok",
} }
assert.NoError(t, p.Compile()) p, _ := parsers.NewParser(c)
logparser := &LogParserPlugin{ logparser := &LogParserPlugin{
FromBeginning: true, FromBeginning: true,
Files: []string{thisdir + "grok/testdata/test_a.log"}, Files: []string{thisdir + "grok/testdata/test_a.log"},
GrokParser: p, GrokParser: &p,
} }
acc := testutil.Accumulator{} acc := testutil.Accumulator{}