Skip to content

Commit

Permalink
#126 print error if grok field is defined multiple times
Browse files Browse the repository at this point in the history
  • Loading branch information
fstab committed Sep 1, 2020
1 parent ed28cb8 commit 6d86abe
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
5 changes: 4 additions & 1 deletion exporter/grok.go
Expand Up @@ -66,8 +66,11 @@ func verifyFieldName(metricName string, template template.Template, regex *onigu
return fmt.Errorf("%v: field name %v is ambigous, as this field is defined in the grok pattern but is also a global field provided by grok_exporter for the %v", metricName, grokFieldName, description)
}
} else {
if !regex.HasCaptureGroup(grokFieldName) {
numGroups := regex.NumberOfCaptureGroups(grokFieldName)
if numGroups == 0 {
return fmt.Errorf("%v: grok field %v not found in match pattern", metricName, grokFieldName)
} else if numGroups > 1 {
return fmt.Errorf("%v: grok field %v found %d times in match pattern: this is ambiguous, the pattern should define each grok field exactly once", metricName, grokFieldName, numGroups)
}
}
}
Expand Down
11 changes: 9 additions & 2 deletions oniguruma/oniguruma.go
Expand Up @@ -83,8 +83,15 @@ func (regex *Regex) Free() {
}

func (regex *Regex) HasCaptureGroup(name string) bool {
_, err := regex.getCaptureGroupNums(name)
return err == nil
return regex.NumberOfCaptureGroups(name) > 0
}

func (regex *Regex) NumberOfCaptureGroups(name string) int {
groups, err := regex.getCaptureGroupNums(name)
if err != nil {
return 0
}
return len(groups)
}

func (r *Regex) getCaptureGroupNums(name string) ([]C.int, error) {
Expand Down

0 comments on commit 6d86abe

Please sign in to comment.