From 030c8591719c5b35da062d2445e2a48b9d622fbd Mon Sep 17 00:00:00 2001 From: MarkJoyMa <64180138+MarkJoyMa@users.noreply.github.com> Date: Wed, 22 Jan 2025 15:42:02 +0800 Subject: [PATCH] feat/sqlx_metric (#4587) Co-authored-by: aiden.ma --- core/stores/sqlx/sqlmanager.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/core/stores/sqlx/sqlmanager.go b/core/stores/sqlx/sqlmanager.go index db652edc..22db12bd 100644 --- a/core/stores/sqlx/sqlmanager.go +++ b/core/stores/sqlx/sqlmanager.go @@ -1,10 +1,14 @@ package sqlx import ( + "crypto/sha256" "database/sql" + "encoding/hex" "io" "time" + "github.com/go-sql-driver/mysql" + "github.com/zeromicro/go-zero/core/syncx" ) @@ -23,6 +27,21 @@ 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() + }, + }) + } + } + return conn, nil }) if err != nil {