lib/promrelabel: fix parsing regex: true in relabeling rules

This commit is contained in:
Aliaksandr Valialkin 2021-09-21 23:00:15 +03:00
parent 9884a55f3c
commit 207c5760ce
No known key found for this signature in database
GPG Key ID: A72BEC6CD3D0DED1
3 changed files with 47 additions and 11 deletions

View File

@ -7,6 +7,7 @@ sort: 15
## tip
* BUGFIX: vmselect: fix accessing [Graphite APIs](https://docs.victoriametrics.com/#graphite-api-usage). The access has been broken in v1.66.0, because `/graphite/*` path prefix accidentally clashed with `/graph*` path prefix used for VictoriaMetrics UI (aka `vmui`).
* BUGFIX: fix parsing `regex: <bool_or_number>` in relabeling rules (for example, `regex: true` or `regex: 123`). The bug has been introduced in v1.66.0.
## [v1.66.0](https://github.com/VictoriaMetrics/VictoriaMetrics/releases/tag/v1.66.0)

View File

@ -4,6 +4,7 @@ import (
"fmt"
"io/ioutil"
"regexp"
"strconv"
"strings"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/envtemplate"
@ -45,29 +46,51 @@ func (mlr *MultiLineRegex) UnmarshalYAML(f func(interface{}) error) error {
if err := f(&v); err != nil {
return err
}
var a []string
s, err := stringValue(v)
if err != nil {
return err
}
mlr.s = s
return nil
}
func stringValue(v interface{}) (string, error) {
if v == nil {
return "null", nil
}
switch x := v.(type) {
case string:
a = []string{x}
case []interface{}:
a = make([]string, len(x))
a := make([]string, len(x))
for i, xx := range x {
s, ok := xx.(string)
if !ok {
return fmt.Errorf("`regex` must contain array of strings; got %T", xx)
s, err := stringValue(xx)
if err != nil {
return "", err
}
a[i] = s
}
default:
return fmt.Errorf("unexpected type for `regex`: %T; want string or []string", v)
return strings.Join(a, "|"), nil
case string:
return x, nil
case float64:
return strconv.FormatFloat(x, 'f', -1, 64), nil
case int:
return strconv.Itoa(x), nil
case bool:
if x {
return "true", nil
}
return "false", nil
default:
return "", fmt.Errorf("unexpected type for `regex`: %T; want string or []string", v)
}
mlr.s = strings.Join(a, "|")
return nil
}
// MarshalYAML marshals mlr to YAML.
func (mlr *MultiLineRegex) MarshalYAML() (interface{}, error) {
a := strings.Split(mlr.s, "|")
if len(a) == 1 {
return a[0], nil
}
return a, nil
}

View File

@ -26,13 +26,25 @@ func TestRelabelConfigMarshalUnmarshal(t *testing.T) {
f(`
- action: keep
regex: foobar
`, "- regex:\n - foobar\n action: keep\n")
`, "- regex: foobar\n action: keep\n")
f(`
- regex:
- 'fo.+'
- '.*ba[r-z]a'
`, "- regex:\n - fo.+\n - .*ba[r-z]a\n")
f(`- regex: foo|bar`, "- regex:\n - foo\n - bar\n")
f(`- regex: True`, `- regex: "true"`+"\n")
f(`- regex: true`, `- regex: "true"`+"\n")
f(`- regex: 123`, `- regex: "123"`+"\n")
f(`- regex: 1.23`, `- regex: "1.23"`+"\n")
f(`- regex: [null]`, `- regex: "null"`+"\n")
f(`
- regex:
- -1.23
- False
- null
- nan
`, "- regex:\n - \"-1.23\"\n - \"false\"\n - \"null\"\n - nan\n")
}
func TestLoadRelabelConfigsSuccess(t *testing.T) {