AlertingNG: Add For+Annotations to Grafana_Alert (#32793)

* add db columns
* Fix deserialisation issue of AlertRule For field (#32848)
* Update to latest alerting-api

Co-authored-by: Sofia Papagiannaki <papagian@users.noreply.github.com>
This commit is contained in:
Kyle Brandt
2021-04-09 10:50:04 -04:00
committed by GitHub
parent fa45fc1833
commit 80dfa83380
8 changed files with 64 additions and 13 deletions

View File

@@ -211,6 +211,8 @@ func toGettableExtendedRuleNode(r ngmodels.AlertRule) apimodels.GettableExtended
RuleGroup: r.RuleGroup,
NoDataState: apimodels.NoDataState(r.NoDataState),
ExecErrState: apimodels.ExecutionErrorState(r.ExecErrState),
For: r.For,
Annotations: r.Annotations,
},
}
}
@@ -225,6 +227,8 @@ func toPostableExtendedRuleNode(r ngmodels.AlertRule) apimodels.PostableExtended
UID: r.UID,
NoDataState: apimodels.NoDataState(r.NoDataState),
ExecErrState: apimodels.ExecutionErrorState(r.ExecErrState),
For: r.For,
Annotations: r.Annotations,
},
}
}

View File

@@ -158,23 +158,26 @@ func (api *API) ruleGroupByOldID(c *models.ReqContext) response.Response {
return response.Error(400, "unable to translate nodata/exec error settings",
fmt.Errorf("unable to translate nodata/exec error settings for alert id %v: %w", id, err))
}
// TODO: What to do with Rule Tags
// ruleTags := map[string]string{}
// for k, v := range oldAlert.Settings.Get("alertRuleTags").MustMap() {
// sV, ok := v.(string)
// if !ok {
// return response.Error(400, "unable to unmarshal rule tags",
// fmt.Errorf("unexpected type %T for tag %v", v, k))
// }
// ruleTags[k] = sV
// }
// TODO: Need place to put FOR duration
ruleTags := map[string]string{}
for k, v := range oldAlert.Settings.Get("alertRuleTags").MustMap() {
sV, ok := v.(string)
if !ok {
return response.Error(400, "unable to unmarshal rule tags",
fmt.Errorf("unexpected type %T for tag %v", v, k))
}
ruleTags[k] = sV
}
rule := ngmodels.AlertRule{
Title: oldAlert.Name,
Data: sseCond.Data,
Condition: sseCond.Condition,
NoDataState: *noDataSetting,
ExecErrState: *execErrSetting,
For: ngmodels.Duration(oldAlert.For),
Annotations: ruleTags,
}
rgc := apimodels.PostableRuleGroupConfig{
// TODO? Generate new name on conflict?
@@ -204,6 +207,7 @@ func (api *API) ruleGroupByOldID(c *models.ReqContext) response.Response {
}
return response.JSON(200, cmd)
}
func transAdjustInterval(freq int64) model.Duration {
// 10 corresponds to the SchedulerCfg, but TODO not worrying about fetching for now.
var baseFreq int64 = 10
@@ -212,6 +216,7 @@ func transAdjustInterval(freq int64) model.Duration {
}
return model.Duration(time.Duration((freq - (freq % baseFreq))) * time.Second)
}
func transGetAlertById(id int64, user models.SignedInUser) (*models.Alert, int, error) {
getAlert := &models.GetAlertByIdQuery{
Id: id,
@@ -224,6 +229,7 @@ func transGetAlertById(id int64, user models.SignedInUser) (*models.Alert, int,
}
return getAlert.Result, 0, nil
}
func transGetAlertsDashById(dashboardId int64, user models.SignedInUser) (*models.Dashboard, int, error) {
getDash := &models.GetDashboardQuery{
Id: dashboardId,
@@ -234,6 +240,7 @@ func transGetAlertsDashById(dashboardId int64, user models.SignedInUser) (*model
}
return getDash.Result, 0, nil
}
func transToSSECondition(m *models.Alert, user models.SignedInUser) (*ngmodels.Condition, error) {
sb, err := m.Settings.ToDB()
if err != nil {
@@ -245,6 +252,7 @@ func transToSSECondition(m *models.Alert, user models.SignedInUser) (*ngmodels.C
}
return evalCond, nil
}
func transNoDataExecSettings(m *models.Alert, user models.SignedInUser) (*ngmodels.NoDataState, *ngmodels.ExecutionErrorState, error) {
oldNoData := m.Settings.Get("noDataState").MustString()
noDataSetting, err := transNoData(oldNoData)
@@ -258,6 +266,7 @@ func transNoDataExecSettings(m *models.Alert, user models.SignedInUser) (*ngmode
}
return &noDataSetting, &execErrSetting, nil
}
func transNoData(s string) (ngmodels.NoDataState, error) {
switch s {
case "ok":
@@ -271,6 +280,7 @@ func transNoData(s string) (ngmodels.NoDataState, error) {
}
return ngmodels.NoData, fmt.Errorf("unrecognized No Data setting %v", s)
}
func transExecErr(s string) (ngmodels.ExecutionErrorState, error) {
switch s {
case "alerting":

View File

@@ -6,6 +6,10 @@
"grafana_alert": {
"title": "prom query with SSE",
"condition": "condition",
"for": 5,
"annotations": {
"foo": "bar"
},
"data": [
{
"refId": "query",

View File

@@ -52,6 +52,8 @@ type AlertRule struct {
RuleGroup string
NoDataState NoDataState
ExecErrState ExecutionErrorState
For Duration
Annotations map[string]string
}
// AlertRuleKey is the alert definition identifier
@@ -100,6 +102,8 @@ type AlertRuleVersion struct {
IntervalSeconds int64
NoDataState NoDataState
ExecErrState ExecutionErrorState
For Duration
Annotations map[string]string
}
// GetAlertRuleByUIDQuery is the query for retrieving/deleting an alert rule by UID and organisation ID.

View File

@@ -211,6 +211,9 @@ func (st DBstore) UpsertAlertRules(rules []UpsertRule) error {
r.New.RuleGroup = r.Existing.RuleGroup
r.New.Version = r.Existing.Version + 1
r.New.For = r.Existing.For
r.New.Annotations = r.Existing.Annotations
if err := st.ValidateAlertRule(r.New, true); err != nil {
return err
}
@@ -241,6 +244,8 @@ func (st DBstore) UpsertAlertRules(rules []UpsertRule) error {
IntervalSeconds: r.New.IntervalSeconds,
NoDataState: r.New.NoDataState,
ExecErrState: r.New.ExecErrState,
For: r.New.For,
Annotations: r.New.Annotations,
})
}
@@ -422,6 +427,8 @@ func (st DBstore) UpdateRuleGroup(cmd UpdateRuleGroupCmd) error {
IntervalSeconds: int64(time.Duration(cmd.RuleGroupConfig.Interval).Seconds()),
NamespaceUID: cmd.NamespaceUID,
RuleGroup: ruleGroup,
For: r.GrafanaManagedAlert.For,
Annotations: r.GrafanaManagedAlert.Annotations,
NoDataState: ngmodels.NoDataState(r.GrafanaManagedAlert.NoDataState),
ExecErrState: ngmodels.ExecutionErrorState(r.GrafanaManagedAlert.ExecErrState),
},

View File

@@ -147,6 +147,12 @@ func AddAlertRuleMigrations(mg *migrator.Migrator, defaultIntervalSeconds int64)
mg.AddMigration("alter alert_rule table data column to mediumtext in mysql", migrator.NewRawSQLMigration("").
Mysql("ALTER TABLE alert_rule MODIFY data MEDIUMTEXT;"))
// add for column
mg.AddMigration("add column for to alert_rule", migrator.NewAddColumnMigration(alertRule, &migrator.Column{Name: "for", Type: migrator.DB_BigInt, Nullable: false, Default: "0"}))
// add annotations column
mg.AddMigration("add column annotations to alert_rule", migrator.NewAddColumnMigration(alertRule, &migrator.Column{Name: "annotations", Type: migrator.DB_Text, Nullable: true}))
}
func AddAlertRuleVersionMigrations(mg *migrator.Migrator) {
@@ -181,4 +187,10 @@ func AddAlertRuleVersionMigrations(mg *migrator.Migrator) {
mg.AddMigration("alter alert_rule_version table data column to mediumtext in mysql", migrator.NewRawSQLMigration("").
Mysql("ALTER TABLE alert_rule_version MODIFY data MEDIUMTEXT;"))
// add for column
mg.AddMigration("add column for to alert_rule_version", migrator.NewAddColumnMigration(alertRuleVersion, &migrator.Column{Name: "for", Type: migrator.DB_BigInt, Nullable: false, Default: "0"}))
// add annotations column
mg.AddMigration("add column annotations to alert_rule_version", migrator.NewAddColumnMigration(alertRuleVersion, &migrator.Column{Name: "annotations", Type: migrator.DB_Text, Nullable: true}))
}