Skip to content

Commit

Permalink
make [C|c]ompiledParse private, fix data race, update TZ to include '…
Browse files Browse the repository at this point in the history
…GMT'
  • Loading branch information
paulstuart committed Apr 22, 2016
1 parent 0ecf191 commit 2badf97
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
16 changes: 11 additions & 5 deletions grok.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "" {
Expand All @@ -206,6 +207,7 @@ func (g *Grok) CompiledParse(gr *gRegexp, text string) (map[string]string, error
}
}
}
g.serviceMu.Unlock()

return captures, nil
}
Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
6 changes: 4 additions & 2 deletions grok_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion patterns/grok-patterns
Original file line number Diff line number Diff line change
Expand Up @@ -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}

Expand Down

0 comments on commit 2badf97

Please sign in to comment.