2015-01-21 11:07:45 -08:00
|
|
|
// Copyright 2013 The Prometheus Authors
|
2013-02-07 02:49:04 -08:00
|
|
|
// 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.
|
|
|
|
|
2013-01-07 14:24:26 -08:00
|
|
|
package rules
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2013-08-20 06:42:06 -07:00
|
|
|
"net/url"
|
|
|
|
"strings"
|
2013-06-13 07:10:05 -07:00
|
|
|
|
2013-06-25 05:02:27 -07:00
|
|
|
clientmodel "github.com/prometheus/client_golang/model"
|
|
|
|
|
2013-01-27 09:49:45 -08:00
|
|
|
"github.com/prometheus/prometheus/rules/ast"
|
2014-03-28 03:58:47 -07:00
|
|
|
"github.com/prometheus/prometheus/storage/metric"
|
2013-02-13 08:41:35 -08:00
|
|
|
"github.com/prometheus/prometheus/utility"
|
2013-01-07 14:24:26 -08:00
|
|
|
)
|
|
|
|
|
2014-12-10 07:16:49 -08:00
|
|
|
// CreateRecordingRule is a convenience function to create a recording rule.
|
2013-06-25 05:02:27 -07:00
|
|
|
func CreateRecordingRule(name string, labels clientmodel.LabelSet, expr ast.Node, permanent bool) (*RecordingRule, error) {
|
2013-04-22 15:26:59 -07:00
|
|
|
if _, ok := expr.(ast.VectorNode); !ok {
|
2014-12-10 07:16:49 -08:00
|
|
|
return nil, fmt.Errorf("recording rule expression %v does not evaluate to vector type", expr)
|
2013-01-07 14:24:26 -08:00
|
|
|
}
|
2014-12-10 07:16:49 -08:00
|
|
|
return &RecordingRule{
|
|
|
|
name: name,
|
|
|
|
labels: labels,
|
|
|
|
vector: expr.(ast.VectorNode),
|
|
|
|
permanent: permanent,
|
|
|
|
}, nil
|
2013-04-22 15:26:59 -07:00
|
|
|
}
|
|
|
|
|
2014-12-10 07:16:49 -08:00
|
|
|
// CreateAlertingRule is a convenience function to create a new alerting rule.
|
2013-07-30 08:18:07 -07:00
|
|
|
func CreateAlertingRule(name string, expr ast.Node, holdDurationStr string, labels clientmodel.LabelSet, summary string, description string) (*AlertingRule, error) {
|
2013-04-22 15:26:59 -07:00
|
|
|
if _, ok := expr.(ast.VectorNode); !ok {
|
2014-12-10 07:16:49 -08:00
|
|
|
return nil, fmt.Errorf("alert rule expression %v does not evaluate to vector type", expr)
|
2013-04-22 15:26:59 -07:00
|
|
|
}
|
|
|
|
holdDuration, err := utility.StringToDuration(holdDurationStr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2013-07-30 08:18:07 -07:00
|
|
|
return NewAlertingRule(name, expr.(ast.VectorNode), holdDuration, labels, summary, description), nil
|
2013-01-07 14:24:26 -08:00
|
|
|
}
|
|
|
|
|
2015-03-03 03:16:46 -08:00
|
|
|
// NewScalarLiteral returns a ScalarLiteral with the given value. If sign is "-"
|
|
|
|
// the value is negated.
|
|
|
|
func NewScalarLiteral(value clientmodel.SampleValue, sign string) *ast.ScalarLiteral {
|
|
|
|
if sign == "-" {
|
|
|
|
value = -value
|
|
|
|
}
|
|
|
|
return ast.NewScalarLiteral(value)
|
|
|
|
}
|
|
|
|
|
2014-12-10 07:16:49 -08:00
|
|
|
// NewFunctionCall is a convenience function to create a new AST function-call node.
|
2013-01-07 14:24:26 -08:00
|
|
|
func NewFunctionCall(name string, args []ast.Node) (ast.Node, error) {
|
|
|
|
function, err := ast.GetFunction(name)
|
|
|
|
if err != nil {
|
2014-12-10 07:16:49 -08:00
|
|
|
return nil, fmt.Errorf("unknown function %q", name)
|
2013-01-07 14:24:26 -08:00
|
|
|
}
|
|
|
|
functionCall, err := ast.NewFunctionCall(function, args)
|
|
|
|
if err != nil {
|
2013-02-13 08:41:35 -08:00
|
|
|
return nil, fmt.Errorf(err.Error())
|
2013-01-07 14:24:26 -08:00
|
|
|
}
|
|
|
|
return functionCall, nil
|
|
|
|
}
|
|
|
|
|
2014-12-10 07:16:49 -08:00
|
|
|
// NewVectorAggregation is a convenience function to create a new AST vector aggregation.
|
2013-12-13 10:20:42 -08:00
|
|
|
func NewVectorAggregation(aggrTypeStr string, vector ast.Node, groupBy clientmodel.LabelNames, keepExtraLabels bool) (*ast.VectorAggregation, error) {
|
2013-04-22 15:26:59 -07:00
|
|
|
if _, ok := vector.(ast.VectorNode); !ok {
|
2014-12-10 07:16:49 -08:00
|
|
|
return nil, fmt.Errorf("operand of %v aggregation must be of vector type", aggrTypeStr)
|
2013-01-07 14:24:26 -08:00
|
|
|
}
|
|
|
|
var aggrTypes = map[string]ast.AggrType{
|
2014-12-24 16:28:35 -08:00
|
|
|
"SUM": ast.Sum,
|
|
|
|
"MAX": ast.Max,
|
|
|
|
"MIN": ast.Min,
|
|
|
|
"AVG": ast.Avg,
|
|
|
|
"COUNT": ast.Count,
|
2013-01-07 14:24:26 -08:00
|
|
|
}
|
|
|
|
aggrType, ok := aggrTypes[aggrTypeStr]
|
|
|
|
if !ok {
|
2014-12-10 07:16:49 -08:00
|
|
|
return nil, fmt.Errorf("unknown aggregation type %q", aggrTypeStr)
|
2013-01-07 14:24:26 -08:00
|
|
|
}
|
2013-12-13 10:20:42 -08:00
|
|
|
return ast.NewVectorAggregation(aggrType, vector.(ast.VectorNode), groupBy, keepExtraLabels), nil
|
2013-01-07 14:24:26 -08:00
|
|
|
}
|
|
|
|
|
2015-03-03 01:58:28 -08:00
|
|
|
// vectorMatching combines data used to match samples between vectors.
|
|
|
|
type vectorMatching struct {
|
|
|
|
matchCardinality ast.VectorMatchCardinality
|
|
|
|
matchOn clientmodel.LabelNames
|
|
|
|
includeLabels clientmodel.LabelNames
|
|
|
|
}
|
|
|
|
|
|
|
|
// newVectorMatching is a convenience function to create a new vectorMatching.
|
|
|
|
func newVectorMatching(card string, matchOn, include clientmodel.LabelNames) (*vectorMatching, error) {
|
|
|
|
var matchCardinalities = map[string]ast.VectorMatchCardinality{
|
|
|
|
"": ast.MatchOneToOne,
|
|
|
|
"GROUP_LEFT": ast.MatchManyToOne,
|
|
|
|
"GROUP_RIGHT": ast.MatchOneToMany,
|
|
|
|
}
|
|
|
|
matchCard, ok := matchCardinalities[card]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("invalid vector match cardinality %q", card)
|
|
|
|
}
|
|
|
|
if matchCard != ast.MatchOneToOne && len(include) == 0 {
|
|
|
|
return nil, fmt.Errorf("grouped vector matching must provide labels")
|
|
|
|
}
|
|
|
|
// There must be no overlap between both labelname lists.
|
|
|
|
for _, matchLabel := range matchOn {
|
|
|
|
for _, incLabel := range include {
|
|
|
|
if matchLabel == incLabel {
|
|
|
|
return nil, fmt.Errorf("use of label %s in ON and %s clauses not allowed", incLabel, card)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &vectorMatching{matchCard, matchOn, include}, nil
|
|
|
|
}
|
|
|
|
|
2014-12-10 07:16:49 -08:00
|
|
|
// NewArithExpr is a convenience function to create a new AST arithmetic expression.
|
2015-03-03 01:58:28 -08:00
|
|
|
func NewArithExpr(opTypeStr string, lhs ast.Node, rhs ast.Node, vecMatching *vectorMatching) (ast.Node, error) {
|
2013-01-07 14:24:26 -08:00
|
|
|
var opTypes = map[string]ast.BinOpType{
|
2014-12-24 16:28:35 -08:00
|
|
|
"+": ast.Add,
|
|
|
|
"-": ast.Sub,
|
|
|
|
"*": ast.Mul,
|
|
|
|
"/": ast.Div,
|
|
|
|
"%": ast.Mod,
|
2013-01-07 14:24:26 -08:00
|
|
|
">": ast.GT,
|
|
|
|
"<": ast.LT,
|
|
|
|
"==": ast.EQ,
|
|
|
|
"!=": ast.NE,
|
|
|
|
">=": ast.GE,
|
|
|
|
"<=": ast.LE,
|
2014-12-24 16:28:35 -08:00
|
|
|
"AND": ast.And,
|
|
|
|
"OR": ast.Or,
|
2013-01-07 14:24:26 -08:00
|
|
|
}
|
|
|
|
opType, ok := opTypes[opTypeStr]
|
|
|
|
if !ok {
|
2014-12-10 07:16:49 -08:00
|
|
|
return nil, fmt.Errorf("invalid binary operator %q", opTypeStr)
|
2013-01-07 14:24:26 -08:00
|
|
|
}
|
2015-03-03 01:58:28 -08:00
|
|
|
var vm vectorMatching
|
|
|
|
if vecMatching != nil {
|
|
|
|
vm = *vecMatching
|
|
|
|
// And/or always do many-to-many matching.
|
|
|
|
if opType == ast.And || opType == ast.Or {
|
|
|
|
vm.matchCardinality = ast.MatchManyToMany
|
|
|
|
}
|
|
|
|
}
|
|
|
|
expr, err := ast.NewArithExpr(opType, lhs, rhs, vm.matchCardinality, vm.matchOn, vm.includeLabels)
|
2013-01-07 14:24:26 -08:00
|
|
|
if err != nil {
|
2013-02-13 08:41:35 -08:00
|
|
|
return nil, fmt.Errorf(err.Error())
|
2013-01-07 14:24:26 -08:00
|
|
|
}
|
|
|
|
return expr, nil
|
|
|
|
}
|
|
|
|
|
2015-02-17 17:30:41 -08:00
|
|
|
// NewVectorSelector is a convenience function to create a new AST vector selector.
|
|
|
|
func NewVectorSelector(m metric.LabelMatchers, offsetStr string) (ast.VectorNode, error) {
|
|
|
|
offset, err := utility.StringToDuration(offsetStr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2013-01-07 14:24:26 -08:00
|
|
|
}
|
2015-02-17 17:30:41 -08:00
|
|
|
return ast.NewVectorSelector(m, offset), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewMatrixSelector is a convenience function to create a new AST matrix selector.
|
|
|
|
func NewMatrixSelector(vector ast.Node, intervalStr string, offsetStr string) (ast.MatrixNode, error) {
|
2013-02-13 08:41:35 -08:00
|
|
|
interval, err := utility.StringToDuration(intervalStr)
|
2013-01-07 14:24:26 -08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-02-17 17:30:41 -08:00
|
|
|
offset, err := utility.StringToDuration(offsetStr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
vectorSelector, ok := vector.(*ast.VectorSelector)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("intervals are currently only supported for vector selectors")
|
|
|
|
}
|
|
|
|
return ast.NewMatrixSelector(vectorSelector, interval, offset), nil
|
2013-01-07 14:24:26 -08:00
|
|
|
}
|
2013-06-13 07:10:05 -07:00
|
|
|
|
2014-03-28 03:58:47 -07:00
|
|
|
func newLabelMatcher(matchTypeStr string, name clientmodel.LabelName, value clientmodel.LabelValue) (*metric.LabelMatcher, error) {
|
|
|
|
matchTypes := map[string]metric.MatchType{
|
|
|
|
"=": metric.Equal,
|
|
|
|
"!=": metric.NotEqual,
|
|
|
|
"=~": metric.RegexMatch,
|
|
|
|
"!~": metric.RegexNoMatch,
|
|
|
|
}
|
|
|
|
matchType, ok := matchTypes[matchTypeStr]
|
|
|
|
if !ok {
|
2014-12-10 07:16:49 -08:00
|
|
|
return nil, fmt.Errorf("invalid label matching operator %q", matchTypeStr)
|
2014-03-28 03:58:47 -07:00
|
|
|
}
|
|
|
|
return metric.NewLabelMatcher(matchType, name, value)
|
|
|
|
}
|
|
|
|
|
2014-12-10 07:16:49 -08:00
|
|
|
// TableLinkForExpression creates an escaped relative link to the table view of
|
|
|
|
// the provided expression.
|
2014-07-25 05:23:47 -07:00
|
|
|
func TableLinkForExpression(expr string) string {
|
2013-08-20 06:42:06 -07:00
|
|
|
// url.QueryEscape percent-escapes everything except spaces, for which it
|
|
|
|
// uses "+". However, in the non-query part of a URI, only percent-escaped
|
|
|
|
// spaces are legal, so we need to manually replace "+" with "%20" after
|
|
|
|
// query-escaping the string.
|
|
|
|
//
|
|
|
|
// See also:
|
|
|
|
// http://stackoverflow.com/questions/1634271/url-encoding-the-space-character-or-20.
|
|
|
|
urlData := url.QueryEscape(fmt.Sprintf(`[{"expr":%q,"tab":1}]`, expr))
|
|
|
|
return fmt.Sprintf("/graph#%s", strings.Replace(urlData, "+", "%20", -1))
|
2013-06-13 07:10:05 -07:00
|
|
|
}
|
2014-07-25 05:23:47 -07:00
|
|
|
|
2014-12-10 07:16:49 -08:00
|
|
|
// GraphLinkForExpression creates an escaped relative link to the graph view of
|
|
|
|
// the provided expression.
|
2014-07-25 05:23:47 -07:00
|
|
|
func GraphLinkForExpression(expr string) string {
|
2014-12-26 08:40:06 -08:00
|
|
|
urlData := url.QueryEscape(fmt.Sprintf(`[{"expr":%q,"tab":0}]`, expr))
|
2014-07-25 05:23:47 -07:00
|
|
|
return fmt.Sprintf("/graph#%s", strings.Replace(urlData, "+", "%20", -1))
|
|
|
|
}
|