fix: health check problem (#4590)

This commit is contained in:
Kevin Wan
2025-01-22 19:32:28 +08:00
committed by GitHub
parent 030c859171
commit 1023800b02
6 changed files with 24 additions and 19 deletions

View File

@@ -41,6 +41,7 @@ var (
type (
statGetter struct {
host string
dbName string
hash string
poolStats func() sql.DBStats

View File

@@ -8,7 +8,7 @@ import (
"time"
"github.com/go-sql-driver/mysql"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/core/syncx"
)
@@ -27,19 +27,19 @@ func getCachedSqlConn(driverName, server string) (*sql.DB, error) {
return nil, err
}
if driverName == mysqlDriverName {
if cfg, err := mysql.ParseDSN(server); err != nil {
// do nothing here
} else {
checksum := sha256.Sum256([]byte(server))
connCollector.registerClient(&statGetter{
dbName: cfg.DBName,
hash: hex.EncodeToString(checksum[:]),
poolStats: func() sql.DBStats {
return conn.Stats()
},
})
}
if cfg, e := mysql.ParseDSN(server); e != nil {
// if cannot parse, don't collect the metrics
logx.Error(e)
} else {
checksum := sha256.Sum256([]byte(server))
connCollector.registerClient(&statGetter{
host: cfg.Addr,
dbName: cfg.DBName,
hash: hex.EncodeToString(checksum[:]),
poolStats: func() sql.DBStats {
return conn.Stats()
},
})
}
return conn, nil