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:
Kyle Brandt
2022-05-23 10:08:14 -04:00
committed by GitHub
parent 0215195e6d
commit 01ef899753
10 changed files with 245 additions and 19 deletions

View File

@@ -100,6 +100,11 @@ func GetReduceFunc(rFunc string) (ReducerFunc, error) {
}
}
// GetSupportedReduceFuncs returns collection of supported function names
func GetSupportedReduceFuncs() []string {
return []string{"sum", "mean", "min", "max", "count", "last"}
}
// Reduce turns the Series into a Number based on the given reduction function
// if ReduceMapper is defined it applies it to the provided series and performs reduction of the resulting series.
// Otherwise, the reduction operation is done against the original series.

View File

@@ -0,0 +1,18 @@
package mathexp
import (
"math/rand"
"github.com/grafana/grafana/pkg/util"
)
func GenerateNumber(value *float64) Number {
size := rand.Intn(5)
labels := make(map[string]string, size)
for i := 0; i < size; i++ {
labels[util.GenerateShortUID()] = util.GenerateShortUID()
}
result := NewNumber(util.GenerateShortUID(), labels)
result.SetValue(value)
return result
}

View File

@@ -6,6 +6,7 @@ import (
"time"
"github.com/grafana/grafana-plugin-sdk-go/data"
"github.com/grafana/grafana/pkg/expr/mathexp/parse"
)
@@ -154,7 +155,21 @@ func (s Series) GetMeta() interface{} {
}
func (s Series) SetMeta(v interface{}) {
s.Frame.SetMeta(&data.FrameMeta{Custom: v})
m := s.Frame.Meta
if m == nil {
m = &data.FrameMeta{}
s.Frame.SetMeta(m)
}
m.Custom = v
}
func (s Series) AddNotice(notice data.Notice) {
m := s.Frame.Meta
if m == nil {
m = &data.FrameMeta{}
s.Frame.SetMeta(m)
}
m.Notices = append(m.Notices, notice)
}
// AsDataFrame returns the underlying *data.Frame.

View File

@@ -2,6 +2,7 @@ package mathexp
import (
"github.com/grafana/grafana-plugin-sdk-go/data"
"github.com/grafana/grafana/pkg/expr/mathexp/parse"
)
@@ -33,6 +34,7 @@ type Value interface {
GetMeta() interface{}
SetMeta(interface{})
AsDataFrame() *data.Frame
AddNotice(notice data.Notice)
}
// Scalar is the type that holds a single number constant.
@@ -55,7 +57,21 @@ func (s Scalar) GetMeta() interface{} {
}
func (s Scalar) SetMeta(v interface{}) {
s.Frame.SetMeta(&data.FrameMeta{Custom: v})
m := s.Frame.Meta
if m == nil {
m = &data.FrameMeta{}
s.Frame.SetMeta(m)
}
m.Custom = v
}
func (s Scalar) AddNotice(notice data.Notice) {
m := s.Frame.Meta
if m == nil {
m = &data.FrameMeta{}
s.Frame.SetMeta(m)
}
m.Notices = append(m.Notices, notice)
}
// AsDataFrame returns the underlying *data.Frame.
@@ -121,7 +137,21 @@ func (n Number) GetMeta() interface{} {
}
func (n Number) SetMeta(v interface{}) {
n.Frame.SetMeta(&data.FrameMeta{Custom: v})
m := n.Frame.Meta
if m == nil {
m = &data.FrameMeta{}
n.Frame.SetMeta(m)
}
m.Custom = v
}
func (n Number) AddNotice(notice data.Notice) {
m := n.Frame.Meta
if m == nil {
m = &data.FrameMeta{}
n.Frame.SetMeta(m)
}
m.Notices = append(m.Notices, notice)
}
// FloatField is a *float64 or a float64 data.Field with methods to always