2017-06-14 01:04:13 -07:00
|
|
|
// Copyright 2017 The Prometheus Authors
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2017-06-07 07:58:15 -07:00
|
|
|
package rulefmt
|
|
|
|
|
|
|
|
import (
|
2018-09-13 05:55:58 -07:00
|
|
|
"context"
|
2017-06-07 07:58:15 -07:00
|
|
|
"io/ioutil"
|
2019-04-15 09:52:58 -07:00
|
|
|
"strings"
|
2018-09-13 05:55:58 -07:00
|
|
|
"time"
|
2017-06-07 07:58:15 -07:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2017-06-14 02:37:54 -07:00
|
|
|
"github.com/prometheus/common/model"
|
2019-03-25 16:01:12 -07:00
|
|
|
yaml "gopkg.in/yaml.v2"
|
|
|
|
|
2018-09-13 05:55:58 -07:00
|
|
|
"github.com/prometheus/prometheus/pkg/timestamp"
|
2017-06-07 07:58:15 -07:00
|
|
|
"github.com/prometheus/prometheus/promql"
|
2018-09-13 05:55:58 -07:00
|
|
|
"github.com/prometheus/prometheus/template"
|
2017-06-07 07:58:15 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// Error represents semantical errors on parsing rule groups.
|
|
|
|
type Error struct {
|
2017-12-06 07:39:06 -08:00
|
|
|
Group string
|
|
|
|
Rule int
|
|
|
|
RuleName string
|
|
|
|
Err error
|
2017-06-07 07:58:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (err *Error) Error() string {
|
2017-12-06 07:39:06 -08:00
|
|
|
return errors.Wrapf(err.Err, "group %q, rule %d, %q", err.Group, err.Rule, err.RuleName).Error()
|
2017-06-07 07:58:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// RuleGroups is a set of rule groups that are typically exposed in a file.
|
|
|
|
type RuleGroups struct {
|
2017-06-19 04:08:46 -07:00
|
|
|
Groups []RuleGroup `yaml:"groups"`
|
2017-06-07 07:58:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validate validates all rules in the rule groups.
|
|
|
|
func (g *RuleGroups) Validate() (errs []error) {
|
2017-06-13 23:49:21 -07:00
|
|
|
set := map[string]struct{}{}
|
|
|
|
|
2017-06-07 07:58:15 -07:00
|
|
|
for _, g := range g.Groups {
|
2017-06-14 00:51:32 -07:00
|
|
|
if g.Name == "" {
|
|
|
|
errs = append(errs, errors.Errorf("Groupname should not be empty"))
|
|
|
|
}
|
|
|
|
|
2017-06-13 23:49:21 -07:00
|
|
|
if _, ok := set[g.Name]; ok {
|
|
|
|
errs = append(
|
|
|
|
errs,
|
|
|
|
errors.Errorf("groupname: \"%s\" is repeated in the same file", g.Name),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
set[g.Name] = struct{}{}
|
|
|
|
|
2017-06-07 07:58:15 -07:00
|
|
|
for i, r := range g.Rules {
|
|
|
|
for _, err := range r.Validate() {
|
2017-12-06 07:39:06 -08:00
|
|
|
var ruleName string
|
|
|
|
if r.Alert != "" {
|
|
|
|
ruleName = r.Alert
|
|
|
|
} else {
|
|
|
|
ruleName = r.Record
|
|
|
|
}
|
2017-06-07 07:58:15 -07:00
|
|
|
errs = append(errs, &Error{
|
2017-12-06 07:39:06 -08:00
|
|
|
Group: g.Name,
|
|
|
|
Rule: i,
|
|
|
|
RuleName: ruleName,
|
|
|
|
Err: err,
|
2017-06-07 07:58:15 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-06-16 04:14:33 -07:00
|
|
|
|
2017-06-07 07:58:15 -07:00
|
|
|
return errs
|
|
|
|
}
|
|
|
|
|
|
|
|
// RuleGroup is a list of sequentially evaluated recording and alerting rules.
|
|
|
|
type RuleGroup struct {
|
2017-06-15 22:16:21 -07:00
|
|
|
Name string `yaml:"name"`
|
|
|
|
Interval model.Duration `yaml:"interval,omitempty"`
|
|
|
|
Rules []Rule `yaml:"rules"`
|
2017-06-07 07:58:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Rule describes an alerting or recording rule.
|
|
|
|
type Rule struct {
|
2017-06-15 22:16:21 -07:00
|
|
|
Record string `yaml:"record,omitempty"`
|
|
|
|
Alert string `yaml:"alert,omitempty"`
|
|
|
|
Expr string `yaml:"expr"`
|
|
|
|
For model.Duration `yaml:"for,omitempty"`
|
|
|
|
Labels map[string]string `yaml:"labels,omitempty"`
|
|
|
|
Annotations map[string]string `yaml:"annotations,omitempty"`
|
2017-06-07 07:58:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validate the rule and return a list of encountered errors.
|
|
|
|
func (r *Rule) Validate() (errs []error) {
|
|
|
|
if r.Record != "" && r.Alert != "" {
|
|
|
|
errs = append(errs, errors.Errorf("only one of 'record' and 'alert' must be set"))
|
|
|
|
}
|
|
|
|
if r.Record == "" && r.Alert == "" {
|
|
|
|
errs = append(errs, errors.Errorf("one of 'record' or 'alert' must be set"))
|
|
|
|
}
|
2017-06-14 00:51:32 -07:00
|
|
|
|
2017-06-07 07:58:15 -07:00
|
|
|
if r.Expr == "" {
|
|
|
|
errs = append(errs, errors.Errorf("field 'expr' must be set in rule"))
|
|
|
|
} else if _, err := promql.ParseExpr(r.Expr); err != nil {
|
|
|
|
errs = append(errs, errors.Errorf("could not parse expression: %s", err))
|
|
|
|
}
|
|
|
|
if r.Record != "" {
|
|
|
|
if len(r.Annotations) > 0 {
|
|
|
|
errs = append(errs, errors.Errorf("invalid field 'annotations' in recording rule"))
|
|
|
|
}
|
2017-06-15 22:16:21 -07:00
|
|
|
if r.For != 0 {
|
2017-06-07 07:58:15 -07:00
|
|
|
errs = append(errs, errors.Errorf("invalid field 'for' in recording rule"))
|
|
|
|
}
|
2017-10-17 02:22:59 -07:00
|
|
|
if !model.IsValidMetricName(model.LabelValue(r.Record)) {
|
|
|
|
errs = append(errs, errors.Errorf("invalid recording rule name: %s", r.Record))
|
|
|
|
}
|
2017-06-07 07:58:15 -07:00
|
|
|
}
|
2017-06-14 02:37:54 -07:00
|
|
|
|
|
|
|
for k, v := range r.Labels {
|
|
|
|
if !model.LabelName(k).IsValid() {
|
|
|
|
errs = append(errs, errors.Errorf("invalid label name: %s", k))
|
|
|
|
}
|
|
|
|
|
|
|
|
if !model.LabelValue(v).IsValid() {
|
|
|
|
errs = append(errs, errors.Errorf("invalid label value: %s", v))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for k := range r.Annotations {
|
|
|
|
if !model.LabelName(k).IsValid() {
|
|
|
|
errs = append(errs, errors.Errorf("invalid annotation name: %s", k))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-13 05:55:58 -07:00
|
|
|
errs = append(errs, testTemplateParsing(r)...)
|
|
|
|
return errs
|
|
|
|
}
|
|
|
|
|
|
|
|
// testTemplateParsing checks if the templates used in labels and annotations
|
|
|
|
// of the alerting rules are parsed correctly.
|
|
|
|
func testTemplateParsing(rl *Rule) (errs []error) {
|
|
|
|
if rl.Alert == "" {
|
|
|
|
// Not an alerting rule.
|
|
|
|
return errs
|
|
|
|
}
|
|
|
|
|
|
|
|
// Trying to parse templates.
|
2019-04-15 09:52:58 -07:00
|
|
|
tmplData := template.AlertTemplateData(map[string]string{}, map[string]string{}, 0)
|
|
|
|
defs := []string{
|
|
|
|
"{{$labels := .Labels}}",
|
|
|
|
"{{$externalLabels := .ExternalLabels}}",
|
|
|
|
"{{$value := .Value}}",
|
|
|
|
}
|
2018-09-13 05:55:58 -07:00
|
|
|
parseTest := func(text string) error {
|
|
|
|
tmpl := template.NewTemplateExpander(
|
|
|
|
context.TODO(),
|
2019-04-15 09:52:58 -07:00
|
|
|
strings.Join(append(defs, text), ""),
|
2018-09-13 05:55:58 -07:00
|
|
|
"__alert_"+rl.Alert,
|
|
|
|
tmplData,
|
|
|
|
model.Time(timestamp.FromTime(time.Now())),
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
)
|
|
|
|
return tmpl.ParseTest()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parsing Labels.
|
|
|
|
for _, val := range rl.Labels {
|
|
|
|
err := parseTest(val)
|
|
|
|
if err != nil {
|
2019-03-25 16:01:12 -07:00
|
|
|
errs = append(errs, errors.Errorf("msg=%s", err.Error()))
|
2018-09-13 05:55:58 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parsing Annotations.
|
|
|
|
for _, val := range rl.Annotations {
|
|
|
|
err := parseTest(val)
|
|
|
|
if err != nil {
|
2019-03-25 16:01:12 -07:00
|
|
|
errs = append(errs, errors.Errorf("msg=%s", err.Error()))
|
2018-09-13 05:55:58 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-07 07:58:15 -07:00
|
|
|
return errs
|
|
|
|
}
|
|
|
|
|
2018-01-22 02:43:52 -08:00
|
|
|
// Parse parses and validates a set of rules.
|
|
|
|
func Parse(content []byte) (*RuleGroups, []error) {
|
|
|
|
var groups RuleGroups
|
2018-04-04 01:07:39 -07:00
|
|
|
if err := yaml.UnmarshalStrict(content, &groups); err != nil {
|
2018-01-22 02:43:52 -08:00
|
|
|
return nil, []error{err}
|
|
|
|
}
|
|
|
|
return &groups, groups.Validate()
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseFile reads and parses rules from a file.
|
2017-06-07 07:58:15 -07:00
|
|
|
func ParseFile(file string) (*RuleGroups, []error) {
|
|
|
|
b, err := ioutil.ReadFile(file)
|
|
|
|
if err != nil {
|
|
|
|
return nil, []error{err}
|
|
|
|
}
|
2018-01-22 02:43:52 -08:00
|
|
|
return Parse(b)
|
2017-06-07 07:58:15 -07:00
|
|
|
}
|