Merge pull request #126 from yuiofthesun/avoid-regex-panic

Avoid regex panic
This commit is contained in:
Matthias Rampke 2018-02-23 09:31:02 +01:00 committed by GitHub
commit 16865d75b7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 3 deletions

View file

@ -106,9 +106,17 @@ func (m *metricMapper) initFromYAMLString(fileContents string) error {
// can use to match metrics later on.
metricRe := strings.Replace(currentMapping.Match, ".", "\\.", -1)
metricRe = strings.Replace(metricRe, "*", "([^.]*)", -1)
currentMapping.regex = regexp.MustCompile("^" + metricRe + "$")
if regex, err := regexp.Compile("^" + metricRe + "$"); err != nil {
return fmt.Errorf("invalid match %s. cannot compile regex in mapping: %v", currentMapping.Match, err)
} else {
currentMapping.regex = regex
}
} else {
currentMapping.regex = regexp.MustCompile(currentMapping.Match)
if regex, err := regexp.Compile(currentMapping.Match); err != nil {
return fmt.Errorf("invalid regex %s in mapping: %v", currentMapping.Match, err)
} else {
currentMapping.regex = regex
}
}
if currentMapping.TimerType == "" {

View file

@ -310,7 +310,7 @@ mappings:
{
config: `---
mappings:
- match: "*\.foo"
- match: "*\\.foo"
match_type: regex
name: "foo"
labels: {}