mirror of
https://github.com/prometheus/statsd_exporter.git
synced 2024-11-14 11:21:17 +00:00
Avoid panic from compiling user supplied regex
If an invalid regex is supplied mustCompile would panic during configuration reload. Return an error instead that can be handled.
This commit is contained in:
parent
d1f7ca4239
commit
7e86bcaa92
1 changed files with 10 additions and 2 deletions
12
mapper.go
12
mapper.go
|
@ -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 == "" {
|
||||
|
|
Loading…
Reference in a new issue