mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-01-23 09:00:20 +08:00
feat(sqlx): error log print traceId and spanId (#2845)
This commit is contained in:
parent
d7d6eccce6
commit
ea7dab3d26
@ -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
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
|
@ -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) {
|
||||
|
Loading…
Reference in New Issue
Block a user