2013-02-07 02:49:04 -08:00
|
|
|
// Copyright 2013 Prometheus Team
|
|
|
|
// 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"
|
2013-02-13 08:41:35 -08:00
|
|
|
"github.com/prometheus/prometheus/utility"
|
2013-01-07 14:24:26 -08:00
|
|
|
)
|
|
|
|
|
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 {
|
|
|
|
return nil, fmt.Errorf("Recording rule expression %v does not evaluate to vector type", expr)
|
2013-01-07 14:24:26 -08:00
|
|
|
}
|
2013-04-22 15:26:59 -07:00
|
|
|
return NewRecordingRule(name, labels, expr.(ast.VectorNode), permanent), nil
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
return nil, fmt.Errorf("Alert rule expression %v does not evaluate to vector type", expr)
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func NewFunctionCall(name string, args []ast.Node) (ast.Node, error) {
|
|
|
|
function, err := ast.GetFunction(name)
|
|
|
|
if err != nil {
|
2013-02-13 08:41:35 -08:00
|
|
|
return nil, fmt.Errorf("Unknown function \"%v\"", 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
|
|
|
|
}
|
|
|
|
|
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 {
|
2013-02-13 08:41:35 -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{
|
2013-05-08 07:35:16 -07: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 {
|
2013-02-13 08:41:35 -08:00
|
|
|
return nil, fmt.Errorf("Unknown aggregation type '%v'", 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
|
|
|
}
|
|
|
|
|
|
|
|
func NewArithExpr(opTypeStr string, lhs ast.Node, rhs ast.Node) (ast.Node, error) {
|
|
|
|
var opTypes = map[string]ast.BinOpType{
|
|
|
|
"+": ast.ADD,
|
|
|
|
"-": ast.SUB,
|
|
|
|
"*": ast.MUL,
|
|
|
|
"/": ast.DIV,
|
|
|
|
"%": ast.MOD,
|
|
|
|
">": ast.GT,
|
|
|
|
"<": ast.LT,
|
|
|
|
"==": ast.EQ,
|
|
|
|
"!=": ast.NE,
|
|
|
|
">=": ast.GE,
|
|
|
|
"<=": ast.LE,
|
|
|
|
"AND": ast.AND,
|
|
|
|
"OR": ast.OR,
|
|
|
|
}
|
|
|
|
opType, ok := opTypes[opTypeStr]
|
|
|
|
if !ok {
|
2013-02-13 08:41:35 -08:00
|
|
|
return nil, fmt.Errorf("Invalid binary operator \"%v\"", opTypeStr)
|
2013-01-07 14:24:26 -08:00
|
|
|
}
|
|
|
|
expr, err := ast.NewArithExpr(opType, lhs, rhs)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2014-02-22 13:29:32 -08:00
|
|
|
func NewMatrixSelector(vector ast.Node, intervalStr string) (ast.MatrixNode, error) {
|
2013-01-07 14:24:26 -08:00
|
|
|
switch vector.(type) {
|
2014-02-22 13:29:32 -08:00
|
|
|
case *ast.VectorSelector:
|
2013-01-07 14:24:26 -08:00
|
|
|
{
|
|
|
|
break
|
|
|
|
}
|
|
|
|
default:
|
2014-02-22 13:29:32 -08:00
|
|
|
return nil, fmt.Errorf("Intervals are currently only supported for vector selectors.")
|
2013-01-07 14:24:26 -08:00
|
|
|
}
|
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
|
|
|
|
}
|
2014-02-22 13:29:32 -08:00
|
|
|
vectorSelector := vector.(*ast.VectorSelector)
|
|
|
|
return ast.NewMatrixSelector(vectorSelector, interval), nil
|
2013-01-07 14:24:26 -08:00
|
|
|
}
|
2013-06-13 07:10:05 -07:00
|
|
|
|
|
|
|
func ConsoleLinkForExpression(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
|
|
|
}
|