Merge branch 'new-logger' into alerting_definitions

Conflicts:
	pkg/api/api.go
	pkg/setting/setting.go
This commit is contained in:
Torkel Ödegaard
2016-06-07 13:31:56 +02:00
29 changed files with 617 additions and 783 deletions
-2
View File
@@ -19,8 +19,6 @@ func Init() {
return
}
log.Info("Alerting: Initializing alerting engine...")
engine = NewEngine()
engine.Start()
+24 -8
View File
@@ -5,7 +5,9 @@ import (
"time"
"github.com/benbjohnson/clock"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/log"
m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/alerting/alertstates"
)
@@ -17,6 +19,7 @@ type Engine struct {
scheduler Scheduler
executor Executor
ruleReader RuleReader
log log.Logger
}
func NewEngine() *Engine {
@@ -25,15 +28,16 @@ func NewEngine() *Engine {
execQueue: make(chan *AlertJob, 1000),
resultQueue: make(chan *AlertResult, 1000),
scheduler: NewScheduler(),
executor: &ExecutorImpl{},
executor: NewExecutor(),
ruleReader: NewRuleReader(),
log: log.New("alerting.engine"),
}
return e
}
func (e *Engine) Start() {
log.Info("Alerting: engine.Start()")
e.log.Info("Starting Alerting Engine")
go e.alertingTicker()
go e.execDispatch()
@@ -84,17 +88,18 @@ func (e *Engine) executeJob(job *AlertJob) {
Error: fmt.Errorf("Timeout"),
AlertJob: job,
}
log.Trace("Alerting: engine.executeJob(): timeout")
e.log.Debug("Job Execution timeout", "alertRuleId", job.Rule.Id)
case result := <-resultChan:
result.Duration = float64(time.Since(now).Nanoseconds()) / float64(1000000)
log.Trace("Alerting: engine.executeJob(): done %vms", result.Duration)
e.log.Debug("Job Execution done", "time_taken", result.Duration, "ruleId", job.Rule.Id)
e.resultQueue <- result
}
}
func (e *Engine) resultHandler() {
for result := range e.resultQueue {
log.Debug("Alerting: engine.resultHandler(): alert(%d) status(%s) actual(%v) retry(%d)", result.AlertJob.Rule.Id, result.State, result.ActualValue, result.AlertJob.RetryCount)
e.log.Debug("Alert Rule Result", "ruleId", result.AlertJob.Rule.Id, "state", result.State, "value", result.ActualValue, "retry", result.AlertJob.RetryCount)
result.AlertJob.Running = false
@@ -103,11 +108,10 @@ func (e *Engine) resultHandler() {
result.AlertJob.RetryCount++
if result.AlertJob.RetryCount < maxRetries {
log.Error(3, "Alerting: Rule('%s') Result Error: %v, Retrying..", result.AlertJob.Rule.Name, result.Error)
e.log.Error("Alert Rule Result Error", "ruleId", result.AlertJob.Rule.Id, "error", result.Error, "retry", result.AlertJob.RetryCount)
e.execQueue <- result.AlertJob
} else {
log.Error(3, "Alerting: Rule('%s') Result Error: %v, Max retries reached", result.AlertJob.Rule.Name, result.Error)
e.log.Error("Alert Rule Result Error After Max Retries", "ruleId", result.AlertJob.Rule.Id, "error", result.Error, "retry", result.AlertJob.RetryCount)
result.State = alertstates.Critical
result.Description = fmt.Sprintf("Failed to run check after %d retires, Error: %v", maxRetries, result.Error)
@@ -119,3 +123,15 @@ func (e *Engine) resultHandler() {
}
}
}
func (e *Engine) saveState(result *AlertResult) {
cmd := &m.UpdateAlertStateCommand{
AlertId: result.AlertJob.Rule.Id,
NewState: result.State,
Info: result.Description,
}
if err := bus.Dispatch(cmd); err != nil {
e.log.Error("Failed to save state", "error", err)
}
}
+9 -2
View File
@@ -19,6 +19,13 @@ var (
)
type ExecutorImpl struct {
log log.Logger
}
func NewExecutor() *ExecutorImpl {
return &ExecutorImpl{
log: log.New("alerting.executor"),
}
}
type compareFn func(float64, float64) bool
@@ -147,10 +154,10 @@ func (e *ExecutorImpl) GetRequestForAlertRule(rule *AlertRule, datasource *m.Dat
}
func (e *ExecutorImpl) evaluateRule(rule *AlertRule, series tsdb.TimeSeriesSlice) *AlertResult {
log.Trace("Alerting: executor.evaluateRule: %v, query result: series: %v", rule.Name, len(series))
e.log.Debug("Evaluating Alerting Rule", "seriesCount", len(series), "ruleName", rule.Name)
for _, serie := range series {
log.Info("Alerting: executor.validate: %v", serie.Name)
log.Debug("Evaluating series", "series", serie.Name)
if aggregator[rule.Aggregator] == nil {
continue
+9 -18
View File
@@ -11,11 +11,10 @@ import (
)
type Migrator struct {
LogLevel log.LogLevel
x *xorm.Engine
dialect Dialect
migrations []Migration
Logger log.Logger
}
type MigrationLog struct {
@@ -30,7 +29,7 @@ type MigrationLog struct {
func NewMigrator(engine *xorm.Engine) *Migrator {
mg := &Migrator{}
mg.x = engine
mg.LogLevel = log.WARN
mg.Logger = log.New("migrator")
mg.migrations = make([]Migration, 0)
mg.dialect = NewDialect(mg.x.DriverName())
return mg
@@ -69,9 +68,7 @@ func (mg *Migrator) GetMigrationLog() (map[string]MigrationLog, error) {
}
func (mg *Migrator) Start() error {
if mg.LogLevel <= log.INFO {
log.Info("Migrator: Starting DB migration")
}
mg.Logger.Info("Starting DB migration")
logMap, err := mg.GetMigrationLog()
if err != nil {
@@ -81,9 +78,7 @@ func (mg *Migrator) Start() error {
for _, m := range mg.migrations {
_, exists := logMap[m.Id()]
if exists {
if mg.LogLevel <= log.DEBUG {
log.Debug("Migrator: Skipping migration: %v, Already executed", m.Id())
}
mg.Logger.Debug("Skipping migration: Already executed", "id", m.Id())
continue
}
@@ -95,12 +90,10 @@ func (mg *Migrator) Start() error {
Timestamp: time.Now(),
}
if mg.LogLevel <= log.DEBUG {
log.Debug("Migrator: Executing SQL: \n %v \n", sql)
}
mg.Logger.Debug("Executing", "sql", sql)
if err := mg.exec(m); err != nil {
log.Error(3, "Migrator: error: \n%s:\n%s", err, sql)
mg.Logger.Error("Exec failed", "error", err, "sql", sql)
record.Error = err.Error()
mg.x.Insert(&record)
return err
@@ -114,9 +107,7 @@ func (mg *Migrator) Start() error {
}
func (mg *Migrator) exec(m Migration) error {
if mg.LogLevel <= log.INFO {
log.Info("Migrator: exec migration id: %v", m.Id())
}
log.Info("Executing migration", "id", m.Id())
err := mg.inTransaction(func(sess *xorm.Session) error {
@@ -125,14 +116,14 @@ func (mg *Migrator) exec(m Migration) error {
sql, args := condition.Sql(mg.dialect)
results, err := sess.Query(sql, args...)
if err != nil || len(results) == 0 {
log.Info("Migrator: skipping migration id: %v, condition not fulfilled", m.Id())
mg.Logger.Info("Skipping migration condition not fulfilled", "id", m.Id())
return sess.Rollback()
}
}
_, err := sess.Exec(m.Sql(mg.dialect))
if err != nil {
log.Error(3, "Migrator: exec FAILED migration id: %v, err: %v", m.Id(), err)
mg.Logger.Error("Executing migration failed", "id", m.Id(), "error", err)
return err
}
return nil
+7 -18
View File
@@ -40,8 +40,8 @@ var (
}
mysqlConfig MySQLConfig
UseSQLite3 bool
UseSQLite3 bool
sqlog log.Logger = log.New("sqlstore")
)
func EnsureAdminUser() {
@@ -74,13 +74,15 @@ func NewEngine() {
x, err := getEngine()
if err != nil {
log.Fatal(3, "Sqlstore: Fail to connect to database: %v", err)
sqlog.Crit("Fail to connect to database", "error", err)
os.Exit(1)
}
err = SetEngine(x, setting.Env == setting.DEV)
if err != nil {
log.Fatal(3, "fail to initialize orm engine: %v", err)
sqlog.Error("Fail to initialize orm engine: %v", err)
os.Exit(1)
}
}
@@ -89,24 +91,12 @@ func SetEngine(engine *xorm.Engine, enableLog bool) (err error) {
dialect = migrator.NewDialect(x.DriverName())
migrator := migrator.NewMigrator(x)
migrator.LogLevel = log.INFO
migrations.AddMigrations(migrator)
if err := migrator.Start(); err != nil {
return fmt.Errorf("Sqlstore::Migration failed err: %v\n", err)
}
if enableLog {
logPath := path.Join(setting.LogsPath, "xorm.log")
os.MkdirAll(path.Dir(logPath), os.ModePerm)
f, err := os.Create(logPath)
if err != nil {
return fmt.Errorf("sqlstore.init(fail to create xorm.log): %v", err)
}
x.Logger = xorm.NewSimpleLogger(f)
}
return nil
}
@@ -158,8 +148,7 @@ func getEngine() (*xorm.Engine, error) {
return nil, fmt.Errorf("Unknown database type: %s", DbCfg.Type)
}
log.Info("Database: %v", DbCfg.Type)
sqlog.Info("Initializing DB", "dbtype", DbCfg.Type)
return xorm.NewEngine(DbCfg.Type, cnnstr)
}
+14 -5
View File
@@ -161,13 +161,22 @@ func GetUserByLogin(query *m.GetUserByLoginQuery) error {
}
user := new(m.User)
if strings.Contains(query.LoginOrEmail, "@") {
user = &m.User{Email: query.LoginOrEmail}
} else {
user = &m.User{Login: query.LoginOrEmail}
// Try and find the user by login first.
// It's not sufficient to assume that a LoginOrEmail with an "@" is an email.
user = &m.User{Login: query.LoginOrEmail}
has, err := x.Get(user)
if err != nil {
return err
}
has, err := x.Get(user)
if has == false && strings.Contains(query.LoginOrEmail, "@") {
// If the user wasn't found, and it contains an "@" fallback to finding the
// user by email.
user = &m.User{Email: query.LoginOrEmail}
has, err = x.Get(user)
}
if err != nil {
return err