Skip to content

Commit

Permalink
Db error log
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaslopezf committed Nov 7, 2023
1 parent 408735f commit b565fe0
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions pkg/zdb/zdbconnector/clickhouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"fmt"
clickhouse2 "github.com/ClickHouse/clickhouse-go/v2"
"github.com/zondax/golem/pkg/zdb/zdbconfig"
"go.uber.org/zap"
"gorm.io/driver/clickhouse"
"gorm.io/gorm"
"net/url"
"strings"
)

Expand All @@ -19,6 +21,8 @@ func (c *ClickHouseConnector) Connect(config *zdbconfig.Config) (*gorm.DB, error
var err error
dbConn, err = gorm.Open(clickhouse.Open(dsn), gormConfig)
if err != nil {
logDSN := obfuscatePasswordInDSN(dsn)
zap.S().Errorf("Failed to open database connection: %v, DSN: %s", err, logDSN)
return nil, err
}
return dbConn, nil
Expand Down Expand Up @@ -50,3 +54,17 @@ func buildClickhouseDSN(params zdbconfig.ConnectionParams) string {
func (c *ClickHouseConnector) VerifyConnection(db *gorm.DB) error {
return db.Exec("SELECT 1").Error
}

func obfuscatePasswordInDSN(dsn string) string {
u, err := url.Parse(dsn)
if err != nil {
zap.S().Errorf("Error parsing DSN: %v", err)
return ""
}

if _, hasPassword := u.User.Password(); hasPassword {
u.User = url.UserPassword(u.User.Username(), "*****")
}

return u.String()
}

0 comments on commit b565fe0

Please sign in to comment.