feat(sqlx): error log print traceId and spanId (#2845)

master
chen quan 2 years ago committed by GitHub
parent d7d6eccce6
commit ea7dab3d26
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -64,7 +64,7 @@ type (
// query arguments into one string and do underlying query without arguments // query arguments into one string and do underlying query without arguments
commonSqlConn struct { commonSqlConn struct {
connProv connProvider connProv connProvider
onError func(error) onError func(context.Context, error)
beginTx beginnable beginTx beginnable
brk breaker.Breaker brk breaker.Breaker
accept func(error) bool accept func(error) bool
@ -98,8 +98,8 @@ func NewSqlConn(driverName, datasource string, opts ...SqlOption) SqlConn {
connProv: func() (*sql.DB, error) { connProv: func() (*sql.DB, error) {
return getSqlConn(driverName, datasource) return getSqlConn(driverName, datasource)
}, },
onError: func(err error) { onError: func(ctx context.Context, err error) {
logInstanceError(datasource, err) logInstanceError(ctx, datasource, err)
}, },
beginTx: begin, beginTx: begin,
brk: breaker.NewBreaker(), brk: breaker.NewBreaker(),
@ -118,8 +118,8 @@ func NewSqlConnFromDB(db *sql.DB, opts ...SqlOption) SqlConn {
connProv: func() (*sql.DB, error) { connProv: func() (*sql.DB, error) {
return db, nil return db, nil
}, },
onError: func(err error) { onError: func(ctx context.Context, err error) {
logx.Errorf("Error on getting sql instance: %v", err) logx.WithContext(ctx).Errorf("Error on getting sql instance: %v", err)
}, },
beginTx: begin, beginTx: begin,
brk: breaker.NewBreaker(), brk: breaker.NewBreaker(),
@ -146,7 +146,7 @@ func (db *commonSqlConn) ExecCtx(ctx context.Context, q string, args ...any) (
var conn *sql.DB var conn *sql.DB
conn, err = db.connProv() conn, err = db.connProv()
if err != nil { if err != nil {
db.onError(err) db.onError(ctx, err)
return err return err
} }
@ -174,7 +174,7 @@ func (db *commonSqlConn) PrepareCtx(ctx context.Context, query string) (stmt Stm
var conn *sql.DB var conn *sql.DB
conn, err = db.connProv() conn, err = db.connProv()
if err != nil { if err != nil {
db.onError(err) db.onError(ctx, err)
return err return err
} }
@ -301,7 +301,7 @@ func (db *commonSqlConn) queryRows(ctx context.Context, scanner func(*sql.Rows)
err = db.brk.DoWithAcceptable(func() error { err = db.brk.DoWithAcceptable(func() error {
conn, err := db.connProv() conn, err := db.connProv()
if err != nil { if err != nil {
db.onError(err) db.onError(ctx, err)
return err return err
} }

@ -139,7 +139,7 @@ func transact(ctx context.Context, db *commonSqlConn, b beginnable,
fn func(context.Context, Session) error) (err error) { fn func(context.Context, Session) error) (err error) {
conn, err := db.connProv() conn, err := db.connProv()
if err != nil { if err != nil {
db.onError(err) db.onError(ctx, err)
return err return err
} }

@ -130,9 +130,9 @@ func format(query string, args ...any) (string, error) {
return b.String(), nil return b.String(), nil
} }
func logInstanceError(datasource string, err error) { func logInstanceError(ctx context.Context, datasource string, err error) {
datasource = desensitize(datasource) 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) { func logSqlError(ctx context.Context, stmt string, err error) {

Loading…
Cancel
Save