From ea7dab3d26ccba0977cd38efa4fc4decd097bbd9 Mon Sep 17 00:00:00 2001 From: chen quan Date: Sat, 11 Feb 2023 16:12:14 +0800 Subject: [PATCH] feat(sqlx): error log print traceId and spanId (#2845) --- core/stores/sqlx/sqlconn.go | 16 ++++++++-------- core/stores/sqlx/tx.go | 2 +- core/stores/sqlx/utils.go | 4 ++-- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/core/stores/sqlx/sqlconn.go b/core/stores/sqlx/sqlconn.go index bc3eeea4..7945ce1f 100644 --- a/core/stores/sqlx/sqlconn.go +++ b/core/stores/sqlx/sqlconn.go @@ -64,7 +64,7 @@ type ( // query arguments into one string and do underlying query without arguments commonSqlConn struct { connProv connProvider - onError func(error) + onError func(context.Context, error) beginTx beginnable brk breaker.Breaker accept func(error) bool @@ -98,8 +98,8 @@ func NewSqlConn(driverName, datasource string, opts ...SqlOption) SqlConn { connProv: func() (*sql.DB, error) { return getSqlConn(driverName, datasource) }, - onError: func(err error) { - logInstanceError(datasource, err) + onError: func(ctx context.Context, err error) { + logInstanceError(ctx, datasource, err) }, beginTx: begin, brk: breaker.NewBreaker(), @@ -118,8 +118,8 @@ func NewSqlConnFromDB(db *sql.DB, opts ...SqlOption) SqlConn { connProv: func() (*sql.DB, error) { return db, nil }, - onError: func(err error) { - logx.Errorf("Error on getting sql instance: %v", err) + onError: func(ctx context.Context, err error) { + logx.WithContext(ctx).Errorf("Error on getting sql instance: %v", err) }, beginTx: begin, brk: breaker.NewBreaker(), @@ -146,7 +146,7 @@ func (db *commonSqlConn) ExecCtx(ctx context.Context, q string, args ...any) ( var conn *sql.DB conn, err = db.connProv() if err != nil { - db.onError(err) + db.onError(ctx, err) return err } @@ -174,7 +174,7 @@ func (db *commonSqlConn) PrepareCtx(ctx context.Context, query string) (stmt Stm var conn *sql.DB conn, err = db.connProv() if err != nil { - db.onError(err) + db.onError(ctx, err) return err } @@ -301,7 +301,7 @@ func (db *commonSqlConn) queryRows(ctx context.Context, scanner func(*sql.Rows) err = db.brk.DoWithAcceptable(func() error { conn, err := db.connProv() if err != nil { - db.onError(err) + db.onError(ctx, err) return err } diff --git a/core/stores/sqlx/tx.go b/core/stores/sqlx/tx.go index 9c586c2e..e95c41c4 100644 --- a/core/stores/sqlx/tx.go +++ b/core/stores/sqlx/tx.go @@ -139,7 +139,7 @@ func transact(ctx context.Context, db *commonSqlConn, b beginnable, fn func(context.Context, Session) error) (err error) { conn, err := db.connProv() if err != nil { - db.onError(err) + db.onError(ctx, err) return err } diff --git a/core/stores/sqlx/utils.go b/core/stores/sqlx/utils.go index 1bdcb684..9e67007b 100644 --- a/core/stores/sqlx/utils.go +++ b/core/stores/sqlx/utils.go @@ -130,9 +130,9 @@ func format(query string, args ...any) (string, error) { return b.String(), nil } -func logInstanceError(datasource string, err error) { +func logInstanceError(ctx context.Context, datasource string, err error) { datasource = desensitize(datasource) - logx.Errorf("Error on getting sql instance of %s: %v", datasource, err) + logx.WithContext(ctx).Errorf("Error on getting sql instance of %s: %v", datasource, err) } func logSqlError(ctx context.Context, stmt string, err error) {