2013-02-07 10:49:04 +00: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 22:24:26 +00:00
|
|
|
%{
|
|
|
|
package rules
|
|
|
|
|
2013-07-11 16:38:44 +00:00
|
|
|
import (
|
|
|
|
clientmodel "github.com/prometheus/client_golang/model"
|
2013-06-25 12:02:27 +00:00
|
|
|
|
2013-07-11 16:38:44 +00:00
|
|
|
"github.com/prometheus/prometheus/rules/ast"
|
2014-03-28 10:58:47 +00:00
|
|
|
"github.com/prometheus/prometheus/storage/metric"
|
2013-06-25 12:02:27 +00:00
|
|
|
)
|
2013-01-07 22:24:26 +00:00
|
|
|
%}
|
|
|
|
|
|
|
|
%union {
|
2013-06-25 12:02:27 +00:00
|
|
|
num clientmodel.SampleValue
|
2013-01-07 22:24:26 +00:00
|
|
|
str string
|
|
|
|
ruleNode ast.Node
|
|
|
|
ruleNodeSlice []ast.Node
|
|
|
|
boolean bool
|
2013-06-25 12:02:27 +00:00
|
|
|
labelNameSlice clientmodel.LabelNames
|
|
|
|
labelSet clientmodel.LabelSet
|
2014-03-28 10:58:47 +00:00
|
|
|
labelMatcher *metric.LabelMatcher
|
|
|
|
labelMatchers metric.LabelMatchers
|
2013-01-07 22:24:26 +00:00
|
|
|
}
|
|
|
|
|
2013-01-11 00:17:37 +00:00
|
|
|
/* We simulate multiple start symbols for closely-related grammars via dummy tokens. See
|
|
|
|
http://www.gnu.org/software/bison/manual/html_node/Multiple-start_002dsymbols.html
|
|
|
|
Reason: we want to be able to parse lists of named rules as well as single expressions.
|
|
|
|
*/
|
|
|
|
%token START_RULES START_EXPRESSION
|
|
|
|
|
2014-03-21 12:11:01 +00:00
|
|
|
%token <str> IDENTIFIER STRING DURATION METRICNAME
|
2013-01-07 22:24:26 +00:00
|
|
|
%token <num> NUMBER
|
2013-12-13 18:20:42 +00:00
|
|
|
%token PERMANENT GROUP_OP KEEPING_EXTRA
|
2013-01-07 22:24:26 +00:00
|
|
|
%token <str> AGGR_OP CMP_OP ADDITIVE_OP MULT_OP
|
2013-07-30 15:18:07 +00:00
|
|
|
%token ALERT IF FOR WITH SUMMARY DESCRIPTION
|
2013-01-07 22:24:26 +00:00
|
|
|
|
|
|
|
%type <ruleNodeSlice> func_arg_list
|
|
|
|
%type <labelNameSlice> label_list grouping_opts
|
|
|
|
%type <labelSet> label_assign label_assign_list rule_labels
|
2014-03-28 10:58:47 +00:00
|
|
|
%type <labelMatcher> label_match
|
|
|
|
%type <labelMatchers> label_match_list label_matches
|
2013-01-07 22:24:26 +00:00
|
|
|
%type <ruleNode> rule_expr func_arg
|
2013-12-13 18:20:42 +00:00
|
|
|
%type <boolean> qualifier extra_labels_opts
|
2014-03-28 10:58:47 +00:00
|
|
|
%type <str> for_duration metric_name label_match_type
|
2013-01-07 22:24:26 +00:00
|
|
|
|
|
|
|
%right '='
|
|
|
|
%left CMP_OP
|
|
|
|
%left ADDITIVE_OP
|
|
|
|
%left MULT_OP
|
2013-01-11 00:17:37 +00:00
|
|
|
%start start
|
2013-01-07 22:24:26 +00:00
|
|
|
|
|
|
|
%%
|
2013-01-11 00:17:37 +00:00
|
|
|
start : START_RULES rules_stat_list
|
|
|
|
| START_EXPRESSION saved_rule_expr
|
|
|
|
;
|
|
|
|
|
2013-01-07 22:24:26 +00:00
|
|
|
rules_stat_list : /* empty */
|
|
|
|
| rules_stat_list rules_stat
|
|
|
|
;
|
|
|
|
|
2013-01-11 00:17:37 +00:00
|
|
|
saved_rule_expr : rule_expr
|
|
|
|
{ yylex.(*RulesLexer).parsedExpr = $1 }
|
|
|
|
;
|
|
|
|
|
2014-03-21 12:11:01 +00:00
|
|
|
|
|
|
|
rules_stat : qualifier metric_name rule_labels '=' rule_expr
|
2013-01-07 22:24:26 +00:00
|
|
|
{
|
2013-04-22 22:26:59 +00:00
|
|
|
rule, err := CreateRecordingRule($2, $3, $5, $1)
|
2013-01-07 22:24:26 +00:00
|
|
|
if err != nil { yylex.Error(err.Error()); return 1 }
|
2013-01-11 00:17:37 +00:00
|
|
|
yylex.(*RulesLexer).parsedRules = append(yylex.(*RulesLexer).parsedRules, rule)
|
2013-01-07 22:24:26 +00:00
|
|
|
}
|
2013-07-30 15:18:07 +00:00
|
|
|
| ALERT IDENTIFIER IF rule_expr for_duration WITH rule_labels SUMMARY STRING DESCRIPTION STRING
|
2013-04-22 22:26:59 +00:00
|
|
|
{
|
2013-07-30 15:18:07 +00:00
|
|
|
rule, err := CreateAlertingRule($2, $4, $5, $7, $9, $11)
|
2013-04-22 22:26:59 +00:00
|
|
|
if err != nil { yylex.Error(err.Error()); return 1 }
|
|
|
|
yylex.(*RulesLexer).parsedRules = append(yylex.(*RulesLexer).parsedRules, rule)
|
|
|
|
}
|
|
|
|
;
|
|
|
|
|
|
|
|
for_duration : /* empty */
|
|
|
|
{ $$ = "0s" }
|
|
|
|
| FOR DURATION
|
|
|
|
{ $$ = $2 }
|
2013-01-07 22:24:26 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
qualifier : /* empty */
|
|
|
|
{ $$ = false }
|
|
|
|
| PERMANENT
|
|
|
|
{ $$ = true }
|
|
|
|
;
|
|
|
|
|
2014-03-21 12:11:01 +00:00
|
|
|
metric_name : METRICNAME
|
|
|
|
{ $$ = $1 }
|
|
|
|
| IDENTIFIER
|
|
|
|
{ $$ = $1 }
|
|
|
|
;
|
|
|
|
|
2013-01-07 22:24:26 +00:00
|
|
|
rule_labels : /* empty */
|
2013-06-25 12:02:27 +00:00
|
|
|
{ $$ = clientmodel.LabelSet{} }
|
2013-01-07 22:24:26 +00:00
|
|
|
| '{' label_assign_list '}'
|
|
|
|
{ $$ = $2 }
|
|
|
|
| '{' '}'
|
2013-06-25 12:02:27 +00:00
|
|
|
{ $$ = clientmodel.LabelSet{} }
|
2013-01-07 22:24:26 +00:00
|
|
|
|
|
|
|
label_assign_list : label_assign
|
|
|
|
{ $$ = $1 }
|
|
|
|
| label_assign_list ',' label_assign
|
|
|
|
{ for k, v := range $3 { $$[k] = v } }
|
|
|
|
;
|
|
|
|
|
|
|
|
label_assign : IDENTIFIER '=' STRING
|
2013-06-25 12:02:27 +00:00
|
|
|
{ $$ = clientmodel.LabelSet{ clientmodel.LabelName($1): clientmodel.LabelValue($3) } }
|
2013-01-07 22:24:26 +00:00
|
|
|
;
|
|
|
|
|
2014-03-28 10:58:47 +00:00
|
|
|
label_matches : /* empty */
|
|
|
|
{ $$ = metric.LabelMatchers{} }
|
|
|
|
| '{' '}'
|
|
|
|
{ $$ = metric.LabelMatchers{} }
|
|
|
|
| '{' label_match_list '}'
|
|
|
|
{ $$ = $2 }
|
|
|
|
;
|
|
|
|
|
|
|
|
label_match_list : label_match
|
|
|
|
{ $$ = metric.LabelMatchers{$1} }
|
|
|
|
| label_match_list ',' label_match
|
|
|
|
{ $$ = append($$, $3) }
|
|
|
|
;
|
|
|
|
|
|
|
|
label_match : IDENTIFIER label_match_type STRING
|
|
|
|
{
|
|
|
|
var err error
|
|
|
|
$$, err = newLabelMatcher($2, clientmodel.LabelName($1), clientmodel.LabelValue($3))
|
|
|
|
if err != nil { yylex.Error(err.Error()); return 1 }
|
|
|
|
}
|
|
|
|
;
|
|
|
|
|
|
|
|
label_match_type : '='
|
|
|
|
{ $$ = "=" }
|
|
|
|
| CMP_OP
|
|
|
|
{ $$ = $1 }
|
|
|
|
;
|
2013-01-07 22:24:26 +00:00
|
|
|
|
|
|
|
rule_expr : '(' rule_expr ')'
|
|
|
|
{ $$ = $2 }
|
2014-03-28 10:58:47 +00:00
|
|
|
| metric_name label_matches
|
|
|
|
{
|
|
|
|
var err error
|
|
|
|
m, err := metric.NewLabelMatcher(metric.Equal, clientmodel.MetricNameLabel, clientmodel.LabelValue($1))
|
|
|
|
if err != nil { yylex.Error(err.Error()); return 1 }
|
|
|
|
$2 = append($2, m)
|
|
|
|
$$ = ast.NewVectorSelector($2)
|
|
|
|
}
|
2013-01-07 22:24:26 +00:00
|
|
|
| IDENTIFIER '(' func_arg_list ')'
|
|
|
|
{
|
|
|
|
var err error
|
|
|
|
$$, err = NewFunctionCall($1, $3)
|
|
|
|
if err != nil { yylex.Error(err.Error()); return 1 }
|
|
|
|
}
|
|
|
|
| IDENTIFIER '(' ')'
|
|
|
|
{
|
|
|
|
var err error
|
|
|
|
$$, err = NewFunctionCall($1, []ast.Node{})
|
|
|
|
if err != nil { yylex.Error(err.Error()); return 1 }
|
|
|
|
}
|
2013-01-22 00:51:26 +00:00
|
|
|
| rule_expr '[' DURATION ']'
|
2013-01-11 00:17:37 +00:00
|
|
|
{
|
|
|
|
var err error
|
2014-02-22 21:29:32 +00:00
|
|
|
$$, err = NewMatrixSelector($1, $3)
|
2013-01-11 00:17:37 +00:00
|
|
|
if err != nil { yylex.Error(err.Error()); return 1 }
|
|
|
|
}
|
2013-12-13 18:20:42 +00:00
|
|
|
| AGGR_OP '(' rule_expr ')' grouping_opts extra_labels_opts
|
2013-01-07 22:24:26 +00:00
|
|
|
{
|
|
|
|
var err error
|
2013-12-13 18:20:42 +00:00
|
|
|
$$, err = NewVectorAggregation($1, $3, $5, $6)
|
2013-01-07 22:24:26 +00:00
|
|
|
if err != nil { yylex.Error(err.Error()); return 1 }
|
|
|
|
}
|
2013-01-22 00:51:26 +00:00
|
|
|
/* Yacc can only attach associativity to terminals, so we
|
2013-01-07 22:24:26 +00:00
|
|
|
* have to list all operators here. */
|
|
|
|
| rule_expr ADDITIVE_OP rule_expr
|
|
|
|
{
|
|
|
|
var err error
|
|
|
|
$$, err = NewArithExpr($2, $1, $3)
|
|
|
|
if err != nil { yylex.Error(err.Error()); return 1 }
|
|
|
|
}
|
|
|
|
| rule_expr MULT_OP rule_expr
|
|
|
|
{
|
|
|
|
var err error
|
|
|
|
$$, err = NewArithExpr($2, $1, $3)
|
|
|
|
if err != nil { yylex.Error(err.Error()); return 1 }
|
|
|
|
}
|
|
|
|
| rule_expr CMP_OP rule_expr
|
|
|
|
{
|
|
|
|
var err error
|
|
|
|
$$, err = NewArithExpr($2, $1, $3)
|
|
|
|
if err != nil { yylex.Error(err.Error()); return 1 }
|
|
|
|
}
|
|
|
|
| NUMBER
|
|
|
|
{ $$ = ast.NewScalarLiteral($1)}
|
|
|
|
;
|
|
|
|
|
2013-12-13 18:20:42 +00:00
|
|
|
extra_labels_opts :
|
|
|
|
{ $$ = false }
|
|
|
|
| KEEPING_EXTRA
|
|
|
|
{ $$ = true }
|
|
|
|
;
|
|
|
|
|
2013-01-07 22:24:26 +00:00
|
|
|
grouping_opts :
|
2013-06-25 12:02:27 +00:00
|
|
|
{ $$ = clientmodel.LabelNames{} }
|
2013-01-07 22:24:26 +00:00
|
|
|
| GROUP_OP '(' label_list ')'
|
|
|
|
{ $$ = $3 }
|
|
|
|
;
|
|
|
|
|
|
|
|
label_list : IDENTIFIER
|
2013-06-25 12:02:27 +00:00
|
|
|
{ $$ = clientmodel.LabelNames{clientmodel.LabelName($1)} }
|
2013-01-07 22:24:26 +00:00
|
|
|
| label_list ',' IDENTIFIER
|
2013-06-25 12:02:27 +00:00
|
|
|
{ $$ = append($$, clientmodel.LabelName($3)) }
|
2013-01-07 22:24:26 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
func_arg_list : func_arg
|
|
|
|
{ $$ = []ast.Node{$1} }
|
|
|
|
| func_arg_list ',' func_arg
|
|
|
|
{ $$ = append($$, $3) }
|
|
|
|
;
|
|
|
|
|
|
|
|
func_arg : rule_expr
|
|
|
|
{ $$ = $1 }
|
|
|
|
| STRING
|
|
|
|
{ $$ = ast.NewStringLiteral($1) }
|
|
|
|
;
|
|
|
|
%%
|