SSE/Alerting: Support prom instant vector responses (#44865)
* SSE/Alerting: (Draft) Support prom instant vector responses fixes #35663 * reduce\classic expressions to handle mathexp.Number * use Notice for warning Co-authored-by: Yuriy Tseretyan <yuriy.tseretyan@grafana.com>
This commit is contained in:
co-authored by
Yuriy Tseretyan
parent
0215195e6d
commit
01ef899753
@@ -7,6 +7,7 @@ import (
|
||||
"strconv"
|
||||
|
||||
"github.com/grafana/grafana-plugin-sdk-go/data"
|
||||
|
||||
"github.com/grafana/grafana/pkg/expr/mathexp"
|
||||
)
|
||||
|
||||
@@ -99,13 +100,21 @@ func (ccc *ConditionsCmd) Execute(ctx context.Context, vars mathexp.Vars) (mathe
|
||||
nilReducedCount := 0
|
||||
firingCount := 0
|
||||
for _, val := range querySeriesSet.Values {
|
||||
series, ok := val.(mathexp.Series)
|
||||
if !ok {
|
||||
var reducedNum mathexp.Number
|
||||
var name string
|
||||
switch v := val.(type) {
|
||||
case mathexp.Series:
|
||||
reducedNum = c.Reducer.Reduce(v)
|
||||
name = v.GetName()
|
||||
case mathexp.Number:
|
||||
reducedNum = v
|
||||
if len(v.Frame.Fields) > 0 {
|
||||
name = v.Frame.Fields[0].Name
|
||||
}
|
||||
default:
|
||||
return newRes, fmt.Errorf("can only reduce type series, got type %v", val.Type())
|
||||
}
|
||||
|
||||
reducedNum := c.Reducer.Reduce(series)
|
||||
|
||||
// TODO handle error / no data signals
|
||||
thisCondNoDataFound := reducedNum.GetFloat64Value() == nil
|
||||
|
||||
@@ -118,7 +127,7 @@ func (ccc *ConditionsCmd) Execute(ctx context.Context, vars mathexp.Vars) (mathe
|
||||
if evalRes {
|
||||
match := EvalMatch{
|
||||
Value: reducedNum.GetFloat64Value(),
|
||||
Metric: series.GetName(),
|
||||
Metric: name,
|
||||
}
|
||||
if reducedNum.GetLabels() != nil {
|
||||
match.Labels = reducedNum.GetLabels().Copy()
|
||||
|
||||
@@ -6,9 +6,10 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/grafana/grafana-plugin-sdk-go/data"
|
||||
"github.com/grafana/grafana/pkg/expr/mathexp"
|
||||
"github.com/stretchr/testify/require"
|
||||
ptr "github.com/xorcare/pointer"
|
||||
|
||||
"github.com/grafana/grafana/pkg/expr/mathexp"
|
||||
)
|
||||
|
||||
func TestUnmarshalConditionCMD(t *testing.T) {
|
||||
@@ -346,6 +347,37 @@ func TestConditionsCmdExecute(t *testing.T) {
|
||||
return v
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "should accept numbers",
|
||||
vars: mathexp.Vars{
|
||||
"A": mathexp.Results{
|
||||
Values: []mathexp.Value{
|
||||
valBasedNumber(ptr.Float64(5)),
|
||||
valBasedNumber(ptr.Float64(10)),
|
||||
valBasedNumber(ptr.Float64(15)),
|
||||
},
|
||||
},
|
||||
},
|
||||
conditionsCmd: &ConditionsCmd{
|
||||
Conditions: []condition{
|
||||
{
|
||||
QueryRefID: "A",
|
||||
Reducer: classicReducer("avg"),
|
||||
Operator: "and",
|
||||
Evaluator: &thresholdEvaluator{"gt", 1},
|
||||
},
|
||||
},
|
||||
},
|
||||
resultNumber: func() mathexp.Number {
|
||||
v := valBasedNumber(ptr.Float64(1))
|
||||
v.SetMeta([]EvalMatch{
|
||||
{Value: ptr.Float64(5)},
|
||||
{Value: ptr.Float64(10)},
|
||||
{Value: ptr.Float64(15)},
|
||||
})
|
||||
return v
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
|
||||
Reference in New Issue
Block a user