fix context

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2023-01-16 23:38:29 +03:00
parent 7e547cbe45
commit 066a62c1e3
4 changed files with 91 additions and 39 deletions

83
conn.go
View File

@ -20,6 +20,13 @@ type wrapperConn struct {
// Prepare implements driver.Conn Prepare // Prepare implements driver.Conn Prepare
func (w *wrapperConn) Prepare(query string) (driver.Stmt, error) { func (w *wrapperConn) Prepare(query string) (driver.Stmt, error) {
var ctx context.Context
if w.ctx != nil {
ctx = w.ctx
} else {
ctx = context.Background()
}
labels := []string{labelMethod, "Prepare", labelQuery, labelUnknown} labels := []string{labelMethod, "Prepare", labelQuery, labelUnknown}
ts := time.Now() ts := time.Now()
stmt, err := w.conn.Prepare(query) stmt, err := w.conn.Prepare(query)
@ -31,7 +38,7 @@ func (w *wrapperConn) Prepare(query string) (driver.Stmt, error) {
w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te) w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te)
if w.opts.LoggerEnabled { if w.opts.LoggerEnabled {
w.opts.Logger.Fields(w.opts.LoggerObserver(context.TODO(), "Prepare", labelUnknown, td, err)...).Log(context.TODO(), w.opts.LoggerLevel) w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "Prepare", labelUnknown, td, err)...).Log(ctx, w.opts.LoggerLevel)
} }
return nil, err return nil, err
} }
@ -40,14 +47,21 @@ func (w *wrapperConn) Prepare(query string) (driver.Stmt, error) {
w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te) w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te)
if w.opts.LoggerEnabled { if w.opts.LoggerEnabled {
w.opts.Logger.Fields(w.opts.LoggerObserver(context.TODO(), "QueryContext", labelUnknown, td, err)...).Log(context.TODO(), w.opts.LoggerLevel) w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "Prepare", labelUnknown, td, err)...).Log(ctx, w.opts.LoggerLevel)
} }
return &wrapperStmt{stmt: stmt, opts: w.opts}, nil return &wrapperStmt{stmt: stmt, opts: w.opts, ctx: ctx}, nil
} }
// Close implements driver.Conn Close // Close implements driver.Conn Close
func (w *wrapperConn) Close() error { func (w *wrapperConn) Close() error {
var ctx context.Context
if w.ctx != nil {
ctx = w.ctx
} else {
ctx = context.Background()
}
labels := []string{labelMethod, "Close"} labels := []string{labelMethod, "Close"}
ts := time.Now() ts := time.Now()
err := w.conn.Close() err := w.conn.Close()
@ -62,7 +76,7 @@ func (w *wrapperConn) Close() error {
w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te) w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te)
if w.opts.LoggerEnabled { if w.opts.LoggerEnabled {
w.opts.Logger.Fields(w.opts.LoggerObserver(context.TODO(), "QueryContext", labelUnknown, td, err)...).Log(context.TODO(), w.opts.LoggerLevel) w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "Close", labelUnknown, td, err)...).Log(ctx, w.opts.LoggerLevel)
} }
return err return err
@ -70,6 +84,13 @@ func (w *wrapperConn) Close() error {
// Begin implements driver.Conn Begin // Begin implements driver.Conn Begin
func (w *wrapperConn) Begin() (driver.Tx, error) { func (w *wrapperConn) Begin() (driver.Tx, error) {
var ctx context.Context
if w.ctx != nil {
ctx = w.ctx
} else {
ctx = context.Background()
}
labels := []string{labelMethod, "Begin"} labels := []string{labelMethod, "Begin"}
ts := time.Now() ts := time.Now()
// nolint:staticcheck // nolint:staticcheck
@ -81,7 +102,7 @@ func (w *wrapperConn) Begin() (driver.Tx, error) {
w.opts.Meter.Summary(meterRequestLatencyMicroseconds, labels...).Update(te) w.opts.Meter.Summary(meterRequestLatencyMicroseconds, labels...).Update(te)
w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te) w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te)
if w.opts.LoggerEnabled { if w.opts.LoggerEnabled {
w.opts.Logger.Fields(w.opts.LoggerObserver(context.TODO(), "Begin", labelUnknown, td, err)...).Log(context.TODO(), w.opts.LoggerLevel) w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "Begin", labelUnknown, td, err)...).Log(ctx, w.opts.LoggerLevel)
} }
return nil, err return nil, err
} }
@ -89,9 +110,9 @@ func (w *wrapperConn) Begin() (driver.Tx, error) {
w.opts.Meter.Summary(meterRequestLatencyMicroseconds, labels...).Update(te) w.opts.Meter.Summary(meterRequestLatencyMicroseconds, labels...).Update(te)
w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te) w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te)
if w.opts.LoggerEnabled { if w.opts.LoggerEnabled {
w.opts.Logger.Fields(w.opts.LoggerObserver(context.TODO(), "Begin", labelUnknown, td, err)...).Log(context.TODO(), w.opts.LoggerLevel) w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "Begin", labelUnknown, td, err)...).Log(ctx, w.opts.LoggerLevel)
} }
return &wrapperTx{tx: tx, opts: w.opts}, nil return &wrapperTx{tx: tx, opts: w.opts, ctx: ctx}, nil
} }
// BeginTx implements driver.ConnBeginTx BeginTx // BeginTx implements driver.ConnBeginTx BeginTx
@ -117,13 +138,13 @@ func (w *wrapperConn) BeginTx(ctx context.Context, opts driver.TxOptions) (drive
span.AddLabels("error", true) span.AddLabels("error", true)
span.AddLabels("err", err.Error()) span.AddLabels("err", err.Error())
if w.opts.LoggerEnabled { if w.opts.LoggerEnabled {
w.opts.Logger.Fields(w.opts.LoggerObserver(context.TODO(), "BeginTx", name, td, err)...).Log(context.TODO(), w.opts.LoggerLevel) w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "BeginTx", name, td, err)...).Log(ctx, w.opts.LoggerLevel)
} }
return nil, err return nil, err
} }
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelSuccess)...).Inc() w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelSuccess)...).Inc()
if w.opts.LoggerEnabled { if w.opts.LoggerEnabled {
w.opts.Logger.Fields(w.opts.LoggerObserver(context.TODO(), "BeginTx", name, td, err)...).Log(context.TODO(), w.opts.LoggerLevel) w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "BeginTx", name, td, err)...).Log(ctx, w.opts.LoggerLevel)
} }
w.ctx = nctx w.ctx = nctx
return &wrapperTx{ctx: ctx, tx: tx, opts: w.opts, span: span, conn: w}, nil return &wrapperTx{ctx: ctx, tx: tx, opts: w.opts, span: span, conn: w}, nil
@ -144,7 +165,7 @@ func (w *wrapperConn) BeginTx(ctx context.Context, opts driver.TxOptions) (drive
w.opts.Meter.Summary(meterRequestLatencyMicroseconds, labels...).Update(te) w.opts.Meter.Summary(meterRequestLatencyMicroseconds, labels...).Update(te)
w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te) w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te)
if w.opts.LoggerEnabled { if w.opts.LoggerEnabled {
w.opts.Logger.Fields(w.opts.LoggerObserver(context.TODO(), "BeginTx", name, td, err)...).Log(context.TODO(), w.opts.LoggerLevel) w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "BeginTx", name, td, err)...).Log(ctx, w.opts.LoggerLevel)
} }
w.ctx = nctx w.ctx = nctx
return &wrapperTx{ctx: ctx, tx: tx, opts: w.opts, span: span, conn: w}, nil return &wrapperTx{ctx: ctx, tx: tx, opts: w.opts, span: span, conn: w}, nil
@ -179,7 +200,7 @@ func (w *wrapperConn) PrepareContext(ctx context.Context, query string) (driver.
span.AddLabels("error", true) span.AddLabels("error", true)
span.AddLabels("err", err.Error()) span.AddLabels("err", err.Error())
if w.opts.LoggerEnabled { if w.opts.LoggerEnabled {
w.opts.Logger.Fields(w.opts.LoggerObserver(context.TODO(), "PrepareContext", name, td, err)...).Log(context.TODO(), w.opts.LoggerLevel) w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "PrepareContext", name, td, err)...).Log(ctx, w.opts.LoggerLevel)
} }
return nil, err return nil, err
} }
@ -187,7 +208,7 @@ func (w *wrapperConn) PrepareContext(ctx context.Context, query string) (driver.
w.opts.Meter.Summary(meterRequestLatencyMicroseconds, labels...).Update(te) w.opts.Meter.Summary(meterRequestLatencyMicroseconds, labels...).Update(te)
w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te) w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te)
if w.opts.LoggerEnabled { if w.opts.LoggerEnabled {
w.opts.Logger.Fields(w.opts.LoggerObserver(context.TODO(), "PrepareContext", name, td, err)...).Log(context.TODO(), w.opts.LoggerLevel) w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "PrepareContext", name, td, err)...).Log(ctx, w.opts.LoggerLevel)
} }
return &wrapperStmt{stmt: stmt, opts: w.opts, ctx: nctx}, nil return &wrapperStmt{stmt: stmt, opts: w.opts, ctx: nctx}, nil
} }
@ -206,13 +227,19 @@ func (w *wrapperConn) PrepareContext(ctx context.Context, query string) (driver.
w.opts.Meter.Summary(meterRequestLatencyMicroseconds, labels...).Update(te) w.opts.Meter.Summary(meterRequestLatencyMicroseconds, labels...).Update(te)
w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te) w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te)
if w.opts.LoggerEnabled { if w.opts.LoggerEnabled {
w.opts.Logger.Fields(w.opts.LoggerObserver(context.TODO(), "PrepareContext", name, td, err)...).Log(context.TODO(), w.opts.LoggerLevel) w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "PrepareContext", name, td, err)...).Log(ctx, w.opts.LoggerLevel)
} }
return stmt, nil return stmt, nil
} }
// Exec implements driver.Execer Exec // Exec implements driver.Execer Exec
func (w *wrapperConn) Exec(query string, args []driver.Value) (driver.Result, error) { func (w *wrapperConn) Exec(query string, args []driver.Value) (driver.Result, error) {
var ctx context.Context
if w.ctx != nil {
ctx = w.ctx
} else {
ctx = context.Background()
}
// nolint:staticcheck // nolint:staticcheck
labels := []string{labelMethod, "Exec", labelQuery, labelUnknown} labels := []string{labelMethod, "Exec", labelQuery, labelUnknown}
if execer, ok := w.conn.(driver.Execer); ok { if execer, ok := w.conn.(driver.Execer); ok {
@ -228,12 +255,12 @@ func (w *wrapperConn) Exec(query string, args []driver.Value) (driver.Result, er
w.opts.Meter.Summary(meterRequestLatencyMicroseconds, labels...).Update(te) w.opts.Meter.Summary(meterRequestLatencyMicroseconds, labels...).Update(te)
w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te) w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te)
if w.opts.LoggerEnabled { if w.opts.LoggerEnabled {
w.opts.Logger.Fields(w.opts.LoggerObserver(context.TODO(), "Exec", labelUnknown, td, err)...).Log(context.TODO(), w.opts.LoggerLevel) w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "Exec", labelUnknown, td, err)...).Log(ctx, w.opts.LoggerLevel)
} }
return res, err return res, err
} }
if w.opts.LoggerEnabled { if w.opts.LoggerEnabled {
w.opts.Logger.Fields(w.opts.LoggerObserver(context.TODO(), "Exec", labelUnknown, 0, ErrUnsupported)...).Log(context.TODO(), w.opts.LoggerLevel) w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "Exec", labelUnknown, 0, ErrUnsupported)...).Log(ctx, w.opts.LoggerLevel)
} }
return nil, ErrUnsupported return nil, ErrUnsupported
} }
@ -275,7 +302,7 @@ func (w *wrapperConn) ExecContext(ctx context.Context, query string, args []driv
w.opts.Meter.Summary(meterRequestLatencyMicroseconds, labels...).Update(te) w.opts.Meter.Summary(meterRequestLatencyMicroseconds, labels...).Update(te)
w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te) w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te)
if w.opts.LoggerEnabled { if w.opts.LoggerEnabled {
w.opts.Logger.Fields(w.opts.LoggerObserver(context.TODO(), "ExecContext", name, td, err)...).Log(context.TODO(), w.opts.LoggerLevel) w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "ExecContext", name, td, err)...).Log(ctx, w.opts.LoggerLevel)
} }
return res, err return res, err
} }
@ -284,7 +311,7 @@ func (w *wrapperConn) ExecContext(ctx context.Context, query string, args []driv
span.AddLabels("error", true) span.AddLabels("error", true)
span.AddLabels("err", err.Error()) span.AddLabels("err", err.Error())
if w.opts.LoggerEnabled { if w.opts.LoggerEnabled {
w.opts.Logger.Fields(w.opts.LoggerObserver(context.TODO(), "ExecContext", labelUnknown, 0, err)...).Log(context.TODO(), w.opts.LoggerLevel) w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "ExecContext", labelUnknown, 0, err)...).Log(ctx, w.opts.LoggerLevel)
} }
return nil, err return nil, err
} }
@ -303,7 +330,7 @@ func (w *wrapperConn) ExecContext(ctx context.Context, query string, args []driv
w.opts.Meter.Summary(meterRequestLatencyMicroseconds, labels...).Update(te) w.opts.Meter.Summary(meterRequestLatencyMicroseconds, labels...).Update(te)
w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te) w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te)
if w.opts.LoggerEnabled { if w.opts.LoggerEnabled {
w.opts.Logger.Fields(w.opts.LoggerObserver(context.TODO(), "ExecContext", name, td, err)...).Log(context.TODO(), w.opts.LoggerLevel) w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "ExecContext", name, td, err)...).Log(ctx, w.opts.LoggerLevel)
} }
return res, err return res, err
} }
@ -329,7 +356,7 @@ func (w *wrapperConn) Ping(ctx context.Context) error {
span.AddLabels("error", true) span.AddLabels("error", true)
span.AddLabels("err", err.Error()) span.AddLabels("err", err.Error())
if w.opts.LoggerEnabled { if w.opts.LoggerEnabled {
w.opts.Logger.Fields(w.opts.LoggerObserver(context.TODO(), "Ping", labelUnknown, td, err)...).Log(context.TODO(), w.opts.LoggerLevel) w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "Ping", labelUnknown, td, err)...).Log(ctx, w.opts.LoggerLevel)
} }
return err return err
} else { } else {
@ -339,13 +366,19 @@ func (w *wrapperConn) Ping(ctx context.Context) error {
w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te) w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te)
} }
if w.opts.LoggerEnabled { if w.opts.LoggerEnabled {
w.opts.Logger.Fields(w.opts.LoggerObserver(context.TODO(), "Ping", labelUnknown, 0, ErrUnsupported)...).Log(context.TODO(), w.opts.LoggerLevel) w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "Ping", labelUnknown, 0, ErrUnsupported)...).Log(ctx, w.opts.LoggerLevel)
} }
return ErrUnsupported return ErrUnsupported
} }
// Query implements driver.Queryer Query // Query implements driver.Queryer Query
func (w *wrapperConn) Query(query string, args []driver.Value) (driver.Rows, error) { func (w *wrapperConn) Query(query string, args []driver.Value) (driver.Rows, error) {
var ctx context.Context
if w.ctx != nil {
ctx = w.ctx
} else {
ctx = context.Background()
}
// nolint:staticcheck // nolint:staticcheck
if conn, ok := w.conn.(driver.Queryer); ok { if conn, ok := w.conn.(driver.Queryer); ok {
labels := []string{labelMethod, "Query", labelQuery, labelUnknown} labels := []string{labelMethod, "Query", labelQuery, labelUnknown}
@ -361,12 +394,12 @@ func (w *wrapperConn) Query(query string, args []driver.Value) (driver.Rows, err
w.opts.Meter.Summary(meterRequestLatencyMicroseconds, labels...).Update(te) w.opts.Meter.Summary(meterRequestLatencyMicroseconds, labels...).Update(te)
w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te) w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te)
if w.opts.LoggerEnabled { if w.opts.LoggerEnabled {
w.opts.Logger.Fields(w.opts.LoggerObserver(context.TODO(), "Query", labelUnknown, td, err)...).Log(context.TODO(), w.opts.LoggerLevel) w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "Query", labelUnknown, td, err)...).Log(ctx, w.opts.LoggerLevel)
} }
return rows, err return rows, err
} }
if w.opts.LoggerEnabled { if w.opts.LoggerEnabled {
w.opts.Logger.Fields(w.opts.LoggerObserver(context.TODO(), "Query", labelUnknown, 0, ErrUnsupported)...).Log(context.TODO(), w.opts.LoggerLevel) w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "Query", labelUnknown, 0, ErrUnsupported)...).Log(ctx, w.opts.LoggerLevel)
} }
return nil, ErrUnsupported return nil, ErrUnsupported
} }
@ -407,7 +440,7 @@ func (w *wrapperConn) QueryContext(ctx context.Context, query string, args []dri
w.opts.Meter.Summary(meterRequestLatencyMicroseconds, labels...).Update(te) w.opts.Meter.Summary(meterRequestLatencyMicroseconds, labels...).Update(te)
w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te) w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te)
if w.opts.LoggerEnabled { if w.opts.LoggerEnabled {
w.opts.Logger.Fields(w.opts.LoggerObserver(context.TODO(), "QueryContext", name, td, err)...).Log(context.TODO(), w.opts.LoggerLevel) w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "QueryContext", name, td, err)...).Log(ctx, w.opts.LoggerLevel)
} }
return rows, err return rows, err
} }
@ -416,7 +449,7 @@ func (w *wrapperConn) QueryContext(ctx context.Context, query string, args []dri
span.AddLabels("error", true) span.AddLabels("error", true)
span.AddLabels("err", err.Error()) span.AddLabels("err", err.Error())
if w.opts.LoggerEnabled { if w.opts.LoggerEnabled {
w.opts.Logger.Fields(w.opts.LoggerObserver(context.TODO(), "QueryContext", name, 0, err)...).Log(context.TODO(), w.opts.LoggerLevel) w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "QueryContext", name, 0, err)...).Log(ctx, w.opts.LoggerLevel)
} }
return nil, err return nil, err
} }
@ -435,7 +468,7 @@ func (w *wrapperConn) QueryContext(ctx context.Context, query string, args []dri
w.opts.Meter.Summary(meterRequestLatencyMicroseconds, labels...).Update(te) w.opts.Meter.Summary(meterRequestLatencyMicroseconds, labels...).Update(te)
w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te) w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te)
if w.opts.LoggerEnabled { if w.opts.LoggerEnabled {
w.opts.Logger.Fields(w.opts.LoggerObserver(context.TODO(), "QueryContext", name, td, err)...).Log(context.TODO(), w.opts.LoggerLevel) w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "QueryContext", name, td, err)...).Log(ctx, w.opts.LoggerLevel)
} }
return rows, err return rows, err
} }

View File

@ -10,11 +10,12 @@ import (
type wrapperDriver struct { type wrapperDriver struct {
driver driver.Driver driver driver.Driver
opts Options opts Options
ctx context.Context
} }
// NewWrapper creates and returns a new SQL driver with passed capabilities // NewWrapper creates and returns a new SQL driver with passed capabilities
func NewWrapper(d driver.Driver, opts ...Option) driver.Driver { func NewWrapper(d driver.Driver, opts ...Option) driver.Driver {
return &wrapperDriver{driver: d, opts: NewOptions(opts...)} return &wrapperDriver{driver: d, opts: NewOptions(opts...), ctx: context.Background()}
} }
// Open implements driver.Driver Open // Open implements driver.Driver Open
@ -24,12 +25,12 @@ func (w *wrapperDriver) Open(name string) (driver.Conn, error) {
td := time.Since(ts) td := time.Since(ts)
if w.opts.LoggerEnabled { if w.opts.LoggerEnabled {
w.opts.Logger.Fields(w.opts.LoggerObserver(context.TODO(), "Open", labelUnknown, td, err)...).Log(context.TODO(), w.opts.LoggerLevel) w.opts.Logger.Fields(w.opts.LoggerObserver(w.ctx, "Open", labelUnknown, td, err)...).Log(w.ctx, w.opts.LoggerLevel)
} }
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &wrapperConn{conn: c, opts: w.opts}, nil return &wrapperConn{conn: c, opts: w.opts, ctx: w.ctx}, nil
} }

36
stmt.go
View File

@ -20,6 +20,12 @@ type wrapperStmt struct {
// Close implements driver.Stmt Close // Close implements driver.Stmt Close
func (w *wrapperStmt) Close() error { func (w *wrapperStmt) Close() error {
var ctx context.Context
if w.ctx != nil {
ctx = w.ctx
} else {
ctx = context.Background()
}
labels := []string{labelMethod, "Close"} labels := []string{labelMethod, "Close"}
ts := time.Now() ts := time.Now()
err := w.stmt.Close() err := w.stmt.Close()
@ -34,7 +40,7 @@ func (w *wrapperStmt) Close() error {
w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te) w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te)
if w.opts.LoggerEnabled { if w.opts.LoggerEnabled {
w.opts.Logger.Fields(w.opts.LoggerObserver(context.TODO(), "Close", labelUnknown, td, err)...).Log(context.TODO(), w.opts.LoggerLevel) w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "Close", labelUnknown, td, err)...).Log(ctx, w.opts.LoggerLevel)
} }
return err return err
} }
@ -46,6 +52,12 @@ func (w *wrapperStmt) NumInput() int {
// Exec implements driver.Stmt Exec // Exec implements driver.Stmt Exec
func (w *wrapperStmt) Exec(args []driver.Value) (driver.Result, error) { func (w *wrapperStmt) Exec(args []driver.Value) (driver.Result, error) {
var ctx context.Context
if w.ctx != nil {
ctx = w.ctx
} else {
ctx = context.Background()
}
labels := []string{labelMethod, "Exec"} labels := []string{labelMethod, "Exec"}
ts := time.Now() ts := time.Now()
// nolint:staticcheck // nolint:staticcheck
@ -61,13 +73,19 @@ func (w *wrapperStmt) Exec(args []driver.Value) (driver.Result, error) {
w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te) w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te)
if w.opts.LoggerEnabled { if w.opts.LoggerEnabled {
w.opts.Logger.Fields(w.opts.LoggerObserver(context.TODO(), "Exec", labelUnknown, td, err)...).Log(context.TODO(), w.opts.LoggerLevel) w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "Exec", labelUnknown, td, err)...).Log(ctx, w.opts.LoggerLevel)
} }
return res, err return res, err
} }
// Query implements driver.Stmt Query // Query implements driver.Stmt Query
func (w *wrapperStmt) Query(args []driver.Value) (driver.Rows, error) { func (w *wrapperStmt) Query(args []driver.Value) (driver.Rows, error) {
var ctx context.Context
if w.ctx != nil {
ctx = w.ctx
} else {
ctx = context.Background()
}
labels := []string{labelMethod, "Query"} labels := []string{labelMethod, "Query"}
ts := time.Now() ts := time.Now()
// nolint:staticcheck // nolint:staticcheck
@ -83,7 +101,7 @@ func (w *wrapperStmt) Query(args []driver.Value) (driver.Rows, error) {
w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te) w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te)
if w.opts.LoggerEnabled { if w.opts.LoggerEnabled {
w.opts.Logger.Fields(w.opts.LoggerObserver(context.TODO(), "Query", labelUnknown, td, err)...).Log(context.TODO(), w.opts.LoggerLevel) w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "Query", labelUnknown, td, err)...).Log(ctx, w.opts.LoggerLevel)
} }
return rows, err return rows, err
} }
@ -125,7 +143,7 @@ func (w *wrapperStmt) ExecContext(ctx context.Context, query string, args []driv
w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te) w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te)
if w.opts.LoggerEnabled { if w.opts.LoggerEnabled {
w.opts.Logger.Fields(w.opts.LoggerObserver(context.TODO(), "ExecContext", name, td, err)...).Log(context.TODO(), w.opts.LoggerLevel) w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "ExecContext", name, td, err)...).Log(ctx, w.opts.LoggerLevel)
} }
return res, err return res, err
} }
@ -136,7 +154,7 @@ func (w *wrapperStmt) ExecContext(ctx context.Context, query string, args []driv
span.AddLabels("err", err.Error()) span.AddLabels("err", err.Error())
if w.opts.LoggerEnabled { if w.opts.LoggerEnabled {
w.opts.Logger.Fields(w.opts.LoggerObserver(context.TODO(), "ExecContext", name, 0, err)...).Log(context.TODO(), w.opts.LoggerLevel) w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "ExecContext", name, 0, err)...).Log(ctx, w.opts.LoggerLevel)
} }
return nil, err return nil, err
} }
@ -157,7 +175,7 @@ func (w *wrapperStmt) ExecContext(ctx context.Context, query string, args []driv
w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te) w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te)
if w.opts.LoggerEnabled { if w.opts.LoggerEnabled {
w.opts.Logger.Fields(w.opts.LoggerObserver(context.TODO(), "ExecContext", name, td, err)).Log(context.TODO(), w.opts.LoggerLevel) w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "ExecContext", name, td, err)).Log(ctx, w.opts.LoggerLevel)
} }
return res, err return res, err
} }
@ -200,7 +218,7 @@ func (w *wrapperStmt) QueryContext(ctx context.Context, query string, args []dri
w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te) w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te)
if w.opts.LoggerEnabled { if w.opts.LoggerEnabled {
w.opts.Logger.Fields(w.opts.LoggerObserver(context.TODO(), "QueryContext", name, td, err)).Log(context.TODO(), w.opts.LoggerLevel) w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "QueryContext", name, td, err)).Log(ctx, w.opts.LoggerLevel)
} }
return rows, err return rows, err
} }
@ -212,7 +230,7 @@ func (w *wrapperStmt) QueryContext(ctx context.Context, query string, args []dri
span.AddLabels("err", err.Error()) span.AddLabels("err", err.Error())
if w.opts.LoggerEnabled { if w.opts.LoggerEnabled {
w.opts.Logger.Fields(w.opts.LoggerObserver(context.TODO(), "QueryContext", name, 0, err)...).Log(context.TODO(), w.opts.LoggerLevel) w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "QueryContext", name, 0, err)...).Log(ctx, w.opts.LoggerLevel)
} }
return nil, err return nil, err
} }
@ -233,7 +251,7 @@ func (w *wrapperStmt) QueryContext(ctx context.Context, query string, args []dri
w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te) w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te)
if w.opts.LoggerEnabled { if w.opts.LoggerEnabled {
w.opts.Logger.Fields(w.opts.LoggerObserver(context.TODO(), "QueryContext", name, td, err)...).Log(context.TODO(), w.opts.LoggerLevel) w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "QueryContext", name, td, err)...).Log(ctx, w.opts.LoggerLevel)
} }
return rows, err return rows, err
} }

4
tx.go
View File

@ -34,7 +34,7 @@ func (w *wrapperTx) Commit() error {
} }
if w.opts.LoggerEnabled { if w.opts.LoggerEnabled {
w.opts.Logger.Fields(w.opts.LoggerObserver(w.ctx, "Commit", labelUnknown, td, err)...).Log(context.TODO(), w.opts.LoggerLevel) w.opts.Logger.Fields(w.opts.LoggerObserver(w.ctx, "Commit", labelUnknown, td, err)...).Log(w.ctx, w.opts.LoggerLevel)
} }
w.ctx = nil w.ctx = nil
@ -57,7 +57,7 @@ func (w *wrapperTx) Rollback() error {
} }
if w.opts.LoggerEnabled { if w.opts.LoggerEnabled {
w.opts.Logger.Fields(w.opts.LoggerObserver(w.ctx, "Rollback", labelUnknown, td, err)...).Log(context.TODO(), w.opts.LoggerLevel) w.opts.Logger.Fields(w.opts.LoggerObserver(w.ctx, "Rollback", labelUnknown, td, err)...).Log(w.ctx, w.opts.LoggerLevel)
} }
w.ctx = nil w.ctx = nil