2015-03-30 09:12:51 -07:00
|
|
|
// Copyright 2015 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.
|
|
|
|
|
2020-02-03 08:23:44 -08:00
|
|
|
package parser
|
2015-03-30 09:12:51 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
2015-11-15 01:26:38 -08:00
|
|
|
"time"
|
2015-03-30 09:12:51 -07:00
|
|
|
|
2015-08-20 08:18:46 -07:00
|
|
|
"github.com/prometheus/common/model"
|
2020-10-22 02:00:08 -07:00
|
|
|
|
2016-12-24 05:35:24 -08:00
|
|
|
"github.com/prometheus/prometheus/pkg/labels"
|
2015-03-30 09:12:51 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// Tree returns a string of the tree structure of the given node.
|
|
|
|
func Tree(node Node) string {
|
|
|
|
return tree(node, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
func tree(node Node, level string) string {
|
2015-04-29 07:35:18 -07:00
|
|
|
if node == nil {
|
|
|
|
return fmt.Sprintf("%s |---- %T\n", level, node)
|
|
|
|
}
|
2015-03-30 09:12:51 -07:00
|
|
|
typs := strings.Split(fmt.Sprintf("%T", node), ".")[1]
|
|
|
|
|
2018-02-12 04:09:51 -08:00
|
|
|
t := fmt.Sprintf("%s |---- %s :: %s\n", level, typs, node)
|
2015-03-30 09:12:51 -07:00
|
|
|
|
|
|
|
level += " · · ·"
|
|
|
|
|
2019-11-25 03:41:59 -08:00
|
|
|
for _, e := range Children(node) {
|
|
|
|
t += tree(e, level)
|
2015-03-30 09:12:51 -07:00
|
|
|
}
|
2019-11-25 03:41:59 -08:00
|
|
|
|
2015-03-30 09:12:51 -07:00
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
func (node *EvalStmt) String() string {
|
|
|
|
return "EVAL " + node.Expr.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (es Expressions) String() (s string) {
|
|
|
|
if len(es) == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
for _, e := range es {
|
|
|
|
s += e.String()
|
|
|
|
s += ", "
|
|
|
|
}
|
|
|
|
return s[:len(s)-2]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (node *AggregateExpr) String() string {
|
2018-01-22 02:14:59 -08:00
|
|
|
aggrString := node.Op.String()
|
2018-01-21 13:22:55 -08:00
|
|
|
|
|
|
|
if node.Without {
|
2018-01-22 02:14:59 -08:00
|
|
|
aggrString += fmt.Sprintf(" without(%s) ", strings.Join(node.Grouping, ", "))
|
2018-01-21 13:22:55 -08:00
|
|
|
} else {
|
|
|
|
if len(node.Grouping) > 0 {
|
2018-01-22 02:14:59 -08:00
|
|
|
aggrString += fmt.Sprintf(" by(%s) ", strings.Join(node.Grouping, ", "))
|
2016-02-07 10:03:16 -08:00
|
|
|
}
|
2016-04-15 10:48:17 -07:00
|
|
|
}
|
2018-01-21 13:22:55 -08:00
|
|
|
|
2018-01-22 02:14:59 -08:00
|
|
|
aggrString += "("
|
2020-02-03 09:48:27 -08:00
|
|
|
if node.Op.IsAggregatorWithParam() {
|
2018-01-22 02:14:59 -08:00
|
|
|
aggrString += fmt.Sprintf("%s, ", node.Param)
|
|
|
|
}
|
|
|
|
aggrString += fmt.Sprintf("%s)", node.Expr)
|
|
|
|
|
2015-03-30 09:12:51 -07:00
|
|
|
return aggrString
|
|
|
|
}
|
|
|
|
|
|
|
|
func (node *BinaryExpr) String() string {
|
2015-09-09 16:37:05 -07:00
|
|
|
returnBool := ""
|
|
|
|
if node.ReturnBool {
|
2018-01-22 02:14:59 -08:00
|
|
|
returnBool = " bool"
|
2015-09-09 16:37:05 -07:00
|
|
|
}
|
|
|
|
|
2015-03-30 09:12:51 -07:00
|
|
|
matching := ""
|
|
|
|
vm := node.VectorMatching
|
2016-10-19 10:38:26 -07:00
|
|
|
if vm != nil && (len(vm.MatchingLabels) > 0 || vm.On) {
|
2016-06-23 09:23:44 -07:00
|
|
|
if vm.On {
|
2018-01-22 02:14:59 -08:00
|
|
|
matching = fmt.Sprintf(" on(%s)", strings.Join(vm.MatchingLabels, ", "))
|
2016-06-23 09:23:44 -07:00
|
|
|
} else {
|
2018-01-22 02:14:59 -08:00
|
|
|
matching = fmt.Sprintf(" ignoring(%s)", strings.Join(vm.MatchingLabels, ", "))
|
2016-04-21 03:45:06 -07:00
|
|
|
}
|
2016-05-28 10:45:35 -07:00
|
|
|
if vm.Card == CardManyToOne || vm.Card == CardOneToMany {
|
2018-01-22 02:14:59 -08:00
|
|
|
matching += " group_"
|
2016-05-28 10:45:35 -07:00
|
|
|
if vm.Card == CardManyToOne {
|
2018-01-22 02:14:59 -08:00
|
|
|
matching += "left"
|
2016-05-28 10:45:35 -07:00
|
|
|
} else {
|
2018-01-22 02:14:59 -08:00
|
|
|
matching += "right"
|
2016-05-28 10:45:35 -07:00
|
|
|
}
|
2016-12-28 00:16:48 -08:00
|
|
|
matching += fmt.Sprintf("(%s)", strings.Join(vm.Include, ", "))
|
2015-03-30 09:12:51 -07:00
|
|
|
}
|
|
|
|
}
|
2015-09-09 16:37:05 -07:00
|
|
|
return fmt.Sprintf("%s %s%s%s %s", node.LHS, node.Op, returnBool, matching, node.RHS)
|
2015-03-30 09:12:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (node *Call) String() string {
|
|
|
|
return fmt.Sprintf("%s(%s)", node.Func.Name, node.Args)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (node *MatrixSelector) String() string {
|
2020-01-10 06:25:41 -08:00
|
|
|
// Copy the Vector selector before changing the offset
|
2020-03-23 07:47:11 -07:00
|
|
|
vecSelector := *node.VectorSelector.(*VectorSelector)
|
2015-11-15 01:26:38 -08:00
|
|
|
offset := ""
|
2021-02-23 18:16:28 -08:00
|
|
|
if vecSelector.OriginalOffset > time.Duration(0) {
|
2021-01-20 02:57:39 -08:00
|
|
|
offset = fmt.Sprintf(" offset %s", model.Duration(vecSelector.OriginalOffset))
|
2021-02-23 18:16:28 -08:00
|
|
|
} else if vecSelector.OriginalOffset < time.Duration(0) {
|
|
|
|
offset = fmt.Sprintf(" offset -%s", model.Duration(-vecSelector.OriginalOffset))
|
2015-11-15 01:26:38 -08:00
|
|
|
}
|
2021-01-20 02:57:39 -08:00
|
|
|
at := ""
|
|
|
|
if vecSelector.Timestamp != nil {
|
|
|
|
at = fmt.Sprintf(" @ %.3f", float64(*vecSelector.Timestamp)/1000.0)
|
2021-02-09 08:03:16 -08:00
|
|
|
} else if vecSelector.StartOrEnd == START {
|
|
|
|
at = " @ start()"
|
|
|
|
} else if vecSelector.StartOrEnd == END {
|
|
|
|
at = " @ end()"
|
2021-01-20 02:57:39 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Do not print the @ and offset twice.
|
2021-02-09 08:03:16 -08:00
|
|
|
offsetVal, atVal, preproc := vecSelector.OriginalOffset, vecSelector.Timestamp, vecSelector.StartOrEnd
|
2021-01-20 02:57:39 -08:00
|
|
|
vecSelector.OriginalOffset = 0
|
|
|
|
vecSelector.Timestamp = nil
|
2021-02-09 08:03:16 -08:00
|
|
|
vecSelector.StartOrEnd = 0
|
2021-01-20 02:57:39 -08:00
|
|
|
|
|
|
|
str := fmt.Sprintf("%s[%s]%s%s", vecSelector.String(), model.Duration(node.Range), at, offset)
|
2020-01-10 06:25:41 -08:00
|
|
|
|
2021-02-09 08:03:16 -08:00
|
|
|
vecSelector.OriginalOffset, vecSelector.Timestamp, vecSelector.StartOrEnd = offsetVal, atVal, preproc
|
2020-01-10 06:25:41 -08:00
|
|
|
|
2021-01-20 02:57:39 -08:00
|
|
|
return str
|
2015-03-30 09:12:51 -07:00
|
|
|
}
|
|
|
|
|
2018-12-22 05:47:13 -08:00
|
|
|
func (node *SubqueryExpr) String() string {
|
|
|
|
step := ""
|
|
|
|
if node.Step != 0 {
|
2019-01-04 05:47:38 -08:00
|
|
|
step = model.Duration(node.Step).String()
|
2018-12-22 05:47:13 -08:00
|
|
|
}
|
2019-11-25 22:45:51 -08:00
|
|
|
offset := ""
|
2021-02-23 18:16:28 -08:00
|
|
|
if node.OriginalOffset > time.Duration(0) {
|
2021-01-20 02:57:39 -08:00
|
|
|
offset = fmt.Sprintf(" offset %s", model.Duration(node.OriginalOffset))
|
2021-02-23 18:16:28 -08:00
|
|
|
} else if node.OriginalOffset < time.Duration(0) {
|
|
|
|
offset = fmt.Sprintf(" offset -%s", model.Duration(-node.OriginalOffset))
|
2019-11-25 22:45:51 -08:00
|
|
|
}
|
2021-01-20 02:57:39 -08:00
|
|
|
at := ""
|
|
|
|
if node.Timestamp != nil {
|
|
|
|
at = fmt.Sprintf(" @ %.3f", float64(*node.Timestamp)/1000.0)
|
2021-02-09 08:03:16 -08:00
|
|
|
} else if node.StartOrEnd == START {
|
|
|
|
at = " @ start()"
|
|
|
|
} else if node.StartOrEnd == END {
|
|
|
|
at = " @ end()"
|
2021-01-20 02:57:39 -08:00
|
|
|
}
|
|
|
|
return fmt.Sprintf("%s[%s:%s]%s%s", node.Expr.String(), model.Duration(node.Range), step, at, offset)
|
2018-12-22 05:47:13 -08:00
|
|
|
}
|
|
|
|
|
2015-03-30 09:12:51 -07:00
|
|
|
func (node *NumberLiteral) String() string {
|
|
|
|
return fmt.Sprint(node.Val)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (node *ParenExpr) String() string {
|
|
|
|
return fmt.Sprintf("(%s)", node.Expr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (node *StringLiteral) String() string {
|
2015-03-30 10:13:36 -07:00
|
|
|
return fmt.Sprintf("%q", node.Val)
|
2015-03-30 09:12:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (node *UnaryExpr) String() string {
|
|
|
|
return fmt.Sprintf("%s%s", node.Op, node.Expr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (node *VectorSelector) String() string {
|
|
|
|
labelStrings := make([]string, 0, len(node.LabelMatchers)-1)
|
|
|
|
for _, matcher := range node.LabelMatchers {
|
2019-05-10 16:46:15 -07:00
|
|
|
// Only include the __name__ label if its equality matching and matches the name.
|
|
|
|
if matcher.Name == labels.MetricName && matcher.Type == labels.MatchEqual && matcher.Value == node.Name {
|
2015-03-30 09:12:51 -07:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
labelStrings = append(labelStrings, matcher.String())
|
|
|
|
}
|
2015-11-15 01:26:38 -08:00
|
|
|
offset := ""
|
2021-02-23 18:16:28 -08:00
|
|
|
if node.OriginalOffset > time.Duration(0) {
|
2021-01-20 02:57:39 -08:00
|
|
|
offset = fmt.Sprintf(" offset %s", model.Duration(node.OriginalOffset))
|
2021-02-23 18:16:28 -08:00
|
|
|
} else if node.OriginalOffset < time.Duration(0) {
|
|
|
|
offset = fmt.Sprintf(" offset -%s", model.Duration(-node.OriginalOffset))
|
2021-01-20 02:57:39 -08:00
|
|
|
}
|
|
|
|
at := ""
|
|
|
|
if node.Timestamp != nil {
|
|
|
|
at = fmt.Sprintf(" @ %.3f", float64(*node.Timestamp)/1000.0)
|
2021-02-09 08:03:16 -08:00
|
|
|
} else if node.StartOrEnd == START {
|
|
|
|
at = " @ start()"
|
|
|
|
} else if node.StartOrEnd == END {
|
|
|
|
at = " @ end()"
|
2015-11-15 01:26:38 -08:00
|
|
|
}
|
2015-03-30 09:12:51 -07:00
|
|
|
|
|
|
|
if len(labelStrings) == 0 {
|
2021-01-20 02:57:39 -08:00
|
|
|
return fmt.Sprintf("%s%s%s", node.Name, at, offset)
|
2015-03-30 09:12:51 -07:00
|
|
|
}
|
|
|
|
sort.Strings(labelStrings)
|
2021-01-20 02:57:39 -08:00
|
|
|
return fmt.Sprintf("%s{%s}%s%s", node.Name, strings.Join(labelStrings, ","), at, offset)
|
2015-03-30 09:12:51 -07:00
|
|
|
}
|