Converting to structured logging the file store/sqlstore/supplier.go (#12089)

* Converting to structured logging the file store/sqlstore/supplier.go

* removed unused : from comments
Этот коммит содержится в:
Nikhil Ranjan
2019-09-10 11:42:05 +02:00
коммит произвёл Miguel de la Cruz
родитель ae05429ffe
Коммит 9094aa7f97

Просмотреть файл

@@ -159,14 +159,14 @@ func NewSqlSupplier(settings model.SqlSettings, metrics einterfaces.MetricsInter
err := supplier.GetMaster().CreateTablesIfNotExists() err := supplier.GetMaster().CreateTablesIfNotExists()
if err != nil { if err != nil {
mlog.Critical(fmt.Sprintf("Error creating database tables: %v", err)) mlog.Critical("Error creating database tables.", mlog.Err(err))
time.Sleep(time.Second) time.Sleep(time.Second)
os.Exit(EXIT_CREATE_TABLE) os.Exit(EXIT_CREATE_TABLE)
} }
err = UpgradeDatabase(supplier, model.CurrentVersion) err = UpgradeDatabase(supplier, model.CurrentVersion)
if err != nil { if err != nil {
mlog.Critical("Failed to upgrade database", mlog.Err(err)) mlog.Critical("Failed to upgrade database.", mlog.Err(err))
time.Sleep(time.Second) time.Sleep(time.Second)
os.Exit(EXIT_GENERIC_FAILURE) os.Exit(EXIT_GENERIC_FAILURE)
} }
@@ -214,13 +214,13 @@ func (s *SqlSupplier) Next() store.LayeredStoreSupplier {
func setupConnection(con_type string, dataSource string, settings *model.SqlSettings) *gorp.DbMap { func setupConnection(con_type string, dataSource string, settings *model.SqlSettings) *gorp.DbMap {
db, err := dbsql.Open(*settings.DriverName, dataSource) db, err := dbsql.Open(*settings.DriverName, dataSource)
if err != nil { if err != nil {
mlog.Critical(fmt.Sprintf("Failed to open SQL connection to err:%v", err.Error())) mlog.Critical("Failed to open SQL connection to err.", mlog.Err(err))
time.Sleep(time.Second) time.Sleep(time.Second)
os.Exit(EXIT_DB_OPEN) os.Exit(EXIT_DB_OPEN)
} }
for i := 0; i < DB_PING_ATTEMPTS; i++ { for i := 0; i < DB_PING_ATTEMPTS; i++ {
mlog.Info(fmt.Sprintf("Pinging SQL %v database", con_type)) mlog.Info("Pinging SQL", mlog.String("database", con_type))
ctx, cancel := context.WithTimeout(context.Background(), DB_PING_TIMEOUT_SECS*time.Second) ctx, cancel := context.WithTimeout(context.Background(), DB_PING_TIMEOUT_SECS*time.Second)
defer cancel() defer cancel()
err = db.PingContext(ctx) err = db.PingContext(ctx)
@@ -228,11 +228,11 @@ func setupConnection(con_type string, dataSource string, settings *model.SqlSett
break break
} else { } else {
if i == DB_PING_ATTEMPTS-1 { if i == DB_PING_ATTEMPTS-1 {
mlog.Critical(fmt.Sprintf("Failed to ping DB, server will exit err=%v", err)) mlog.Critical("Failed to ping DB, server will exit.", mlog.Err(err))
time.Sleep(time.Second) time.Sleep(time.Second)
os.Exit(EXIT_PING) os.Exit(EXIT_PING)
} else { } else {
mlog.Error(fmt.Sprintf("Failed to ping DB retrying in %v seconds err=%v", DB_PING_TIMEOUT_SECS, err)) mlog.Error("Failed to ping DB", mlog.Err(err), mlog.Int("retrying in seconds", DB_PING_TIMEOUT_SECS))
time.Sleep(DB_PING_TIMEOUT_SECS * time.Second) time.Sleep(DB_PING_TIMEOUT_SECS * time.Second)
} }
} }
@@ -365,7 +365,7 @@ func (ss *SqlSupplier) DoesTableExist(tableName string) bool {
) )
if err != nil { if err != nil {
mlog.Critical(fmt.Sprintf("Failed to check if table exists %v", err)) mlog.Critical("Failed to check if table exists", mlog.Err(err))
time.Sleep(time.Second) time.Sleep(time.Second)
os.Exit(EXIT_TABLE_EXISTS) os.Exit(EXIT_TABLE_EXISTS)
} }
@@ -387,7 +387,7 @@ func (ss *SqlSupplier) DoesTableExist(tableName string) bool {
) )
if err != nil { if err != nil {
mlog.Critical(fmt.Sprintf("Failed to check if table exists %v", err)) mlog.Critical("Failed to check if table exists", mlog.Err(err))
time.Sleep(time.Second) time.Sleep(time.Second)
os.Exit(EXIT_TABLE_EXISTS_MYSQL) os.Exit(EXIT_TABLE_EXISTS_MYSQL)
} }
@@ -401,7 +401,7 @@ func (ss *SqlSupplier) DoesTableExist(tableName string) bool {
) )
if err != nil { if err != nil {
mlog.Critical(fmt.Sprintf("Failed to check if table exists %v", err)) mlog.Critical("Failed to check if table exists", mlog.Err(err))
time.Sleep(time.Second) time.Sleep(time.Second)
os.Exit(EXIT_TABLE_EXISTS_SQLITE) os.Exit(EXIT_TABLE_EXISTS_SQLITE)
} }
@@ -433,7 +433,7 @@ func (ss *SqlSupplier) DoesColumnExist(tableName string, columnName string) bool
return false return false
} }
mlog.Critical(fmt.Sprintf("Failed to check if column exists %v", err)) mlog.Critical("Failed to check if column exists", mlog.Err(err))
time.Sleep(time.Second) time.Sleep(time.Second)
os.Exit(EXIT_DOES_COLUMN_EXISTS_POSTGRES) os.Exit(EXIT_DOES_COLUMN_EXISTS_POSTGRES)
} }
@@ -456,7 +456,7 @@ func (ss *SqlSupplier) DoesColumnExist(tableName string, columnName string) bool
) )
if err != nil { if err != nil {
mlog.Critical(fmt.Sprintf("Failed to check if column exists %v", err)) mlog.Critical("Failed to check if column exists", mlog.Err(err))
time.Sleep(time.Second) time.Sleep(time.Second)
os.Exit(EXIT_DOES_COLUMN_EXISTS_MYSQL) os.Exit(EXIT_DOES_COLUMN_EXISTS_MYSQL)
} }
@@ -471,7 +471,7 @@ func (ss *SqlSupplier) DoesColumnExist(tableName string, columnName string) bool
) )
if err != nil { if err != nil {
mlog.Critical(fmt.Sprintf("Failed to check if column exists %v", err)) mlog.Critical("Failed to check if column exists", mlog.Err(err))
time.Sleep(time.Second) time.Sleep(time.Second)
os.Exit(EXIT_DOES_COLUMN_EXISTS_SQLITE) os.Exit(EXIT_DOES_COLUMN_EXISTS_SQLITE)
} }
@@ -498,7 +498,7 @@ func (ss *SqlSupplier) DoesTriggerExist(triggerName string) bool {
`, triggerName) `, triggerName)
if err != nil { if err != nil {
mlog.Critical(fmt.Sprintf("Failed to check if trigger exists %v", err)) mlog.Critical("Failed to check if trigger exists", mlog.Err(err))
time.Sleep(time.Second) time.Sleep(time.Second)
os.Exit(EXIT_GENERIC_FAILURE) os.Exit(EXIT_GENERIC_FAILURE)
} }
@@ -517,7 +517,7 @@ func (ss *SqlSupplier) DoesTriggerExist(triggerName string) bool {
`, triggerName) `, triggerName)
if err != nil { if err != nil {
mlog.Critical(fmt.Sprintf("Failed to check if trigger exists %v", err)) mlog.Critical("Failed to check if trigger exists", mlog.Err(err))
time.Sleep(time.Second) time.Sleep(time.Second)
os.Exit(EXIT_GENERIC_FAILURE) os.Exit(EXIT_GENERIC_FAILURE)
} }
@@ -541,7 +541,7 @@ func (ss *SqlSupplier) CreateColumnIfNotExists(tableName string, columnName stri
if ss.DriverName() == model.DATABASE_DRIVER_POSTGRES { if ss.DriverName() == model.DATABASE_DRIVER_POSTGRES {
_, err := ss.GetMaster().ExecNoTimeout("ALTER TABLE " + tableName + " ADD " + columnName + " " + postgresColType + " DEFAULT '" + defaultValue + "'") _, err := ss.GetMaster().ExecNoTimeout("ALTER TABLE " + tableName + " ADD " + columnName + " " + postgresColType + " DEFAULT '" + defaultValue + "'")
if err != nil { if err != nil {
mlog.Critical(fmt.Sprintf("Failed to create column %v", err)) mlog.Critical("Failed to create column", mlog.Err(err))
time.Sleep(time.Second) time.Sleep(time.Second)
os.Exit(EXIT_CREATE_COLUMN_POSTGRES) os.Exit(EXIT_CREATE_COLUMN_POSTGRES)
} }
@@ -551,7 +551,7 @@ func (ss *SqlSupplier) CreateColumnIfNotExists(tableName string, columnName stri
} else if ss.DriverName() == model.DATABASE_DRIVER_MYSQL { } else if ss.DriverName() == model.DATABASE_DRIVER_MYSQL {
_, err := ss.GetMaster().ExecNoTimeout("ALTER TABLE " + tableName + " ADD " + columnName + " " + mySqlColType + " DEFAULT '" + defaultValue + "'") _, err := ss.GetMaster().ExecNoTimeout("ALTER TABLE " + tableName + " ADD " + columnName + " " + mySqlColType + " DEFAULT '" + defaultValue + "'")
if err != nil { if err != nil {
mlog.Critical(fmt.Sprintf("Failed to create column %v", err)) mlog.Critical("Failed to create column", mlog.Err(err))
time.Sleep(time.Second) time.Sleep(time.Second)
os.Exit(EXIT_CREATE_COLUMN_MYSQL) os.Exit(EXIT_CREATE_COLUMN_MYSQL)
} }
@@ -575,7 +575,7 @@ func (ss *SqlSupplier) CreateColumnIfNotExistsNoDefault(tableName string, column
if ss.DriverName() == model.DATABASE_DRIVER_POSTGRES { if ss.DriverName() == model.DATABASE_DRIVER_POSTGRES {
_, err := ss.GetMaster().ExecNoTimeout("ALTER TABLE " + tableName + " ADD " + columnName + " " + postgresColType) _, err := ss.GetMaster().ExecNoTimeout("ALTER TABLE " + tableName + " ADD " + columnName + " " + postgresColType)
if err != nil { if err != nil {
mlog.Critical(fmt.Sprintf("Failed to create column %v", err)) mlog.Critical("Failed to create column", mlog.Err(err))
time.Sleep(time.Second) time.Sleep(time.Second)
os.Exit(EXIT_CREATE_COLUMN_POSTGRES) os.Exit(EXIT_CREATE_COLUMN_POSTGRES)
} }
@@ -585,7 +585,7 @@ func (ss *SqlSupplier) CreateColumnIfNotExistsNoDefault(tableName string, column
} else if ss.DriverName() == model.DATABASE_DRIVER_MYSQL { } else if ss.DriverName() == model.DATABASE_DRIVER_MYSQL {
_, err := ss.GetMaster().ExecNoTimeout("ALTER TABLE " + tableName + " ADD " + columnName + " " + mySqlColType) _, err := ss.GetMaster().ExecNoTimeout("ALTER TABLE " + tableName + " ADD " + columnName + " " + mySqlColType)
if err != nil { if err != nil {
mlog.Critical(fmt.Sprintf("Failed to create column %v", err)) mlog.Critical("Failed to create column", mlog.Err(err))
time.Sleep(time.Second) time.Sleep(time.Second)
os.Exit(EXIT_CREATE_COLUMN_MYSQL) os.Exit(EXIT_CREATE_COLUMN_MYSQL)
} }
@@ -608,7 +608,7 @@ func (ss *SqlSupplier) RemoveColumnIfExists(tableName string, columnName string)
_, err := ss.GetMaster().ExecNoTimeout("ALTER TABLE " + tableName + " DROP COLUMN " + columnName) _, err := ss.GetMaster().ExecNoTimeout("ALTER TABLE " + tableName + " DROP COLUMN " + columnName)
if err != nil { if err != nil {
mlog.Critical(fmt.Sprintf("Failed to drop column %v", err)) mlog.Critical("Failed to drop column", mlog.Err(err))
time.Sleep(time.Second) time.Sleep(time.Second)
os.Exit(EXIT_REMOVE_COLUMN) os.Exit(EXIT_REMOVE_COLUMN)
} }
@@ -623,7 +623,7 @@ func (ss *SqlSupplier) RemoveTableIfExists(tableName string) bool {
_, err := ss.GetMaster().ExecNoTimeout("DROP TABLE " + tableName) _, err := ss.GetMaster().ExecNoTimeout("DROP TABLE " + tableName)
if err != nil { if err != nil {
mlog.Critical(fmt.Sprintf("Failed to drop table %v", err)) mlog.Critical("Failed to drop table", mlog.Err(err))
time.Sleep(time.Second) time.Sleep(time.Second)
os.Exit(EXIT_REMOVE_TABLE) os.Exit(EXIT_REMOVE_TABLE)
} }
@@ -644,7 +644,7 @@ func (ss *SqlSupplier) RenameColumnIfExists(tableName string, oldColumnName stri
} }
if err != nil { if err != nil {
mlog.Critical(fmt.Sprintf("Failed to rename column %v", err)) mlog.Critical("Failed to rename column", mlog.Err(err))
time.Sleep(time.Second) time.Sleep(time.Second)
os.Exit(EXIT_RENAME_COLUMN) os.Exit(EXIT_RENAME_COLUMN)
} }
@@ -666,7 +666,7 @@ func (ss *SqlSupplier) GetMaxLengthOfColumnIfExists(tableName string, columnName
} }
if err != nil { if err != nil {
mlog.Critical(fmt.Sprintf("Failed to get max length of column %v", err)) mlog.Critical("Failed to get max length of column", mlog.Err(err))
time.Sleep(time.Second) time.Sleep(time.Second)
os.Exit(EXIT_MAX_COLUMN) os.Exit(EXIT_MAX_COLUMN)
} }
@@ -687,7 +687,7 @@ func (ss *SqlSupplier) AlterColumnTypeIfExists(tableName string, columnName stri
} }
if err != nil { if err != nil {
mlog.Critical(fmt.Sprintf("Failed to alter column type %v", err)) mlog.Critical("Failed to alter column type", mlog.Err(err))
time.Sleep(time.Second) time.Sleep(time.Second)
os.Exit(EXIT_ALTER_COLUMN) os.Exit(EXIT_ALTER_COLUMN)
} }
@@ -736,7 +736,7 @@ func (ss *SqlSupplier) AlterColumnDefaultIfExists(tableName string, columnName s
} }
if err != nil { if err != nil {
mlog.Critical(fmt.Sprintf("Failed to alter column %s.%s default %s: %v", tableName, columnName, defaultValue, err)) mlog.Critical("Failed to alter column", mlog.String("table", tableName), mlog.String("column", columnName), mlog.String("default value", defaultValue), mlog.Err(err))
time.Sleep(time.Second) time.Sleep(time.Second)
os.Exit(EXIT_GENERIC_FAILURE) os.Exit(EXIT_GENERIC_FAILURE)
return false return false
@@ -790,7 +790,7 @@ func (ss *SqlSupplier) createIndexIfNotExists(indexName string, tableName string
_, err := ss.GetMaster().ExecNoTimeout(query) _, err := ss.GetMaster().ExecNoTimeout(query)
if err != nil { if err != nil {
mlog.Critical(fmt.Sprintf("Failed to create index %v, %v", errExists, err)) mlog.Critical("Failed to create index", mlog.Err(errExists), mlog.Err(err))
time.Sleep(time.Second) time.Sleep(time.Second)
os.Exit(EXIT_CREATE_INDEX_POSTGRES) os.Exit(EXIT_CREATE_INDEX_POSTGRES)
} }
@@ -798,7 +798,7 @@ func (ss *SqlSupplier) createIndexIfNotExists(indexName string, tableName string
count, err := ss.GetMaster().SelectInt("SELECT COUNT(0) AS index_exists FROM information_schema.statistics WHERE TABLE_SCHEMA = DATABASE() and table_name = ? AND index_name = ?", tableName, indexName) count, err := ss.GetMaster().SelectInt("SELECT COUNT(0) AS index_exists FROM information_schema.statistics WHERE TABLE_SCHEMA = DATABASE() and table_name = ? AND index_name = ?", tableName, indexName)
if err != nil { if err != nil {
mlog.Critical(fmt.Sprintf("Failed to check index %v", err)) mlog.Critical("Failed to check index", mlog.Err(err))
time.Sleep(time.Second) time.Sleep(time.Second)
os.Exit(EXIT_CREATE_INDEX_MYSQL) os.Exit(EXIT_CREATE_INDEX_MYSQL)
} }
@@ -814,14 +814,14 @@ func (ss *SqlSupplier) createIndexIfNotExists(indexName string, tableName string
_, err = ss.GetMaster().ExecNoTimeout("CREATE " + uniqueStr + fullTextIndex + " INDEX " + indexName + " ON " + tableName + " (" + strings.Join(columnNames, ", ") + ")") _, err = ss.GetMaster().ExecNoTimeout("CREATE " + uniqueStr + fullTextIndex + " INDEX " + indexName + " ON " + tableName + " (" + strings.Join(columnNames, ", ") + ")")
if err != nil { if err != nil {
mlog.Critical(fmt.Sprintf("Failed to create index %v", err)) mlog.Critical("Failed to create index", mlog.Err(err))
time.Sleep(time.Second) time.Sleep(time.Second)
os.Exit(EXIT_CREATE_INDEX_FULL_MYSQL) os.Exit(EXIT_CREATE_INDEX_FULL_MYSQL)
} }
} else if ss.DriverName() == model.DATABASE_DRIVER_SQLITE { } else if ss.DriverName() == model.DATABASE_DRIVER_SQLITE {
_, err := ss.GetMaster().ExecNoTimeout("CREATE INDEX IF NOT EXISTS " + indexName + " ON " + tableName + " (" + strings.Join(columnNames, ", ") + ")") _, err := ss.GetMaster().ExecNoTimeout("CREATE INDEX IF NOT EXISTS " + indexName + " ON " + tableName + " (" + strings.Join(columnNames, ", ") + ")")
if err != nil { if err != nil {
mlog.Critical(fmt.Sprintf("Failed to create index %v", err)) mlog.Critical("Failed to create index", mlog.Err(err))
time.Sleep(time.Second) time.Sleep(time.Second)
os.Exit(EXIT_CREATE_INDEX_SQLITE) os.Exit(EXIT_CREATE_INDEX_SQLITE)
} }
@@ -845,7 +845,7 @@ func (ss *SqlSupplier) RemoveIndexIfExists(indexName string, tableName string) b
_, err = ss.GetMaster().ExecNoTimeout("DROP INDEX " + indexName) _, err = ss.GetMaster().ExecNoTimeout("DROP INDEX " + indexName)
if err != nil { if err != nil {
mlog.Critical(fmt.Sprintf("Failed to remove index %v", err)) mlog.Critical("Failed to remove index", mlog.Err(err))
time.Sleep(time.Second) time.Sleep(time.Second)
os.Exit(EXIT_REMOVE_INDEX_POSTGRES) os.Exit(EXIT_REMOVE_INDEX_POSTGRES)
} }
@@ -855,7 +855,7 @@ func (ss *SqlSupplier) RemoveIndexIfExists(indexName string, tableName string) b
count, err := ss.GetMaster().SelectInt("SELECT COUNT(0) AS index_exists FROM information_schema.statistics WHERE TABLE_SCHEMA = DATABASE() and table_name = ? AND index_name = ?", tableName, indexName) count, err := ss.GetMaster().SelectInt("SELECT COUNT(0) AS index_exists FROM information_schema.statistics WHERE TABLE_SCHEMA = DATABASE() and table_name = ? AND index_name = ?", tableName, indexName)
if err != nil { if err != nil {
mlog.Critical(fmt.Sprintf("Failed to check index %v", err)) mlog.Critical("Failed to check index", mlog.Err(err))
time.Sleep(time.Second) time.Sleep(time.Second)
os.Exit(EXIT_REMOVE_INDEX_MYSQL) os.Exit(EXIT_REMOVE_INDEX_MYSQL)
} }
@@ -866,14 +866,14 @@ func (ss *SqlSupplier) RemoveIndexIfExists(indexName string, tableName string) b
_, err = ss.GetMaster().ExecNoTimeout("DROP INDEX " + indexName + " ON " + tableName) _, err = ss.GetMaster().ExecNoTimeout("DROP INDEX " + indexName + " ON " + tableName)
if err != nil { if err != nil {
mlog.Critical(fmt.Sprintf("Failed to remove index %v", err)) mlog.Critical("Failed to remove index", mlog.Err(err))
time.Sleep(time.Second) time.Sleep(time.Second)
os.Exit(EXIT_REMOVE_INDEX_MYSQL) os.Exit(EXIT_REMOVE_INDEX_MYSQL)
} }
} else if ss.DriverName() == model.DATABASE_DRIVER_SQLITE { } else if ss.DriverName() == model.DATABASE_DRIVER_SQLITE {
_, err := ss.GetMaster().ExecNoTimeout("DROP INDEX IF EXISTS " + indexName) _, err := ss.GetMaster().ExecNoTimeout("DROP INDEX IF EXISTS " + indexName)
if err != nil { if err != nil {
mlog.Critical(fmt.Sprintf("Failed to remove index %v", err)) mlog.Critical("Failed to remove index", mlog.Err(err))
time.Sleep(time.Second) time.Sleep(time.Second)
os.Exit(EXIT_REMOVE_INDEX_SQLITE) os.Exit(EXIT_REMOVE_INDEX_SQLITE)
} }