mirror of
https://github.com/grafana/grafana.git
synced 2025-08-01 03:01:51 +08:00
feat(alerting): doing schema changes
This commit is contained in:
@ -31,17 +31,22 @@ func (s AlertSeverityType) IsValid() bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Alert struct {
|
type Alert struct {
|
||||||
Id int64
|
Id int64
|
||||||
OrgId int64
|
OrgId int64
|
||||||
DashboardId int64
|
DashboardId int64
|
||||||
PanelId int64
|
PanelId int64
|
||||||
Name string
|
Name string
|
||||||
Message string
|
Message string
|
||||||
Severity AlertSeverityType
|
Severity AlertSeverityType
|
||||||
State AlertStateType
|
State AlertStateType
|
||||||
Handler int64
|
Handler int64
|
||||||
Enabled bool
|
Paused bool
|
||||||
Frequency int64
|
Silenced bool
|
||||||
|
ExecutionError string
|
||||||
|
Frequency int64
|
||||||
|
|
||||||
|
LastEvalData *simplejson.Json
|
||||||
|
LastEvalTime time.Time
|
||||||
|
|
||||||
CreatedBy int64
|
CreatedBy int64
|
||||||
UpdatedBy int64
|
UpdatedBy int64
|
||||||
|
@ -40,16 +40,16 @@ func (c *QueryCondition) Eval(context *alerting.EvalContext) {
|
|||||||
|
|
||||||
for _, series := range seriesList {
|
for _, series := range seriesList {
|
||||||
reducedValue := c.Reducer.Reduce(series)
|
reducedValue := c.Reducer.Reduce(series)
|
||||||
pass := c.Evaluator.Eval(series, reducedValue)
|
evalMatch := c.Evaluator.Eval(series, reducedValue)
|
||||||
|
|
||||||
if context.IsTestRun {
|
if context.IsTestRun {
|
||||||
context.Logs = append(context.Logs, &alerting.ResultLogEntry{
|
context.Logs = append(context.Logs, &alerting.ResultLogEntry{
|
||||||
Message: fmt.Sprintf("Condition[%d]: Eval: %v, Metric: %s, Value: %1.3f", c.Index, pass, series.Name, reducedValue),
|
Message: fmt.Sprintf("Condition[%d]: Eval: %v, Metric: %s, Value: %1.3f", c.Index, evalMatch, series.Name, reducedValue),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
if pass {
|
if evalMatch {
|
||||||
context.Events = append(context.Events, &alerting.Event{
|
context.EvalMatches = append(context.EvalMatches, &alerting.EvalMatch{
|
||||||
Metric: series.Name,
|
Metric: series.Name,
|
||||||
Value: reducedValue,
|
Value: reducedValue,
|
||||||
})
|
})
|
||||||
|
@ -13,7 +13,7 @@ import (
|
|||||||
type EvalContext struct {
|
type EvalContext struct {
|
||||||
Firing bool
|
Firing bool
|
||||||
IsTestRun bool
|
IsTestRun bool
|
||||||
Events []*Event
|
EvalMatches []*EvalMatch
|
||||||
Logs []*ResultLogEntry
|
Logs []*ResultLogEntry
|
||||||
Error error
|
Error error
|
||||||
Description string
|
Description string
|
||||||
@ -94,12 +94,12 @@ func (c *EvalContext) GetImageUrl() (string, error) {
|
|||||||
|
|
||||||
func NewEvalContext(rule *Rule) *EvalContext {
|
func NewEvalContext(rule *Rule) *EvalContext {
|
||||||
return &EvalContext{
|
return &EvalContext{
|
||||||
StartTime: time.Now(),
|
StartTime: time.Now(),
|
||||||
Rule: rule,
|
Rule: rule,
|
||||||
Logs: make([]*ResultLogEntry, 0),
|
Logs: make([]*ResultLogEntry, 0),
|
||||||
Events: make([]*Event, 0),
|
EvalMatches: make([]*EvalMatch, 0),
|
||||||
DoneChan: make(chan bool, 1),
|
DoneChan: make(chan bool, 1),
|
||||||
CancelChan: make(chan bool, 1),
|
CancelChan: make(chan bool, 1),
|
||||||
log: log.New("alerting.evalContext"),
|
log: log.New("alerting.evalContext"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -87,7 +87,6 @@ func (e *DashAlertExtractor) GetAlerts() ([]*m.Alert, error) {
|
|||||||
Id: jsonAlert.Get("id").MustInt64(),
|
Id: jsonAlert.Get("id").MustInt64(),
|
||||||
Name: jsonAlert.Get("name").MustString(),
|
Name: jsonAlert.Get("name").MustString(),
|
||||||
Handler: jsonAlert.Get("handler").MustInt64(),
|
Handler: jsonAlert.Get("handler").MustInt64(),
|
||||||
Enabled: jsonAlert.Get("enabled").MustBool(),
|
|
||||||
Message: jsonAlert.Get("message").MustString(),
|
Message: jsonAlert.Get("message").MustString(),
|
||||||
Severity: m.AlertSeverityType(jsonAlert.Get("severity").MustString()),
|
Severity: m.AlertSeverityType(jsonAlert.Get("severity").MustString()),
|
||||||
Frequency: getTimeDurationStringToSeconds(jsonAlert.Get("frequency").MustString()),
|
Frequency: getTimeDurationStringToSeconds(jsonAlert.Get("frequency").MustString()),
|
||||||
|
@ -12,10 +12,9 @@ type ResultLogEntry struct {
|
|||||||
Data interface{}
|
Data interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
type Event struct {
|
type EvalMatch struct {
|
||||||
Value float64
|
Value float64
|
||||||
Metric string
|
Metric string
|
||||||
State string
|
|
||||||
Tags map[string]string
|
Tags map[string]string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -59,7 +59,7 @@ func (this *EmailNotifier) Notify(context *alerting.EvalContext) {
|
|||||||
"RuleUrl": ruleUrl,
|
"RuleUrl": ruleUrl,
|
||||||
"ImageLink": context.ImagePublicUrl,
|
"ImageLink": context.ImagePublicUrl,
|
||||||
"AlertPageUrl": setting.AppUrl + "alerting",
|
"AlertPageUrl": setting.AppUrl + "alerting",
|
||||||
"Events": context.Events,
|
"EvalMatches": context.EvalMatches,
|
||||||
},
|
},
|
||||||
To: this.Addresses,
|
To: this.Addresses,
|
||||||
Template: "alert_notification.html",
|
Template: "alert_notification.html",
|
||||||
|
@ -50,7 +50,7 @@ func (this *SlackNotifier) Notify(context *alerting.EvalContext) {
|
|||||||
|
|
||||||
fields := make([]map[string]interface{}, 0)
|
fields := make([]map[string]interface{}, 0)
|
||||||
fieldLimitCount := 4
|
fieldLimitCount := 4
|
||||||
for index, evt := range context.Events {
|
for index, evt := range context.EvalMatches {
|
||||||
fields = append(fields, map[string]interface{}{
|
fields = append(fields, map[string]interface{}{
|
||||||
"title": evt.Metric,
|
"title": evt.Metric,
|
||||||
"value": evt.Value,
|
"value": evt.Value,
|
||||||
|
@ -20,28 +20,30 @@ func addAlertMigrations(mg *Migrator) {
|
|||||||
{Name: "frequency", Type: DB_BigInt, Nullable: false},
|
{Name: "frequency", Type: DB_BigInt, Nullable: false},
|
||||||
{Name: "handler", Type: DB_BigInt, Nullable: false},
|
{Name: "handler", Type: DB_BigInt, Nullable: false},
|
||||||
{Name: "severity", Type: DB_Text, Nullable: false},
|
{Name: "severity", Type: DB_Text, Nullable: false},
|
||||||
{Name: "enabled", Type: DB_Bool, Nullable: false},
|
{Name: "paused", Type: DB_Bool, Nullable: false},
|
||||||
|
{Name: "silenced", Type: DB_Bool, Nullable: false},
|
||||||
|
{Name: "execution_error", Type: DB_Text, Nullable: false},
|
||||||
|
{Name: "last_eval_data", Type: DB_Text, Nullable: false},
|
||||||
|
{Name: "last_eval_time", Type: DB_DateTime, Nullable: false},
|
||||||
{Name: "created", Type: DB_DateTime, Nullable: false},
|
{Name: "created", Type: DB_DateTime, Nullable: false},
|
||||||
{Name: "updated", Type: DB_DateTime, Nullable: false},
|
{Name: "updated", Type: DB_DateTime, Nullable: false},
|
||||||
{Name: "updated_by", Type: DB_BigInt, Nullable: false},
|
{Name: "updated_by", Type: DB_BigInt, Nullable: false},
|
||||||
{Name: "created_by", Type: DB_BigInt, Nullable: false},
|
{Name: "created_by", Type: DB_BigInt, Nullable: false},
|
||||||
},
|
},
|
||||||
|
Indices: []*Index{
|
||||||
|
{Cols: []string{"org_id", "id"}, Type: IndexType},
|
||||||
|
{Cols: []string{"state"}, Type: IndexType},
|
||||||
|
{Cols: []string{"dashboard_id"}, Type: IndexType},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
// create table
|
// create table
|
||||||
mg.AddMigration("create alert table v1", NewAddTableMigration(alertV1))
|
mg.AddMigration("create alert table v1", NewAddTableMigration(alertV1))
|
||||||
|
|
||||||
alert_heartbeat := Table{
|
// create indices
|
||||||
Name: "alert_heartbeat",
|
mg.AddMigration("add index alert org_id & id ", NewAddIndexMigration(alertV1, alertV1.Indices[0]))
|
||||||
Columns: []*Column{
|
mg.AddMigration("add index alert state", NewAddIndexMigration(alertV1, alertV1.Indices[1]))
|
||||||
{Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
|
mg.AddMigration("add index alert dashboard_id", NewAddIndexMigration(alertV1, alertV1.Indices[2]))
|
||||||
{Name: "server_id", Type: DB_NVarchar, Length: 50, Nullable: false},
|
|
||||||
{Name: "created", Type: DB_DateTime, Nullable: false},
|
|
||||||
{Name: "updated", Type: DB_DateTime, Nullable: false},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
mg.AddMigration("create alert_heartbeat table v1", NewAddTableMigration(alert_heartbeat))
|
|
||||||
|
|
||||||
alert_notification := Table{
|
alert_notification := Table{
|
||||||
Name: "alert_notification",
|
Name: "alert_notification",
|
||||||
@ -54,7 +56,12 @@ func addAlertMigrations(mg *Migrator) {
|
|||||||
{Name: "created", Type: DB_DateTime, Nullable: false},
|
{Name: "created", Type: DB_DateTime, Nullable: false},
|
||||||
{Name: "updated", Type: DB_DateTime, Nullable: false},
|
{Name: "updated", Type: DB_DateTime, Nullable: false},
|
||||||
},
|
},
|
||||||
|
Indices: []*Index{
|
||||||
|
{Cols: []string{"org_id", "name"}, Type: UniqueIndex},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
mg.AddMigration("create alert_notification table v1", NewAddTableMigration(alert_notification))
|
mg.AddMigration("create alert_notification table v1", NewAddTableMigration(alert_notification))
|
||||||
|
|
||||||
|
mg.AddMigration("add index alert_notification org_id & name", NewAddIndexMigration(alert_notification, alert_notification.Indices[0]))
|
||||||
}
|
}
|
||||||
|
18
pkg/services/sqlstore/migrations/heartbeat_mig.go
Normal file
18
pkg/services/sqlstore/migrations/heartbeat_mig.go
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
package migrations
|
||||||
|
|
||||||
|
// // create table
|
||||||
|
// mg.AddMigration("create alert table v1", NewAddTableMigration(alertV1))
|
||||||
|
//
|
||||||
|
// alert_heartbeat := Table{
|
||||||
|
// Name: "alert_heartbeat",
|
||||||
|
// Columns: []*Column{
|
||||||
|
// {Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
|
||||||
|
// {Name: "server_id", Type: DB_NVarchar, Length: 50, Nullable: false},
|
||||||
|
// {Name: "created", Type: DB_DateTime, Nullable: false},
|
||||||
|
// {Name: "updated", Type: DB_DateTime, Nullable: false},
|
||||||
|
// },
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// mg.AddMigration("create alert_heartbeat table v1", NewAddTableMigration(alert_heartbeat))
|
||||||
|
//
|
||||||
|
//
|
Reference in New Issue
Block a user