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 ast
|
|
|
|
|
|
|
|
import (
|
2013-01-12 20:22:59 +00:00
|
|
|
"encoding/json"
|
2013-01-07 22:24:26 +00:00
|
|
|
"fmt"
|
2013-03-26 14:32:48 +00:00
|
|
|
"github.com/prometheus/prometheus/model"
|
2013-06-03 15:07:03 +00:00
|
|
|
"github.com/prometheus/prometheus/stats"
|
2013-05-07 11:15:10 +00:00
|
|
|
"github.com/prometheus/prometheus/storage/metric"
|
2013-02-13 16:41:35 +00:00
|
|
|
"github.com/prometheus/prometheus/utility"
|
2013-01-07 22:24:26 +00:00
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2013-01-11 01:27:03 +00:00
|
|
|
type OutputFormat int
|
|
|
|
|
|
|
|
const (
|
2013-01-12 20:22:59 +00:00
|
|
|
TEXT OutputFormat = iota
|
|
|
|
JSON
|
2013-01-11 01:27:03 +00:00
|
|
|
)
|
|
|
|
|
2013-04-26 14:02:52 +00:00
|
|
|
func (opType BinOpType) String() string {
|
2013-01-07 22:24:26 +00:00
|
|
|
opTypeMap := map[BinOpType]string{
|
|
|
|
ADD: "+",
|
|
|
|
SUB: "-",
|
|
|
|
MUL: "*",
|
|
|
|
DIV: "/",
|
|
|
|
MOD: "%",
|
|
|
|
GT: ">",
|
|
|
|
LT: "<",
|
|
|
|
EQ: "==",
|
|
|
|
NE: "!=",
|
|
|
|
GE: ">=",
|
|
|
|
LE: "<=",
|
2013-06-06 13:12:37 +00:00
|
|
|
AND: "AND",
|
|
|
|
OR: "OR",
|
2013-01-07 22:24:26 +00:00
|
|
|
}
|
|
|
|
return opTypeMap[opType]
|
|
|
|
}
|
|
|
|
|
2013-04-26 14:02:52 +00:00
|
|
|
func (aggrType AggrType) String() string {
|
2013-01-07 22:24:26 +00:00
|
|
|
aggrTypeMap := map[AggrType]string{
|
2013-05-08 14:35:16 +00:00
|
|
|
SUM: "SUM",
|
|
|
|
AVG: "AVG",
|
|
|
|
MIN: "MIN",
|
|
|
|
MAX: "MAX",
|
|
|
|
COUNT: "COUNT",
|
2013-01-07 22:24:26 +00:00
|
|
|
}
|
|
|
|
return aggrTypeMap[aggrType]
|
|
|
|
}
|
|
|
|
|
2013-04-26 14:02:52 +00:00
|
|
|
func (exprType ExprType) String() string {
|
2013-01-11 01:27:03 +00:00
|
|
|
exprTypeMap := map[ExprType]string{
|
|
|
|
SCALAR: "scalar",
|
|
|
|
VECTOR: "vector",
|
|
|
|
MATRIX: "matrix",
|
|
|
|
STRING: "string",
|
|
|
|
}
|
|
|
|
return exprTypeMap[exprType]
|
|
|
|
}
|
|
|
|
|
2013-03-14 21:47:17 +00:00
|
|
|
func (vector Vector) String() string {
|
2013-06-06 13:12:37 +00:00
|
|
|
metricStrings := make([]string, 0, len(vector))
|
2013-01-07 22:24:26 +00:00
|
|
|
for _, sample := range vector {
|
|
|
|
metricStrings = append(metricStrings,
|
2013-06-11 09:01:36 +00:00
|
|
|
fmt.Sprintf("%s => %v @[%v]",
|
|
|
|
sample.Metric,
|
2013-01-07 22:24:26 +00:00
|
|
|
sample.Value, sample.Timestamp))
|
|
|
|
}
|
|
|
|
return strings.Join(metricStrings, "\n")
|
|
|
|
}
|
|
|
|
|
2013-03-14 21:47:17 +00:00
|
|
|
func (matrix Matrix) String() string {
|
2013-06-06 13:12:37 +00:00
|
|
|
metricStrings := make([]string, 0, len(matrix))
|
2013-01-11 01:27:03 +00:00
|
|
|
for _, sampleSet := range matrix {
|
2013-03-26 14:32:48 +00:00
|
|
|
metricName, ok := sampleSet.Metric[model.MetricNameLabel]
|
2013-01-11 01:27:03 +00:00
|
|
|
if !ok {
|
|
|
|
panic("Tried to print matrix without metric name")
|
|
|
|
}
|
2013-06-06 13:12:37 +00:00
|
|
|
labelStrings := make([]string, 0, len(sampleSet.Metric)-1)
|
2013-01-11 01:27:03 +00:00
|
|
|
for label, value := range sampleSet.Metric {
|
2013-03-26 14:32:48 +00:00
|
|
|
if label != model.MetricNameLabel {
|
2013-06-11 09:01:36 +00:00
|
|
|
labelStrings = append(labelStrings, fmt.Sprintf("%s=%q", label, value))
|
2013-01-11 01:27:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
sort.Strings(labelStrings)
|
2013-06-06 13:12:37 +00:00
|
|
|
valueStrings := make([]string, 0, len(sampleSet.Values))
|
2013-01-12 20:22:59 +00:00
|
|
|
for _, value := range sampleSet.Values {
|
|
|
|
valueStrings = append(valueStrings,
|
|
|
|
fmt.Sprintf("\n%v @[%v]", value.Value, value.Timestamp))
|
|
|
|
}
|
2013-01-11 01:27:03 +00:00
|
|
|
metricStrings = append(metricStrings,
|
2013-04-26 14:02:52 +00:00
|
|
|
fmt.Sprintf("%s{%s} => %s",
|
2013-01-11 01:27:03 +00:00
|
|
|
metricName,
|
2013-06-11 09:01:36 +00:00
|
|
|
strings.Join(labelStrings, ", "),
|
2013-01-11 01:27:03 +00:00
|
|
|
strings.Join(valueStrings, ", ")))
|
|
|
|
}
|
|
|
|
sort.Strings(metricStrings)
|
|
|
|
return strings.Join(metricStrings, "\n")
|
|
|
|
}
|
|
|
|
|
2013-01-18 00:54:26 +00:00
|
|
|
func ErrorToJSON(err error) string {
|
2013-01-12 20:22:59 +00:00
|
|
|
errorStruct := struct {
|
|
|
|
Type string
|
2013-01-22 00:50:16 +00:00
|
|
|
Value string
|
2013-01-12 20:22:59 +00:00
|
|
|
}{
|
|
|
|
Type: "error",
|
2013-01-22 00:50:16 +00:00
|
|
|
Value: err.Error(),
|
2013-01-12 20:22:59 +00:00
|
|
|
}
|
2013-01-11 01:27:03 +00:00
|
|
|
|
2013-01-12 20:22:59 +00:00
|
|
|
errorJSON, err := json.MarshalIndent(errorStruct, "", "\t")
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return string(errorJSON)
|
2013-01-11 01:27:03 +00:00
|
|
|
}
|
|
|
|
|
2013-01-15 10:30:55 +00:00
|
|
|
func TypedValueToJSON(data interface{}, typeStr string) string {
|
2013-01-12 20:22:59 +00:00
|
|
|
dataStruct := struct {
|
|
|
|
Type string
|
|
|
|
Value interface{}
|
|
|
|
}{
|
|
|
|
Type: typeStr,
|
|
|
|
Value: data,
|
|
|
|
}
|
|
|
|
dataJSON, err := json.MarshalIndent(dataStruct, "", "\t")
|
|
|
|
if err != nil {
|
2013-01-18 00:54:26 +00:00
|
|
|
return ErrorToJSON(err)
|
2013-01-12 20:22:59 +00:00
|
|
|
}
|
|
|
|
return string(dataJSON)
|
2013-01-11 01:27:03 +00:00
|
|
|
}
|
|
|
|
|
2013-06-03 15:07:03 +00:00
|
|
|
func EvalToString(node Node, timestamp time.Time, format OutputFormat, storage *metric.TieredStorage, queryStats *stats.TimerGroup) string {
|
|
|
|
viewTimer := queryStats.GetTimer(stats.TotalViewBuildingTime).Start()
|
|
|
|
viewAdapter, err := viewAdapterForInstantQuery(node, timestamp, storage, queryStats)
|
|
|
|
viewTimer.Stop()
|
2013-03-21 17:06:15 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2013-06-03 15:07:03 +00:00
|
|
|
evalTimer := queryStats.GetTimer(stats.InnerEvalTime).Start()
|
2013-01-12 20:22:59 +00:00
|
|
|
switch node.Type() {
|
|
|
|
case SCALAR:
|
2013-03-21 17:06:15 +00:00
|
|
|
scalar := node.(ScalarNode).Eval(timestamp, viewAdapter)
|
2013-06-03 15:07:03 +00:00
|
|
|
evalTimer.Stop()
|
2013-01-12 20:22:59 +00:00
|
|
|
switch format {
|
|
|
|
case TEXT:
|
|
|
|
return fmt.Sprintf("scalar: %v", scalar)
|
|
|
|
case JSON:
|
2013-01-15 10:30:55 +00:00
|
|
|
return TypedValueToJSON(scalar, "scalar")
|
2013-01-12 20:22:59 +00:00
|
|
|
}
|
|
|
|
case VECTOR:
|
2013-03-21 17:06:15 +00:00
|
|
|
vector := node.(VectorNode).Eval(timestamp, viewAdapter)
|
2013-06-03 15:07:03 +00:00
|
|
|
evalTimer.Stop()
|
2013-01-12 20:22:59 +00:00
|
|
|
switch format {
|
|
|
|
case TEXT:
|
2013-03-14 21:47:17 +00:00
|
|
|
return vector.String()
|
2013-01-12 20:22:59 +00:00
|
|
|
case JSON:
|
2013-01-15 10:30:55 +00:00
|
|
|
return TypedValueToJSON(vector, "vector")
|
2013-01-12 20:22:59 +00:00
|
|
|
}
|
|
|
|
case MATRIX:
|
2013-03-21 17:06:15 +00:00
|
|
|
matrix := node.(MatrixNode).Eval(timestamp, viewAdapter)
|
2013-06-03 15:07:03 +00:00
|
|
|
evalTimer.Stop()
|
2013-01-12 20:22:59 +00:00
|
|
|
switch format {
|
|
|
|
case TEXT:
|
2013-03-14 21:47:17 +00:00
|
|
|
return matrix.String()
|
2013-01-12 20:22:59 +00:00
|
|
|
case JSON:
|
2013-01-15 10:30:55 +00:00
|
|
|
return TypedValueToJSON(matrix, "matrix")
|
2013-01-12 20:22:59 +00:00
|
|
|
}
|
|
|
|
case STRING:
|
2013-03-21 17:06:15 +00:00
|
|
|
str := node.(StringNode).Eval(timestamp, viewAdapter)
|
2013-06-03 15:07:03 +00:00
|
|
|
evalTimer.Stop()
|
2013-01-12 20:22:59 +00:00
|
|
|
switch format {
|
|
|
|
case TEXT:
|
|
|
|
return str
|
|
|
|
case JSON:
|
2013-01-15 10:30:55 +00:00
|
|
|
return TypedValueToJSON(str, "string")
|
2013-01-12 20:22:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
panic("Switch didn't cover all node types")
|
2013-01-11 01:27:03 +00:00
|
|
|
}
|
|
|
|
|
2013-01-07 22:24:26 +00:00
|
|
|
func (node *ScalarLiteral) NodeTreeToDotGraph() string {
|
|
|
|
return fmt.Sprintf("%#p[label=\"%v\"];\n", node, node.value)
|
|
|
|
}
|
|
|
|
|
|
|
|
func functionArgsToDotGraph(node Node, args []Node) string {
|
|
|
|
graph := ""
|
|
|
|
for _, arg := range args {
|
|
|
|
graph += fmt.Sprintf("%#p -> %#p;\n", node, arg)
|
|
|
|
}
|
|
|
|
for _, arg := range args {
|
|
|
|
graph += arg.NodeTreeToDotGraph()
|
|
|
|
}
|
|
|
|
return graph
|
|
|
|
}
|
|
|
|
|
|
|
|
func (node *ScalarFunctionCall) NodeTreeToDotGraph() string {
|
2013-04-26 14:02:52 +00:00
|
|
|
graph := fmt.Sprintf("%#p[label=\"%s\"];\n", node, node.function.name)
|
2013-01-07 22:24:26 +00:00
|
|
|
graph += functionArgsToDotGraph(node, node.args)
|
|
|
|
return graph
|
|
|
|
}
|
|
|
|
|
|
|
|
func (node *ScalarArithExpr) NodeTreeToDotGraph() string {
|
2013-04-26 14:02:52 +00:00
|
|
|
graph := fmt.Sprintf(`
|
|
|
|
%#p[label="%s"];
|
|
|
|
%#p -> %#p;
|
|
|
|
%#p -> %#p;
|
|
|
|
%s
|
|
|
|
%s
|
|
|
|
}`, node, node.opType, node, node.lhs, node, node.rhs, node.lhs.NodeTreeToDotGraph(), node.rhs.NodeTreeToDotGraph())
|
2013-01-07 22:24:26 +00:00
|
|
|
return graph
|
|
|
|
}
|
|
|
|
|
|
|
|
func (node *VectorLiteral) NodeTreeToDotGraph() string {
|
2013-04-26 14:02:52 +00:00
|
|
|
return fmt.Sprintf("%#p[label=\"%s\"];\n", node, node)
|
2013-01-07 22:24:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (node *VectorFunctionCall) NodeTreeToDotGraph() string {
|
2013-04-26 14:02:52 +00:00
|
|
|
graph := fmt.Sprintf("%#p[label=\"%s\"];\n", node, node.function.name)
|
2013-01-07 22:24:26 +00:00
|
|
|
graph += functionArgsToDotGraph(node, node.args)
|
|
|
|
return graph
|
|
|
|
}
|
|
|
|
|
|
|
|
func (node *VectorAggregation) NodeTreeToDotGraph() string {
|
2013-06-06 13:12:37 +00:00
|
|
|
groupByStrings := make([]string, 0, len(node.groupBy))
|
2013-01-07 22:24:26 +00:00
|
|
|
for _, label := range node.groupBy {
|
|
|
|
groupByStrings = append(groupByStrings, string(label))
|
|
|
|
}
|
|
|
|
|
2013-04-26 14:02:52 +00:00
|
|
|
graph := fmt.Sprintf("%#p[label=\"%s BY (%s)\"]\n",
|
2013-01-07 22:24:26 +00:00
|
|
|
node,
|
2013-04-26 14:02:52 +00:00
|
|
|
node.aggrType,
|
2013-01-07 22:24:26 +00:00
|
|
|
strings.Join(groupByStrings, ", "))
|
|
|
|
graph += fmt.Sprintf("%#p -> %#p;\n", node, node.vector)
|
|
|
|
graph += node.vector.NodeTreeToDotGraph()
|
|
|
|
return graph
|
|
|
|
}
|
|
|
|
|
|
|
|
func (node *VectorArithExpr) NodeTreeToDotGraph() string {
|
2013-04-26 14:02:52 +00:00
|
|
|
graph := fmt.Sprintf(`
|
|
|
|
%#p[label="%s"];
|
|
|
|
%#p -> %#p;
|
|
|
|
%#p -> %#p;
|
|
|
|
%s
|
|
|
|
%s
|
|
|
|
`, node, node.opType, node, node.lhs, node, node.rhs, node.lhs.NodeTreeToDotGraph(), node.rhs.NodeTreeToDotGraph())
|
2013-01-07 22:24:26 +00:00
|
|
|
return graph
|
|
|
|
}
|
|
|
|
|
|
|
|
func (node *MatrixLiteral) NodeTreeToDotGraph() string {
|
2013-04-26 14:02:52 +00:00
|
|
|
return fmt.Sprintf("%#p[label=\"%s\"];\n", node, node)
|
2013-01-07 22:24:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (node *StringLiteral) NodeTreeToDotGraph() string {
|
2013-06-11 09:01:36 +00:00
|
|
|
return fmt.Sprintf("%#p[label=\"'%q'\"];\n", node, node.str)
|
2013-01-07 22:24:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (node *StringFunctionCall) NodeTreeToDotGraph() string {
|
2013-04-26 14:02:52 +00:00
|
|
|
graph := fmt.Sprintf("%#p[label=\"%s\"];\n", node, node.function.name)
|
2013-01-07 22:24:26 +00:00
|
|
|
graph += functionArgsToDotGraph(node, node.args)
|
|
|
|
return graph
|
|
|
|
}
|
2013-06-06 13:12:37 +00:00
|
|
|
|
|
|
|
func (nodes Nodes) String() string {
|
|
|
|
nodeStrings := make([]string, 0, len(nodes))
|
|
|
|
for _, node := range nodes {
|
|
|
|
nodeStrings = append(nodeStrings, node.String())
|
|
|
|
}
|
|
|
|
return strings.Join(nodeStrings, ", ")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (node *ScalarLiteral) String() string {
|
|
|
|
return fmt.Sprint(node.value)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (node *ScalarFunctionCall) String() string {
|
|
|
|
return fmt.Sprintf("%s(%s)", node.function.name, node.args)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (node *ScalarArithExpr) String() string {
|
|
|
|
return fmt.Sprintf("(%s %s %s)", node.lhs, node.opType, node.rhs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (node *VectorLiteral) String() string {
|
|
|
|
metricName, ok := node.labels[model.MetricNameLabel]
|
|
|
|
if !ok {
|
|
|
|
panic("Tried to print vector without metric name")
|
|
|
|
}
|
|
|
|
labelStrings := make([]string, 0, len(node.labels)-1)
|
|
|
|
for label, value := range node.labels {
|
|
|
|
if label != model.MetricNameLabel {
|
2013-06-11 09:01:36 +00:00
|
|
|
labelStrings = append(labelStrings, fmt.Sprintf("%s=%q", label, value))
|
2013-06-06 13:12:37 +00:00
|
|
|
}
|
|
|
|
}
|
2013-06-11 09:01:36 +00:00
|
|
|
|
|
|
|
switch len(labelStrings) {
|
|
|
|
case 0:
|
|
|
|
return string(metricName)
|
|
|
|
default:
|
|
|
|
sort.Strings(labelStrings)
|
|
|
|
return fmt.Sprintf("%s{%s}", metricName, strings.Join(labelStrings, ","))
|
|
|
|
}
|
2013-06-06 13:12:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (node *VectorFunctionCall) String() string {
|
|
|
|
return fmt.Sprintf("%s(%s)", node.function.name, node.args)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (node *VectorAggregation) String() string {
|
|
|
|
return fmt.Sprintf("%s(%s) BY (%s)", node.aggrType, node.vector, node.groupBy)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (node *VectorArithExpr) String() string {
|
|
|
|
return fmt.Sprintf("(%s %s %s)", node.lhs, node.opType, node.rhs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (node *MatrixLiteral) String() string {
|
|
|
|
vectorString := (&VectorLiteral{labels: node.labels}).String()
|
|
|
|
intervalString := fmt.Sprintf("[%s]", utility.DurationToString(node.interval))
|
|
|
|
return vectorString + intervalString
|
|
|
|
}
|
|
|
|
|
|
|
|
func (node *StringLiteral) String() string {
|
2013-06-11 09:01:36 +00:00
|
|
|
return fmt.Sprintf("%q", node.str)
|
2013-06-06 13:12:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (node *StringFunctionCall) String() string {
|
|
|
|
return fmt.Sprintf("%s(%s)", node.function.name, node.args)
|
|
|
|
}
|