From 2badf976a3ea5f37d8de03b37cc1a2882f29d782 Mon Sep 17 00:00:00 2001 From: Paul Stuart Date: Thu, 21 Apr 2016 20:34:23 -0700 Subject: [PATCH] make [C|c]ompiledParse private, fix data race, update TZ to include 'GMT' --- grok.go | 16 +++++++++++----- grok_test.go | 6 ++++-- patterns/grok-patterns | 2 +- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/grok.go b/grok.go index 01a1cdb..ce3359b 100644 --- a/grok.go +++ b/grok.go @@ -192,9 +192,10 @@ func (g *Grok) Match(pattern, text string) (bool, error) { return true, nil } -// CompiledParse parses the specified text and returns a map with the results. -func (g *Grok) CompiledParse(gr *gRegexp, text string) (map[string]string, error) { +// compiledParse parses the specified text and returns a map with the results. +func (g *Grok) compiledParse(gr *gRegexp, text string) (map[string]string, error) { captures := make(map[string]string) + g.serviceMu.Lock() if match := gr.regexp.FindStringSubmatch(text); len(match) > 0 { for i, name := range gr.regexp.SubexpNames() { if name != "" { @@ -206,6 +207,7 @@ func (g *Grok) CompiledParse(gr *gRegexp, text string) (map[string]string, error } } } + g.serviceMu.Unlock() return captures, nil } @@ -217,7 +219,7 @@ func (g *Grok) Parse(pattern, text string) (map[string]string, error) { return nil, err } - return g.CompiledParse(gr, text) + return g.compiledParse(gr, text) } // ParseTyped returns a inteface{} map with typed captured fields based on provided pattern over the text @@ -284,6 +286,11 @@ func (g *Grok) buildPatterns() error { } func (g *Grok) compile(pattern string) (*gRegexp, error) { + g.serviceMu.Lock() + defer g.serviceMu.Unlock() + if g.compiledPatterns == nil { + g.compiledPatterns = map[string]*gRegexp{} + } if gr, ok := g.compiledPatterns[pattern]; ok { return gr, nil } @@ -360,14 +367,13 @@ func (g *Grok) ParseStream(reader *bufio.Reader, pattern string, process func(ma } for { line, err := reader.ReadString('\n') - fmt.Println("LINE:", line) if err == io.EOF { return nil } if err != nil { return err } - values, err := g.CompiledParse(gr, line) + values, err := g.compiledParse(gr, line) if err != nil { return err } diff --git a/grok_test.go b/grok_test.go index d527c3f..3144465 100644 --- a/grok_test.go +++ b/grok_test.go @@ -84,8 +84,10 @@ func TestAddPatternsFromPathErr(t *testing.T) { } func TestConfigPatternsDir(t *testing.T) { - g, _ := NewWithConfig(&Config{PatternsDir: []string{"./patterns"}}) - // g,_ := New() + g, err := NewWithConfig(&Config{PatternsDir: []string{"./patterns"}}) + if err != nil { + t.Error(err) + } if captures, err := g.Parse("%{SYSLOGLINE}", `Sep 12 23:19:02 docker syslog-ng[25389]: syslog-ng starting up; version='3.5.3'`); err != nil { t.Fatalf("error : %s", err.Error()) diff --git a/patterns/grok-patterns b/patterns/grok-patterns index 12e6fea..032315e 100644 --- a/patterns/grok-patterns +++ b/patterns/grok-patterns @@ -75,7 +75,7 @@ ISO8601_SECOND (?:%{SECOND}|60) TIMESTAMP_ISO8601 %{YEAR}-%{MONTHNUM}-%{MONTHDAY}[T ]%{HOUR}:?%{MINUTE}(?::?%{SECOND})?%{ISO8601_TIMEZONE}? DATE %{DATE_US}|%{DATE_EU} DATESTAMP %{DATE}[- ]%{TIME} -TZ (?:[PMCE][SD]T|UTC) +TZ (?:[PMCE][SD]T|UTC|GMT) DATESTAMP_RFC822 %{DAY} %{MONTH} %{MONTHDAY} %{YEAR} %{TIME} %{TZ} DATESTAMP_OTHER %{DAY} %{MONTH} %{MONTHDAY} %{TIME} %{TZ} %{YEAR}