2020-06-01 12:46:37 +02:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2020-09-11 21:14:30 +02:00
|
|
|
"crypto/md5"
|
2020-06-01 12:46:37 +02:00
|
|
|
"fmt"
|
2020-06-15 21:15:47 +02:00
|
|
|
"hash/fnv"
|
2020-06-01 12:46:37 +02:00
|
|
|
"io/ioutil"
|
2021-12-02 13:45:08 +01:00
|
|
|
"net/url"
|
2020-06-01 12:46:37 +02:00
|
|
|
"path/filepath"
|
2020-06-15 21:15:47 +02:00
|
|
|
"sort"
|
2020-06-01 12:46:37 +02:00
|
|
|
"strings"
|
|
|
|
|
2021-12-02 13:45:08 +01:00
|
|
|
"gopkg.in/yaml.v2"
|
2021-02-01 14:02:44 +01:00
|
|
|
|
2021-12-02 13:45:08 +01:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmalert/datasource"
|
2020-06-01 12:46:37 +02:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmalert/notifier"
|
2020-10-20 09:15:21 +02:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmalert/utils"
|
2020-08-13 15:43:55 +02:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/envtemplate"
|
2020-09-03 10:04:42 +02:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
|
2022-02-11 15:17:00 +01:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promutils"
|
2020-06-01 12:46:37 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Group contains list of Rules grouped into
|
|
|
|
// entity with one name and evaluation interval
|
|
|
|
type Group struct {
|
2021-02-01 14:02:44 +01:00
|
|
|
Type datasource.Type `yaml:"type,omitempty"`
|
2020-06-09 14:21:20 +02:00
|
|
|
File string
|
2022-04-16 13:25:54 +02:00
|
|
|
Name string `yaml:"name"`
|
|
|
|
Interval *promutils.Duration `yaml:"interval,omitempty"`
|
2022-06-09 08:21:30 +02:00
|
|
|
Limit int `yaml:"limit,omitempty"`
|
2022-04-16 13:25:54 +02:00
|
|
|
Rules []Rule `yaml:"rules"`
|
|
|
|
Concurrency int `yaml:"concurrency"`
|
2021-08-31 13:52:34 +02:00
|
|
|
// Labels is a set of label value pairs, that will be added to every rule.
|
|
|
|
// It has priority over the external labels.
|
|
|
|
Labels map[string]string `yaml:"labels"`
|
2020-09-11 21:14:30 +02:00
|
|
|
// Checksum stores the hash of yaml definition for this group.
|
|
|
|
// May be used to detect any changes like rules re-ordering etc.
|
|
|
|
Checksum string
|
2021-12-02 13:45:08 +01:00
|
|
|
// Optional HTTP URL parameters added to each rule request
|
|
|
|
Params url.Values `yaml:"params"`
|
2022-07-21 15:59:55 +02:00
|
|
|
// Headers contains optional HTTP headers added to each rule request
|
|
|
|
Headers []datasource.Header `yaml:"headers,omitempty"`
|
2020-06-01 12:46:37 +02:00
|
|
|
|
|
|
|
// Catches all undefined fields and must be empty after parsing.
|
|
|
|
XXX map[string]interface{} `yaml:",inline"`
|
|
|
|
}
|
|
|
|
|
2020-09-11 21:14:30 +02:00
|
|
|
// UnmarshalYAML implements the yaml.Unmarshaler interface.
|
|
|
|
func (g *Group) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
|
|
|
type group Group
|
|
|
|
if err := unmarshal((*group)(g)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
b, err := yaml.Marshal(g)
|
|
|
|
if err != nil {
|
2020-09-23 21:46:24 +02:00
|
|
|
return fmt.Errorf("failed to marshal group configuration for checksum: %w", err)
|
2020-09-11 21:14:30 +02:00
|
|
|
}
|
2021-02-01 14:02:44 +01:00
|
|
|
// change default value to prometheus datasource.
|
|
|
|
if g.Type.Get() == "" {
|
|
|
|
g.Type.Set(datasource.NewPrometheusType())
|
|
|
|
}
|
|
|
|
|
2020-09-11 21:14:30 +02:00
|
|
|
h := md5.New()
|
|
|
|
h.Write(b)
|
|
|
|
g.Checksum = fmt.Sprintf("%x", h.Sum(nil))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-01 12:46:37 +02:00
|
|
|
// Validate check for internal Group or Rule configuration errors
|
2020-06-06 22:27:09 +02:00
|
|
|
func (g *Group) Validate(validateAnnotations, validateExpressions bool) error {
|
2020-06-01 12:46:37 +02:00
|
|
|
if g.Name == "" {
|
|
|
|
return fmt.Errorf("group name must be set")
|
|
|
|
}
|
2021-02-01 14:02:44 +01:00
|
|
|
|
2020-06-15 21:15:47 +02:00
|
|
|
uniqueRules := map[uint64]struct{}{}
|
2020-06-01 12:46:37 +02:00
|
|
|
for _, r := range g.Rules {
|
|
|
|
ruleName := r.Record
|
|
|
|
if r.Alert != "" {
|
|
|
|
ruleName = r.Alert
|
|
|
|
}
|
2020-06-15 21:15:47 +02:00
|
|
|
if _, ok := uniqueRules[r.ID]; ok {
|
|
|
|
return fmt.Errorf("rule %q duplicate", ruleName)
|
2020-06-01 12:46:37 +02:00
|
|
|
}
|
2020-06-15 21:15:47 +02:00
|
|
|
uniqueRules[r.ID] = struct{}{}
|
2020-06-01 12:46:37 +02:00
|
|
|
if err := r.Validate(); err != nil {
|
2020-06-30 21:58:18 +02:00
|
|
|
return fmt.Errorf("invalid rule %q.%q: %w", g.Name, ruleName, err)
|
2020-06-01 12:46:37 +02:00
|
|
|
}
|
2020-06-06 22:27:09 +02:00
|
|
|
if validateExpressions {
|
2021-02-01 14:02:44 +01:00
|
|
|
// its needed only for tests.
|
|
|
|
// because correct types must be inherited after unmarshalling.
|
|
|
|
exprValidator := g.Type.ValidateExpr
|
|
|
|
if err := exprValidator(r.Expr); err != nil {
|
2020-06-30 21:58:18 +02:00
|
|
|
return fmt.Errorf("invalid expression for rule %q.%q: %w", g.Name, ruleName, err)
|
2020-06-06 22:27:09 +02:00
|
|
|
}
|
2020-06-01 12:46:37 +02:00
|
|
|
}
|
2020-06-06 22:27:09 +02:00
|
|
|
if validateAnnotations {
|
|
|
|
if err := notifier.ValidateTemplates(r.Annotations); err != nil {
|
2020-06-30 21:58:18 +02:00
|
|
|
return fmt.Errorf("invalid annotations for rule %q.%q: %w", g.Name, ruleName, err)
|
2020-06-06 22:27:09 +02:00
|
|
|
}
|
|
|
|
if err := notifier.ValidateTemplates(r.Labels); err != nil {
|
2020-06-30 21:58:18 +02:00
|
|
|
return fmt.Errorf("invalid labels for rule %q.%q: %w", g.Name, ruleName, err)
|
2020-06-06 22:27:09 +02:00
|
|
|
}
|
2020-06-01 12:46:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return checkOverflow(g.XXX, fmt.Sprintf("group %q", g.Name))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rule describes entity that represent either
|
|
|
|
// recording rule or alerting rule.
|
|
|
|
type Rule struct {
|
2020-06-15 21:15:47 +02:00
|
|
|
ID uint64
|
2022-04-16 13:25:54 +02:00
|
|
|
Record string `yaml:"record,omitempty"`
|
|
|
|
Alert string `yaml:"alert,omitempty"`
|
|
|
|
Expr string `yaml:"expr"`
|
|
|
|
For *promutils.Duration `yaml:"for,omitempty"`
|
|
|
|
Labels map[string]string `yaml:"labels,omitempty"`
|
|
|
|
Annotations map[string]string `yaml:"annotations,omitempty"`
|
2020-06-15 21:15:47 +02:00
|
|
|
|
|
|
|
// Catches all undefined fields and must be empty after parsing.
|
|
|
|
XXX map[string]interface{} `yaml:",inline"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalYAML implements the yaml.Unmarshaler interface.
|
|
|
|
func (r *Rule) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
|
|
|
type rule Rule
|
|
|
|
if err := unmarshal((*rule)(r)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
r.ID = HashRule(*r)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-28 13:20:31 +02:00
|
|
|
// Name returns Rule name according to its type
|
|
|
|
func (r *Rule) Name() string {
|
|
|
|
if r.Record != "" {
|
|
|
|
return r.Record
|
|
|
|
}
|
|
|
|
return r.Alert
|
|
|
|
}
|
|
|
|
|
2020-06-15 21:15:47 +02:00
|
|
|
// HashRule hashes significant Rule fields into
|
2020-09-11 21:14:30 +02:00
|
|
|
// unique hash that supposed to define Rule uniqueness
|
2020-06-15 21:15:47 +02:00
|
|
|
func HashRule(r Rule) uint64 {
|
|
|
|
h := fnv.New64a()
|
|
|
|
h.Write([]byte(r.Expr))
|
|
|
|
if r.Record != "" {
|
|
|
|
h.Write([]byte("recording"))
|
|
|
|
h.Write([]byte(r.Record))
|
|
|
|
} else {
|
|
|
|
h.Write([]byte("alerting"))
|
|
|
|
h.Write([]byte(r.Alert))
|
|
|
|
}
|
2020-09-11 21:14:30 +02:00
|
|
|
kv := sortMap(r.Labels)
|
2020-06-15 21:15:47 +02:00
|
|
|
for _, i := range kv {
|
|
|
|
h.Write([]byte(i.key))
|
|
|
|
h.Write([]byte(i.value))
|
|
|
|
h.Write([]byte("\xff"))
|
|
|
|
}
|
|
|
|
return h.Sum64()
|
2020-06-01 12:46:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validate check for Rule configuration errors
|
|
|
|
func (r *Rule) Validate() error {
|
|
|
|
if (r.Record == "" && r.Alert == "") || (r.Record != "" && r.Alert != "") {
|
|
|
|
return fmt.Errorf("either `record` or `alert` must be set")
|
|
|
|
}
|
|
|
|
if r.Expr == "" {
|
|
|
|
return fmt.Errorf("expression can't be empty")
|
|
|
|
}
|
2020-06-15 21:15:47 +02:00
|
|
|
return checkOverflow(r.XXX, "rule")
|
2020-06-01 12:46:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parse parses rule configs from given file patterns
|
2020-06-06 22:27:09 +02:00
|
|
|
func Parse(pathPatterns []string, validateAnnotations, validateExpressions bool) ([]Group, error) {
|
2020-06-01 12:46:37 +02:00
|
|
|
var fp []string
|
|
|
|
for _, pattern := range pathPatterns {
|
|
|
|
matches, err := filepath.Glob(pattern)
|
|
|
|
if err != nil {
|
2020-06-30 21:58:18 +02:00
|
|
|
return nil, fmt.Errorf("error reading file pattern %s: %w", pattern, err)
|
2020-06-01 12:46:37 +02:00
|
|
|
}
|
|
|
|
fp = append(fp, matches...)
|
|
|
|
}
|
2020-10-20 09:15:21 +02:00
|
|
|
errGroup := new(utils.ErrGroup)
|
2020-06-01 12:46:37 +02:00
|
|
|
var groups []Group
|
|
|
|
for _, file := range fp {
|
|
|
|
uniqueGroups := map[string]struct{}{}
|
|
|
|
gr, err := parseFile(file)
|
|
|
|
if err != nil {
|
2020-10-20 09:15:21 +02:00
|
|
|
errGroup.Add(fmt.Errorf("failed to parse file %q: %w", file, err))
|
|
|
|
continue
|
2020-06-01 12:46:37 +02:00
|
|
|
}
|
|
|
|
for _, g := range gr {
|
2020-06-06 22:27:09 +02:00
|
|
|
if err := g.Validate(validateAnnotations, validateExpressions); err != nil {
|
2020-10-20 09:15:21 +02:00
|
|
|
errGroup.Add(fmt.Errorf("invalid group %q in file %q: %w", g.Name, file, err))
|
|
|
|
continue
|
2020-06-01 12:46:37 +02:00
|
|
|
}
|
|
|
|
if _, ok := uniqueGroups[g.Name]; ok {
|
2020-10-20 09:15:21 +02:00
|
|
|
errGroup.Add(fmt.Errorf("group name %q duplicate in file %q", g.Name, file))
|
|
|
|
continue
|
2020-06-01 12:46:37 +02:00
|
|
|
}
|
|
|
|
uniqueGroups[g.Name] = struct{}{}
|
|
|
|
g.File = file
|
|
|
|
groups = append(groups, g)
|
|
|
|
}
|
|
|
|
}
|
2020-10-20 09:15:21 +02:00
|
|
|
if err := errGroup.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-06-01 12:46:37 +02:00
|
|
|
if len(groups) < 1 {
|
2020-09-03 10:04:42 +02:00
|
|
|
logger.Warnf("no groups found in %s", strings.Join(pathPatterns, ";"))
|
2020-06-01 12:46:37 +02:00
|
|
|
}
|
|
|
|
return groups, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseFile(path string) ([]Group, error) {
|
|
|
|
data, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error reading alert rule file: %w", err)
|
|
|
|
}
|
2020-08-13 15:43:55 +02:00
|
|
|
data = envtemplate.Replace(data)
|
2020-06-01 12:46:37 +02:00
|
|
|
g := struct {
|
|
|
|
Groups []Group `yaml:"groups"`
|
|
|
|
// Catches all undefined fields and must be empty after parsing.
|
|
|
|
XXX map[string]interface{} `yaml:",inline"`
|
|
|
|
}{}
|
|
|
|
err = yaml.Unmarshal(data, &g)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return g.Groups, checkOverflow(g.XXX, "config")
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkOverflow(m map[string]interface{}, ctx string) error {
|
|
|
|
if len(m) > 0 {
|
|
|
|
var keys []string
|
|
|
|
for k := range m {
|
|
|
|
keys = append(keys, k)
|
|
|
|
}
|
|
|
|
return fmt.Errorf("unknown fields in %s: %s", ctx, strings.Join(keys, ", "))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2020-09-11 21:14:30 +02:00
|
|
|
|
|
|
|
type item struct {
|
|
|
|
key, value string
|
|
|
|
}
|
|
|
|
|
|
|
|
func sortMap(m map[string]string) []item {
|
|
|
|
var kv []item
|
|
|
|
for k, v := range m {
|
|
|
|
kv = append(kv, item{key: k, value: v})
|
|
|
|
}
|
|
|
|
sort.Slice(kv, func(i, j int) bool {
|
|
|
|
return kv[i].key < kv[j].key
|
|
|
|
})
|
|
|
|
return kv
|
|
|
|
}
|