Alerting: Copy frame field labels to time series tags (#29886)

* Alerting: Copy frame field labels to time series tags

* keep comment

* add test case
This commit is contained in:
Will Browne 2020-12-16 18:07:45 +01:00 committed by GitHub
parent eee4f92435
commit af47d28499
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 2 deletions

View File

@ -330,13 +330,16 @@ func FrameToSeriesSlice(frame *data.Frame) (tsdb.TimeSeriesSlice, error) {
Points: make(tsdb.TimeSeriesPoints, field.Len()), Points: make(tsdb.TimeSeriesPoints, field.Len()),
} }
if len(field.Labels) > 0 {
ts.Tags = field.Labels.Copy()
}
switch { switch {
case field.Config != nil && field.Config.DisplayName != "": case field.Config != nil && field.Config.DisplayName != "":
ts.Name = field.Config.DisplayName ts.Name = field.Config.DisplayName
case field.Config != nil && field.Config.DisplayNameFromDS != "": case field.Config != nil && field.Config.DisplayNameFromDS != "":
ts.Name = field.Config.DisplayNameFromDS ts.Name = field.Config.DisplayNameFromDS
case len(field.Labels) > 0: case len(field.Labels) > 0:
ts.Tags = field.Labels.Copy()
// Tags are appended to the name so they are eventually included in EvalMatch's Metric property // Tags are appended to the name so they are eventually included in EvalMatch's Metric property
// for display in notifications. // for display in notifications.
ts.Name = fmt.Sprintf("%v {%v}", field.Name, field.Labels.String()) ts.Name = fmt.Sprintf("%v {%v}", field.Name, field.Labels.String())

View File

@ -324,7 +324,7 @@ func TestFrameToSeriesSlice(t *testing.T) {
name: "display name from data source", name: "display name from data source",
frame: data.NewFrame("", frame: data.NewFrame("",
data.NewField("Time", data.Labels{}, []time.Time{}), data.NewField("Time", data.Labels{}, []time.Time{}),
data.NewField(`Values`, data.Labels{}, []*int64{}).SetConfig(&data.FieldConfig{ data.NewField(`Values`, data.Labels{"Rating": "10"}, []*int64{}).SetConfig(&data.FieldConfig{
DisplayNameFromDS: "sloth", DisplayNameFromDS: "sloth",
})), })),
@ -332,6 +332,7 @@ func TestFrameToSeriesSlice(t *testing.T) {
&tsdb.TimeSeries{ &tsdb.TimeSeries{
Name: "sloth", Name: "sloth",
Points: tsdb.TimeSeriesPoints{}, Points: tsdb.TimeSeriesPoints{},
Tags: map[string]string{"Rating": "10"},
}, },
}, },
Err: require.NoError, Err: require.NoError,