Skip to content

Commit

Permalink
Fixing Improper-sqlite-logs (#1057)
Browse files Browse the repository at this point in the history
  • Loading branch information
coolwednesday authored Sep 30, 2024
1 parent 5987075 commit 2074f0b
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 12 deletions.
36 changes: 24 additions & 12 deletions pkg/gofr/datasource/sql/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,11 @@ func NewSQL(configs config.Config, logger datasource.Logger, metrics Metrics) *D

database := &DB{config: dbConfig, logger: logger, metrics: metrics}

logger.Debugf("connecting to '%s' user to '%s' database at '%s:%s'", database.config.User,
database.config.Database, database.config.HostName, database.config.Port)
printConnectionSuccessLog("connecting", database.config, logger)

database.DB, err = sql.Open(otelRegisteredDialect, dbConnectionString)
if err != nil {
database.logger.Errorf("could not open connection with '%s' user to '%s' database at '%s:%s', error: %v",
database.config.User, database.config.Database, database.config.HostName, database.config.Port, err)
printConnectionFailureLog("open connection with", database.config, database.logger, err)

return database
}
Expand All @@ -94,14 +92,12 @@ func NewSQL(configs config.Config, logger datasource.Logger, metrics Metrics) *D

func pingToTestConnection(database *DB) *DB {
if err := database.DB.Ping(); err != nil {
database.logger.Errorf("could not connect with '%s' user to '%s' database at '%s:%s', error: %v",
database.config.User, database.config.Database, database.config.HostName, database.config.Port, err)
printConnectionFailureLog("connect", database.config, database.logger, err)

return database
}

database.logger.Logf("connected to '%s' database at '%s:%s'", database.config.Database,
database.config.HostName, database.config.Port)
printConnectionSuccessLog("connected", database.config, database.logger)

return database
}
Expand All @@ -116,14 +112,12 @@ func retryConnection(database *DB) {
for {
err := database.DB.Ping()
if err == nil {
database.logger.Logf("connected to '%s' database at '%s:%s'", database.config.Database,
database.config.HostName, database.config.Port)
printConnectionSuccessLog("connected", database.config, database.logger)

break
}

database.logger.Debugf("could not connect with '%s' user to '%s' database at '%s:%s', error: %v",
database.config.User, database.config.Database, database.config.HostName, database.config.Port, err)
printConnectionFailureLog("connect", database.config, database.logger, err)

time.Sleep(connRetryFrequencyInSeconds * time.Second)
}
Expand Down Expand Up @@ -204,3 +198,21 @@ func pushDBMetrics(db *sql.DB, metrics Metrics) {
}
}
}

func printConnectionSuccessLog(status string, dbconfig *DBConfig, logger datasource.Logger) {
if dbconfig.Dialect == sqlite {
logger.Debugf("%s to '%s' database", status, dbconfig.Database)
} else {
logger.Debugf("%s to '%s' user to '%s' database at '%s:%s'", status, dbconfig.User,
dbconfig.Database, dbconfig.HostName, dbconfig.Port)
}
}

func printConnectionFailureLog(action string, dbconfig *DBConfig, logger datasource.Logger, err error) {
if dbconfig.Dialect == sqlite {
logger.Errorf("could not %s database '%s', error: %v", action, dbconfig.Database, err)
} else {
logger.Errorf("could not %s '%s' user to '%s' database at '%s:%s', error: %v",
action, dbconfig.User, dbconfig.Database, dbconfig.HostName, dbconfig.Port, err)
}
}
55 changes: 55 additions & 0 deletions pkg/gofr/datasource/sql/sql_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sql

import (
"errors"
"fmt"
"testing"
"time"
Expand Down Expand Up @@ -256,6 +257,60 @@ func Test_NewSQLMockWithConfig(t *testing.T) {
assert.NotNil(t, mockMetric)
}

var errSqliteConnection = errors.New("connection failed")

func Test_sqliteSuccessfulConnLogs(t *testing.T) {
tests := []struct {
desc string
status string
expectedLog string
}{
{"sqlite connection in process", "connecting", `connecting to 'test' database`},
{"sqlite connected successfully", "connected", `connected to 'test' database`},
}

for _, test := range tests {
logs := testutil.StdoutOutputForFunc(func() {
mockLogger := logging.NewMockLogger(logging.DEBUG)
mockConfig := &DBConfig{
Dialect: sqlite,
Database: "test",
}

printConnectionSuccessLog(test.status, mockConfig, mockLogger)
})

assert.Contains(t, logs, test.expectedLog)
}
}

func Test_sqliteErrConnLogs(t *testing.T) {
test := []struct {
desc string
action string
err error
expectedLog string
}{
{"sqlite connection failure", "connect", errSqliteConnection,
`could not connect database 'test', error: connection failed`},
{"sqlite open connection failure", "open connection with", errSqliteConnection,
`could not open connection with database 'test', error: connection failed`},
}
for _, tt := range test {
logs := testutil.StderrOutputForFunc(func() {
mockLogger := logging.NewMockLogger(logging.DEBUG)
mockConfig := &DBConfig{
Dialect: sqlite,
Database: "test",
}

printConnectionFailureLog(tt.action, mockConfig, mockLogger, tt.err)
})

assert.Contains(t, logs, tt.expectedLog)
}
}

func Test_SQLRetryConnectionInfoLog(t *testing.T) {
logs := testutil.StdoutOutputForFunc(func() {
ctrl := gomock.NewController(t)
Expand Down

0 comments on commit 2074f0b

Please sign in to comment.