mirror of
https://github.com/grafana/grafana.git
synced 2024-12-30 10:47:30 -06:00
Alerting: fix a bug where the metric in the evaluation string was not correctly populated (#41731)
Instead of using the frame name as the series name, we now use the name of the series value column if the frame name is not set. Queries to some datasources may end up with an empty frame name, which leads to an empty series name. As the series name is used in the frontend to display which metrics were queried etc. it shouldn't be empty.
This commit is contained in:
parent
bb01f8c4cf
commit
2319c52c85
@ -118,6 +118,11 @@ FIELDS:
|
||||
frame.Fields = fields
|
||||
s.Frame = frame
|
||||
|
||||
// We use the frame name as series name if the frame name is set
|
||||
if s.Frame.Name != "" {
|
||||
s.Frame.Fields[seriesTypeValIdx].Name = s.Frame.Name
|
||||
}
|
||||
|
||||
return s, nil
|
||||
}
|
||||
|
||||
@ -142,7 +147,7 @@ func (s Series) GetLabels() data.Labels { return s.Frame.Fields[seriesTypeValIdx
|
||||
|
||||
func (s Series) SetLabels(ls data.Labels) { s.Frame.Fields[seriesTypeValIdx].Labels = ls }
|
||||
|
||||
func (s Series) GetName() string { return s.Frame.Name }
|
||||
func (s Series) GetName() string { return s.Frame.Fields[seriesTypeValIdx].Name }
|
||||
|
||||
func (s Series) GetMeta() interface{} {
|
||||
return s.Frame.Meta.Custom
|
||||
|
@ -6,6 +6,7 @@ import (
|
||||
|
||||
"github.com/grafana/grafana-plugin-sdk-go/data"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestSeriesSort(t *testing.T) {
|
||||
@ -76,7 +77,6 @@ func TestSeriesFromFrame(t *testing.T) {
|
||||
{
|
||||
name: "[]time, []float frame should convert",
|
||||
frame: &data.Frame{
|
||||
Name: "test",
|
||||
Fields: []*data.Field{
|
||||
data.NewField("time", nil, []time.Time{}),
|
||||
data.NewField("value", nil, []float64{}),
|
||||
@ -86,7 +86,6 @@ func TestSeriesFromFrame(t *testing.T) {
|
||||
Is: assert.Equal,
|
||||
Series: Series{
|
||||
Frame: &data.Frame{
|
||||
Name: "test",
|
||||
Fields: []*data.Field{
|
||||
data.NewField("time", nil, []time.Time{}),
|
||||
data.NewField("value", nil, []*float64{}),
|
||||
@ -97,7 +96,6 @@ func TestSeriesFromFrame(t *testing.T) {
|
||||
{
|
||||
name: "[]time, []*float frame should convert",
|
||||
frame: &data.Frame{
|
||||
Name: "test",
|
||||
Fields: []*data.Field{
|
||||
data.NewField("time", nil, []time.Time{time.Unix(5, 0)}),
|
||||
data.NewField("value", nil, []*float64{float64Pointer(5)}),
|
||||
@ -107,7 +105,6 @@ func TestSeriesFromFrame(t *testing.T) {
|
||||
Is: assert.Equal,
|
||||
Series: Series{
|
||||
Frame: &data.Frame{
|
||||
Name: "test",
|
||||
Fields: []*data.Field{
|
||||
data.NewField("time", nil, []time.Time{time.Unix(5, 0)}),
|
||||
data.NewField("value", nil, []*float64{float64Pointer(5)}),
|
||||
@ -118,7 +115,6 @@ func TestSeriesFromFrame(t *testing.T) {
|
||||
{
|
||||
name: "[]*float, []time frame should convert",
|
||||
frame: &data.Frame{
|
||||
Name: "test",
|
||||
Fields: []*data.Field{
|
||||
data.NewField("value", nil, []*float64{float64Pointer(5)}),
|
||||
data.NewField("time", nil, []time.Time{time.Unix(5, 0)}),
|
||||
@ -128,7 +124,6 @@ func TestSeriesFromFrame(t *testing.T) {
|
||||
Is: assert.Equal,
|
||||
Series: Series{
|
||||
Frame: &data.Frame{
|
||||
Name: "test",
|
||||
Fields: []*data.Field{
|
||||
data.NewField("time", nil, []time.Time{time.Unix(5, 0)}),
|
||||
data.NewField("value", nil, []*float64{float64Pointer(5)}),
|
||||
@ -139,7 +134,6 @@ func TestSeriesFromFrame(t *testing.T) {
|
||||
{
|
||||
name: "[]*int, []*time frame should convert",
|
||||
frame: &data.Frame{
|
||||
Name: "test",
|
||||
Fields: []*data.Field{
|
||||
data.NewField("time", nil, []*time.Time{unixTimePointer(5, 0)}),
|
||||
data.NewField("value", nil, []*int64{int64Pointer(5)}),
|
||||
@ -149,7 +143,6 @@ func TestSeriesFromFrame(t *testing.T) {
|
||||
Is: assert.Equal,
|
||||
Series: Series{
|
||||
Frame: &data.Frame{
|
||||
Name: "test",
|
||||
Fields: []*data.Field{
|
||||
data.NewField("time", nil, []time.Time{time.Unix(5, 0)}),
|
||||
data.NewField("value", nil, []*float64{float64Pointer(5)}),
|
||||
@ -160,7 +153,6 @@ func TestSeriesFromFrame(t *testing.T) {
|
||||
{
|
||||
name: "[]int, []*time frame should convert",
|
||||
frame: &data.Frame{
|
||||
Name: "test",
|
||||
Fields: []*data.Field{
|
||||
data.NewField("time", nil, []*time.Time{unixTimePointer(5, 0)}),
|
||||
data.NewField("value", nil, []int64{5}),
|
||||
@ -170,7 +162,6 @@ func TestSeriesFromFrame(t *testing.T) {
|
||||
Is: assert.Equal,
|
||||
Series: Series{
|
||||
Frame: &data.Frame{
|
||||
Name: "test",
|
||||
Fields: []*data.Field{
|
||||
data.NewField("time", nil, []time.Time{time.Unix(5, 0)}),
|
||||
data.NewField("value", nil, []*float64{float64Pointer(5)}),
|
||||
@ -181,7 +172,6 @@ func TestSeriesFromFrame(t *testing.T) {
|
||||
{
|
||||
name: "[]string, []*time frame should convert",
|
||||
frame: &data.Frame{
|
||||
Name: "test",
|
||||
Fields: []*data.Field{
|
||||
data.NewField("time", nil, []*time.Time{unixTimePointer(5, 0)}),
|
||||
data.NewField("value", nil, []string{"5"}),
|
||||
@ -191,7 +181,6 @@ func TestSeriesFromFrame(t *testing.T) {
|
||||
Is: assert.Equal,
|
||||
Series: Series{
|
||||
Frame: &data.Frame{
|
||||
Name: "test",
|
||||
Fields: []*data.Field{
|
||||
data.NewField("time", nil, []time.Time{time.Unix(5, 0)}),
|
||||
data.NewField("value", nil, []*float64{float64Pointer(5)}),
|
||||
@ -202,7 +191,6 @@ func TestSeriesFromFrame(t *testing.T) {
|
||||
{
|
||||
name: "[]*string, []*time frame should convert",
|
||||
frame: &data.Frame{
|
||||
Name: "test",
|
||||
Fields: []*data.Field{
|
||||
data.NewField("time", nil, []*time.Time{unixTimePointer(5, 0)}),
|
||||
data.NewField("value", nil, []*string{strPointer("5")}),
|
||||
@ -212,7 +200,6 @@ func TestSeriesFromFrame(t *testing.T) {
|
||||
Is: assert.Equal,
|
||||
Series: Series{
|
||||
Frame: &data.Frame{
|
||||
Name: "test",
|
||||
Fields: []*data.Field{
|
||||
data.NewField("time", nil, []time.Time{time.Unix(5, 0)}),
|
||||
data.NewField("value", nil, []*float64{float64Pointer(5)}),
|
||||
@ -223,7 +210,6 @@ func TestSeriesFromFrame(t *testing.T) {
|
||||
{
|
||||
name: "[]bool, []*time frame should convert",
|
||||
frame: &data.Frame{
|
||||
Name: "test",
|
||||
Fields: []*data.Field{
|
||||
data.NewField("time", nil, []*time.Time{unixTimePointer(5, 0)}),
|
||||
data.NewField("value", nil, []bool{true}),
|
||||
@ -233,7 +219,6 @@ func TestSeriesFromFrame(t *testing.T) {
|
||||
Is: assert.Equal,
|
||||
Series: Series{
|
||||
Frame: &data.Frame{
|
||||
Name: "test",
|
||||
Fields: []*data.Field{
|
||||
data.NewField("time", nil, []time.Time{time.Unix(5, 0)}),
|
||||
data.NewField("value", nil, []*float64{float64Pointer(1)}),
|
||||
@ -244,7 +229,6 @@ func TestSeriesFromFrame(t *testing.T) {
|
||||
{
|
||||
name: "[]*bool, []*time frame should convert",
|
||||
frame: &data.Frame{
|
||||
Name: "test",
|
||||
Fields: []*data.Field{
|
||||
data.NewField("time", nil, []*time.Time{unixTimePointer(5, 0)}),
|
||||
data.NewField("value", nil, []*bool{boolPointer(true)}),
|
||||
@ -254,7 +238,6 @@ func TestSeriesFromFrame(t *testing.T) {
|
||||
Is: assert.Equal,
|
||||
Series: Series{
|
||||
Frame: &data.Frame{
|
||||
Name: "test",
|
||||
Fields: []*data.Field{
|
||||
data.NewField("time", nil, []time.Time{time.Unix(5, 0)}),
|
||||
data.NewField("value", nil, []*float64{float64Pointer(1)}),
|
||||
@ -265,7 +248,6 @@ func TestSeriesFromFrame(t *testing.T) {
|
||||
{
|
||||
name: "[]*time, []*time frame should error",
|
||||
frame: &data.Frame{
|
||||
Name: "test",
|
||||
Fields: []*data.Field{
|
||||
data.NewField("time", nil, []*time.Time{}),
|
||||
data.NewField("time", nil, []*time.Time{}),
|
||||
@ -276,7 +258,6 @@ func TestSeriesFromFrame(t *testing.T) {
|
||||
{
|
||||
name: "[]*float64, []float64 frame should error",
|
||||
frame: &data.Frame{
|
||||
Name: "test",
|
||||
Fields: []*data.Field{
|
||||
data.NewField("value", nil, []*float64{}),
|
||||
data.NewField("value", nil, []*float64{}),
|
||||
@ -287,7 +268,6 @@ func TestSeriesFromFrame(t *testing.T) {
|
||||
{
|
||||
name: "[]*float64 frame should error",
|
||||
frame: &data.Frame{
|
||||
Name: "test",
|
||||
Fields: []*data.Field{
|
||||
data.NewField("value", nil, []*float64{}),
|
||||
},
|
||||
@ -305,3 +285,41 @@ func TestSeriesFromFrame(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSeriesName(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
frame *data.Frame
|
||||
expectedSeriesName string
|
||||
}{
|
||||
{
|
||||
name: "when the frame got a name this name should be used",
|
||||
frame: &data.Frame{
|
||||
Name: "test",
|
||||
Fields: []*data.Field{
|
||||
data.NewField("time", nil, []time.Time{}),
|
||||
data.NewField("value", nil, []float64{}),
|
||||
},
|
||||
},
|
||||
expectedSeriesName: "test",
|
||||
},
|
||||
{
|
||||
name: "when a frame got no name the name of the value column should be used",
|
||||
frame: &data.Frame{
|
||||
Name: "",
|
||||
Fields: []*data.Field{
|
||||
data.NewField("time", nil, []time.Time{}),
|
||||
data.NewField("value", nil, []float64{}),
|
||||
},
|
||||
},
|
||||
expectedSeriesName: "value",
|
||||
},
|
||||
}
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
s, err := SeriesFromFrame(test.frame)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, test.expectedSeriesName, s.GetName())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user