feat/sqlx_metric (#4587)

Co-authored-by: aiden.ma <Aiden.ma@yijinin.com>
This commit is contained in:
MarkJoyMa 2025-01-22 15:42:02 +08:00 committed by GitHub
parent e6d1b47a43
commit 030c859171
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,10 +1,14 @@
package sqlx package sqlx
import ( import (
"crypto/sha256"
"database/sql" "database/sql"
"encoding/hex"
"io" "io"
"time" "time"
"github.com/go-sql-driver/mysql"
"github.com/zeromicro/go-zero/core/syncx" "github.com/zeromicro/go-zero/core/syncx"
) )
@ -23,6 +27,21 @@ func getCachedSqlConn(driverName, server string) (*sql.DB, error) {
return nil, err 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()
},
})
}
}
return conn, nil return conn, nil
}) })
if err != nil { if err != nil {