mirror of
https://github.com/prometheus/prometheus.git
synced 2025-03-05 20:59:13 -08:00
promql: make matrix exported
This commit is contained in:
parent
a62df87022
commit
b3f71df350
|
@ -98,12 +98,12 @@ type Expr interface {
|
||||||
// Expressions is a list of expression nodes that implements Node.
|
// Expressions is a list of expression nodes that implements Node.
|
||||||
type Expressions []Expr
|
type Expressions []Expr
|
||||||
|
|
||||||
// AggregateExpr represents an aggregation operation on a vector.
|
// AggregateExpr represents an aggregation operation on a Vector.
|
||||||
type AggregateExpr struct {
|
type AggregateExpr struct {
|
||||||
Op itemType // The used aggregation operation.
|
Op itemType // The used aggregation operation.
|
||||||
Expr Expr // The vector expression over which is aggregated.
|
Expr Expr // The Vector expression over which is aggregated.
|
||||||
Param Expr // Parameter used by some aggregators.
|
Param Expr // Parameter used by some aggregators.
|
||||||
Grouping []string // The labels by which to group the vector.
|
Grouping []string // The labels by which to group the Vector.
|
||||||
Without bool // Whether to drop the given labels rather than keep them.
|
Without bool // Whether to drop the given labels rather than keep them.
|
||||||
KeepCommonLabels bool // Whether to keep common labels among result elements.
|
KeepCommonLabels bool // Whether to keep common labels among result elements.
|
||||||
}
|
}
|
||||||
|
@ -113,7 +113,7 @@ type BinaryExpr struct {
|
||||||
Op itemType // The operation of the expression.
|
Op itemType // The operation of the expression.
|
||||||
LHS, RHS Expr // The operands on the respective sides of the operator.
|
LHS, RHS Expr // The operands on the respective sides of the operator.
|
||||||
|
|
||||||
// The matching behavior for the operation if both operands are vectors.
|
// The matching behavior for the operation if both operands are Vectors.
|
||||||
// If they are not this field is nil.
|
// If they are not this field is nil.
|
||||||
VectorMatching *VectorMatching
|
VectorMatching *VectorMatching
|
||||||
|
|
||||||
|
@ -127,7 +127,7 @@ type Call struct {
|
||||||
Args Expressions // Arguments used in the call.
|
Args Expressions // Arguments used in the call.
|
||||||
}
|
}
|
||||||
|
|
||||||
// MatrixSelector represents a matrix selection.
|
// MatrixSelector represents a Matrix selection.
|
||||||
type MatrixSelector struct {
|
type MatrixSelector struct {
|
||||||
Name string
|
Name string
|
||||||
Range time.Duration
|
Range time.Duration
|
||||||
|
@ -162,7 +162,7 @@ type UnaryExpr struct {
|
||||||
Expr Expr
|
Expr Expr
|
||||||
}
|
}
|
||||||
|
|
||||||
// VectorSelector represents a vector selection.
|
// VectorSelector represents a Vector selection.
|
||||||
type VectorSelector struct {
|
type VectorSelector struct {
|
||||||
Name string
|
Name string
|
||||||
Offset time.Duration
|
Offset time.Duration
|
||||||
|
@ -199,7 +199,7 @@ func (*UnaryExpr) expr() {}
|
||||||
func (*VectorSelector) expr() {}
|
func (*VectorSelector) expr() {}
|
||||||
|
|
||||||
// VectorMatchCardinality describes the cardinality relationship
|
// VectorMatchCardinality describes the cardinality relationship
|
||||||
// of two vectors in a binary operation.
|
// of two Vectors in a binary operation.
|
||||||
type VectorMatchCardinality int
|
type VectorMatchCardinality int
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -223,13 +223,13 @@ func (vmc VectorMatchCardinality) String() string {
|
||||||
panic("promql.VectorMatchCardinality.String: unknown match cardinality")
|
panic("promql.VectorMatchCardinality.String: unknown match cardinality")
|
||||||
}
|
}
|
||||||
|
|
||||||
// VectorMatching describes how elements from two vectors in a binary
|
// VectorMatching describes how elements from two Vectors in a binary
|
||||||
// operation are supposed to be matched.
|
// operation are supposed to be matched.
|
||||||
type VectorMatching struct {
|
type VectorMatching struct {
|
||||||
// The cardinality of the two vectors.
|
// The cardinality of the two Vectors.
|
||||||
Card VectorMatchCardinality
|
Card VectorMatchCardinality
|
||||||
// MatchingLabels contains the labels which define equality of a pair of
|
// MatchingLabels contains the labels which define equality of a pair of
|
||||||
// elements from the vectors.
|
// elements from the Vectors.
|
||||||
MatchingLabels []string
|
MatchingLabels []string
|
||||||
// On includes the given label names from matching,
|
// On includes the given label names from matching,
|
||||||
// rather than excluding them.
|
// rather than excluding them.
|
||||||
|
|
|
@ -50,7 +50,7 @@ type Value interface {
|
||||||
String() string
|
String() string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (matrix) Type() ValueType { return ValueTypeMatrix }
|
func (Matrix) Type() ValueType { return ValueTypeMatrix }
|
||||||
func (Vector) Type() ValueType { return ValueTypeVector }
|
func (Vector) Type() ValueType { return ValueTypeVector }
|
||||||
func (scalar) Type() ValueType { return ValueTypeScalar }
|
func (scalar) Type() ValueType { return ValueTypeScalar }
|
||||||
func (stringVal) Type() ValueType { return ValueTypeString }
|
func (stringVal) Type() ValueType { return ValueTypeString }
|
||||||
|
@ -63,7 +63,7 @@ const (
|
||||||
ValueTypeNone = "none"
|
ValueTypeNone = "none"
|
||||||
ValueTypeVector = "Vector"
|
ValueTypeVector = "Vector"
|
||||||
ValueTypeScalar = "scalar"
|
ValueTypeScalar = "scalar"
|
||||||
ValueTypeMatrix = "matrix"
|
ValueTypeMatrix = "Matrix"
|
||||||
ValueTypeString = "string"
|
ValueTypeString = "string"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -127,11 +127,11 @@ func (vec Vector) String() string {
|
||||||
return strings.Join(entries, "\n")
|
return strings.Join(entries, "\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
// matrix is a slice of SampleStreams that implements sort.Interface and
|
// Matrix is a slice of SampleStreams that implements sort.Interface and
|
||||||
// has a String method.
|
// has a String method.
|
||||||
type matrix []sampleStream
|
type Matrix []sampleStream
|
||||||
|
|
||||||
func (m matrix) String() string {
|
func (m Matrix) String() string {
|
||||||
// TODO(fabxc): sort, or can we rely on order from the querier?
|
// TODO(fabxc): sort, or can we rely on order from the querier?
|
||||||
strs := make([]string, len(m))
|
strs := make([]string, len(m))
|
||||||
|
|
||||||
|
@ -162,13 +162,13 @@ func (r *Result) Vector() (Vector, error) {
|
||||||
return v, nil
|
return v, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Matrix returns a matrix. An error is returned if
|
// Matrix returns a Matrix. An error is returned if
|
||||||
// the result was an error or the result value is not a matrix.
|
// the result was an error or the result value is not a Matrix.
|
||||||
func (r *Result) Matrix() (matrix, error) {
|
func (r *Result) Matrix() (Matrix, error) {
|
||||||
if r.Err != nil {
|
if r.Err != nil {
|
||||||
return nil, r.Err
|
return nil, r.Err
|
||||||
}
|
}
|
||||||
v, ok := r.Value.(matrix)
|
v, ok := r.Value.(Matrix)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("query result is not a range Vector")
|
return nil, fmt.Errorf("query result is not a range Vector")
|
||||||
}
|
}
|
||||||
|
@ -509,7 +509,7 @@ func (ng *Engine) execEvalStmt(ctx context.Context, query *query, s *EvalStmt) (
|
||||||
}
|
}
|
||||||
|
|
||||||
appendTimer := query.stats.GetTimer(stats.ResultAppendTime).Start()
|
appendTimer := query.stats.GetTimer(stats.ResultAppendTime).Start()
|
||||||
mat := matrix{}
|
mat := Matrix{}
|
||||||
for _, ss := range sampleStreams {
|
for _, ss := range sampleStreams {
|
||||||
mat = append(mat, ss)
|
mat = append(mat, ss)
|
||||||
}
|
}
|
||||||
|
@ -519,7 +519,7 @@ func (ng *Engine) execEvalStmt(ctx context.Context, query *query, s *EvalStmt) (
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Turn matrix type with protected metric into model.Matrix.
|
// Turn Matrix type with protected metric into model.Matrix.
|
||||||
resMatrix := mat
|
resMatrix := mat
|
||||||
|
|
||||||
// TODO(fabxc): order ensured by storage?
|
// TODO(fabxc): order ensured by storage?
|
||||||
|
@ -668,12 +668,12 @@ func (ev *evaluator) evalFloat(e Expr) float64 {
|
||||||
return float64(sc.v)
|
return float64(sc.v)
|
||||||
}
|
}
|
||||||
|
|
||||||
// evalMatrix attempts to evaluate e into a matrix and errors otherwise.
|
// evalMatrix attempts to evaluate e into a Matrix and errors otherwise.
|
||||||
// The error message uses the term "range Vector" to match the user facing
|
// The error message uses the term "range Vector" to match the user facing
|
||||||
// documentation.
|
// documentation.
|
||||||
func (ev *evaluator) evalMatrix(e Expr) matrix {
|
func (ev *evaluator) evalMatrix(e Expr) Matrix {
|
||||||
val := ev.eval(e)
|
val := ev.eval(e)
|
||||||
mat, ok := val.(matrix)
|
mat, ok := val.(Matrix)
|
||||||
if !ok {
|
if !ok {
|
||||||
ev.errorf("expected range Vector but got %s", documentedType(val.Type()))
|
ev.errorf("expected range Vector but got %s", documentedType(val.Type()))
|
||||||
}
|
}
|
||||||
|
@ -750,7 +750,7 @@ func (ev *evaluator) eval(expr Expr) Value {
|
||||||
return e.Func.Call(ev, e.Args)
|
return e.Func.Call(ev, e.Args)
|
||||||
|
|
||||||
case *MatrixSelector:
|
case *MatrixSelector:
|
||||||
return ev.matrixSelector(e)
|
return ev.MatrixSelector(e)
|
||||||
|
|
||||||
case *NumberLiteral:
|
case *NumberLiteral:
|
||||||
return scalar{v: e.Val, t: ev.Timestamp}
|
return scalar{v: e.Val, t: ev.Timestamp}
|
||||||
|
@ -815,13 +815,13 @@ func (ev *evaluator) VectorSelector(node *VectorSelector) Vector {
|
||||||
return vec
|
return vec
|
||||||
}
|
}
|
||||||
|
|
||||||
// matrixSelector evaluates a *MatrixSelector expression.
|
// MatrixSelector evaluates a *MatrixSelector expression.
|
||||||
func (ev *evaluator) matrixSelector(node *MatrixSelector) matrix {
|
func (ev *evaluator) MatrixSelector(node *MatrixSelector) Matrix {
|
||||||
var (
|
var (
|
||||||
offset = durationMilliseconds(node.Offset)
|
offset = durationMilliseconds(node.Offset)
|
||||||
maxt = ev.Timestamp - offset
|
maxt = ev.Timestamp - offset
|
||||||
mint = maxt - durationMilliseconds(node.Range)
|
mint = maxt - durationMilliseconds(node.Range)
|
||||||
matrix = make(matrix, 0, len(node.series))
|
Matrix = make(Matrix, 0, len(node.series))
|
||||||
)
|
)
|
||||||
|
|
||||||
for i, it := range node.iterators {
|
for i, it := range node.iterators {
|
||||||
|
@ -851,9 +851,9 @@ func (ev *evaluator) matrixSelector(node *MatrixSelector) matrix {
|
||||||
ss.Values = append(ss.Values, samplePair{t: t + offset, v: v})
|
ss.Values = append(ss.Values, samplePair{t: t + offset, v: v})
|
||||||
}
|
}
|
||||||
|
|
||||||
matrix = append(matrix, ss)
|
Matrix = append(Matrix, ss)
|
||||||
}
|
}
|
||||||
return matrix
|
return Matrix
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ev *evaluator) VectorAnd(lhs, rhs Vector, matching *VectorMatching) Vector {
|
func (ev *evaluator) VectorAnd(lhs, rhs Vector, matching *VectorMatching) Vector {
|
||||||
|
@ -1496,7 +1496,7 @@ func documentedType(t ValueType) string {
|
||||||
switch t {
|
switch t {
|
||||||
case "Vector":
|
case "Vector":
|
||||||
return "instant Vector"
|
return "instant Vector"
|
||||||
case "matrix":
|
case "Matrix":
|
||||||
return "range Vector"
|
return "range Vector"
|
||||||
default:
|
default:
|
||||||
return string(t)
|
return string(t)
|
||||||
|
|
|
@ -54,8 +54,8 @@ func extrapolatedRate(ev *evaluator, arg Expr, isCounter bool, isRate bool) Valu
|
||||||
|
|
||||||
resultVector := Vector{}
|
resultVector := Vector{}
|
||||||
|
|
||||||
matrixValue := ev.evalMatrix(ms)
|
MatrixValue := ev.evalMatrix(ms)
|
||||||
for _, samples := range matrixValue {
|
for _, samples := range MatrixValue {
|
||||||
// No sense in trying to compute a rate without at least two points. Drop
|
// No sense in trying to compute a rate without at least two points. Drop
|
||||||
// this Vector element.
|
// this Vector element.
|
||||||
if len(samples.Values) < 2 {
|
if len(samples.Values) < 2 {
|
||||||
|
@ -125,7 +125,7 @@ func extrapolatedRate(ev *evaluator, arg Expr, isCounter bool, isRate bool) Valu
|
||||||
return resultVector
|
return resultVector
|
||||||
}
|
}
|
||||||
|
|
||||||
// === delta(matrix ValueTypeMatrix) Vector ===
|
// === delta(Matrix ValueTypeMatrix) Vector ===
|
||||||
func funcDelta(ev *evaluator, args Expressions) Value {
|
func funcDelta(ev *evaluator, args Expressions) Value {
|
||||||
return extrapolatedRate(ev, args[0], false, false)
|
return extrapolatedRate(ev, args[0], false, false)
|
||||||
}
|
}
|
||||||
|
@ -425,7 +425,7 @@ func aggrOverTime(ev *evaluator, args Expressions, aggrFn func([]samplePair) flo
|
||||||
return resultVector
|
return resultVector
|
||||||
}
|
}
|
||||||
|
|
||||||
// === avg_over_time(matrix ValueTypeMatrix) Vector ===
|
// === avg_over_time(Matrix ValueTypeMatrix) Vector ===
|
||||||
func funcAvgOverTime(ev *evaluator, args Expressions) Value {
|
func funcAvgOverTime(ev *evaluator, args Expressions) Value {
|
||||||
return aggrOverTime(ev, args, func(values []samplePair) float64 {
|
return aggrOverTime(ev, args, func(values []samplePair) float64 {
|
||||||
var sum float64
|
var sum float64
|
||||||
|
@ -436,7 +436,7 @@ func funcAvgOverTime(ev *evaluator, args Expressions) Value {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// === count_over_time(matrix ValueTypeMatrix) Vector ===
|
// === count_over_time(Matrix ValueTypeMatrix) Vector ===
|
||||||
func funcCountOverTime(ev *evaluator, args Expressions) Value {
|
func funcCountOverTime(ev *evaluator, args Expressions) Value {
|
||||||
return aggrOverTime(ev, args, func(values []samplePair) float64 {
|
return aggrOverTime(ev, args, func(values []samplePair) float64 {
|
||||||
return float64(len(values))
|
return float64(len(values))
|
||||||
|
@ -453,7 +453,7 @@ func funcFloor(ev *evaluator, args Expressions) Value {
|
||||||
return Vector
|
return Vector
|
||||||
}
|
}
|
||||||
|
|
||||||
// === max_over_time(matrix ValueTypeMatrix) Vector ===
|
// === max_over_time(Matrix ValueTypeMatrix) Vector ===
|
||||||
func funcMaxOverTime(ev *evaluator, args Expressions) Value {
|
func funcMaxOverTime(ev *evaluator, args Expressions) Value {
|
||||||
return aggrOverTime(ev, args, func(values []samplePair) float64 {
|
return aggrOverTime(ev, args, func(values []samplePair) float64 {
|
||||||
max := math.Inf(-1)
|
max := math.Inf(-1)
|
||||||
|
@ -464,7 +464,7 @@ func funcMaxOverTime(ev *evaluator, args Expressions) Value {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// === min_over_time(matrix ValueTypeMatrix) Vector ===
|
// === min_over_time(Matrix ValueTypeMatrix) Vector ===
|
||||||
func funcMinOverTime(ev *evaluator, args Expressions) Value {
|
func funcMinOverTime(ev *evaluator, args Expressions) Value {
|
||||||
return aggrOverTime(ev, args, func(values []samplePair) float64 {
|
return aggrOverTime(ev, args, func(values []samplePair) float64 {
|
||||||
min := math.Inf(1)
|
min := math.Inf(1)
|
||||||
|
@ -475,7 +475,7 @@ func funcMinOverTime(ev *evaluator, args Expressions) Value {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// === sum_over_time(matrix ValueTypeMatrix) Vector ===
|
// === sum_over_time(Matrix ValueTypeMatrix) Vector ===
|
||||||
func funcSumOverTime(ev *evaluator, args Expressions) Value {
|
func funcSumOverTime(ev *evaluator, args Expressions) Value {
|
||||||
return aggrOverTime(ev, args, func(values []samplePair) float64 {
|
return aggrOverTime(ev, args, func(values []samplePair) float64 {
|
||||||
var sum float64
|
var sum float64
|
||||||
|
@ -486,7 +486,7 @@ func funcSumOverTime(ev *evaluator, args Expressions) Value {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// === quantile_over_time(matrix ValueTypeMatrix) Vector ===
|
// === quantile_over_time(Matrix ValueTypeMatrix) Vector ===
|
||||||
func funcQuantileOverTime(ev *evaluator, args Expressions) Value {
|
func funcQuantileOverTime(ev *evaluator, args Expressions) Value {
|
||||||
q := ev.evalFloat(args[0])
|
q := ev.evalFloat(args[0])
|
||||||
mat := ev.evalMatrix(args[1])
|
mat := ev.evalMatrix(args[1])
|
||||||
|
@ -511,7 +511,7 @@ func funcQuantileOverTime(ev *evaluator, args Expressions) Value {
|
||||||
return resultVector
|
return resultVector
|
||||||
}
|
}
|
||||||
|
|
||||||
// === stddev_over_time(matrix ValueTypeMatrix) Vector ===
|
// === stddev_over_time(Matrix ValueTypeMatrix) Vector ===
|
||||||
func funcStddevOverTime(ev *evaluator, args Expressions) Value {
|
func funcStddevOverTime(ev *evaluator, args Expressions) Value {
|
||||||
return aggrOverTime(ev, args, func(values []samplePair) float64 {
|
return aggrOverTime(ev, args, func(values []samplePair) float64 {
|
||||||
var sum, squaredSum, count float64
|
var sum, squaredSum, count float64
|
||||||
|
@ -525,7 +525,7 @@ func funcStddevOverTime(ev *evaluator, args Expressions) Value {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// === stdvar_over_time(matrix ValueTypeMatrix) Vector ===
|
// === stdvar_over_time(Matrix ValueTypeMatrix) Vector ===
|
||||||
func funcStdvarOverTime(ev *evaluator, args Expressions) Value {
|
func funcStdvarOverTime(ev *evaluator, args Expressions) Value {
|
||||||
return aggrOverTime(ev, args, func(values []samplePair) float64 {
|
return aggrOverTime(ev, args, func(values []samplePair) float64 {
|
||||||
var sum, squaredSum, count float64
|
var sum, squaredSum, count float64
|
||||||
|
@ -745,7 +745,7 @@ func funcHistogramQuantile(ev *evaluator, args Expressions) Value {
|
||||||
return outVec
|
return outVec
|
||||||
}
|
}
|
||||||
|
|
||||||
// === resets(matrix ValueTypeMatrix) Vector ===
|
// === resets(Matrix ValueTypeMatrix) Vector ===
|
||||||
func funcResets(ev *evaluator, args Expressions) Value {
|
func funcResets(ev *evaluator, args Expressions) Value {
|
||||||
in := ev.evalMatrix(args[0])
|
in := ev.evalMatrix(args[0])
|
||||||
out := make(Vector, 0, len(in))
|
out := make(Vector, 0, len(in))
|
||||||
|
@ -770,7 +770,7 @@ func funcResets(ev *evaluator, args Expressions) Value {
|
||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
// === changes(matrix ValueTypeMatrix) Vector ===
|
// === changes(Matrix ValueTypeMatrix) Vector ===
|
||||||
func funcChanges(ev *evaluator, args Expressions) Value {
|
func funcChanges(ev *evaluator, args Expressions) Value {
|
||||||
in := ev.evalMatrix(args[0])
|
in := ev.evalMatrix(args[0])
|
||||||
out := make(Vector, 0, len(in))
|
out := make(Vector, 0, len(in))
|
||||||
|
|
|
@ -531,7 +531,7 @@ func (p *parser) balance(lhs Expr, op itemType, rhs Expr, vecMatching *VectorMat
|
||||||
|
|
||||||
// unaryExpr parses a unary expression.
|
// unaryExpr parses a unary expression.
|
||||||
//
|
//
|
||||||
// <Vector_selector> | <matrix_selector> | (+|-) <number_literal> | '(' <expr> ')'
|
// <Vector_selector> | <Matrix_selector> | (+|-) <number_literal> | '(' <expr> ')'
|
||||||
//
|
//
|
||||||
func (p *parser) unaryExpr() Expr {
|
func (p *parser) unaryExpr() Expr {
|
||||||
switch t := p.peek(); t.typ {
|
switch t := p.peek(); t.typ {
|
||||||
|
@ -583,7 +583,7 @@ func (p *parser) unaryExpr() Expr {
|
||||||
return e
|
return e
|
||||||
}
|
}
|
||||||
|
|
||||||
// rangeSelector parses a matrix (a.k.a. range) selector based on a given
|
// rangeSelector parses a Matrix (a.k.a. range) selector based on a given
|
||||||
// Vector selector.
|
// Vector selector.
|
||||||
//
|
//
|
||||||
// <Vector_selector> '[' <duration> ']'
|
// <Vector_selector> '[' <duration> ']'
|
||||||
|
|
|
@ -351,7 +351,7 @@ func (ev *evalCmd) expect(pos int, m labels.Labels, vals ...sequenceValue) {
|
||||||
// compareResult compares the result value with the defined expectation.
|
// compareResult compares the result value with the defined expectation.
|
||||||
func (ev *evalCmd) compareResult(result Value) error {
|
func (ev *evalCmd) compareResult(result Value) error {
|
||||||
switch val := result.(type) {
|
switch val := result.(type) {
|
||||||
case matrix:
|
case Matrix:
|
||||||
if ev.instant {
|
if ev.instant {
|
||||||
return fmt.Errorf("received range result on instant evaluation")
|
return fmt.Errorf("received range result on instant evaluation")
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue