commit
4e6a2d3a57
@ -24,7 +24,7 @@ func namedValueToValue(named []driver.NamedValue) ([]driver.Value, error) {
|
|||||||
|
|
||||||
// namedValueToLabels convert driver arguments to interface{} slice
|
// namedValueToLabels convert driver arguments to interface{} slice
|
||||||
func namedValueToLabels(named []driver.NamedValue) []interface{} {
|
func namedValueToLabels(named []driver.NamedValue) []interface{} {
|
||||||
largs := make([]interface{}, len(named)*2)
|
largs := make([]interface{}, 0, len(named)*2)
|
||||||
var name string
|
var name string
|
||||||
for _, param := range named {
|
for _, param := range named {
|
||||||
if param.Name != "" {
|
if param.Name != "" {
|
||||||
@ -32,8 +32,7 @@ func namedValueToLabels(named []driver.NamedValue) []interface{} {
|
|||||||
} else {
|
} else {
|
||||||
name = fmt.Sprintf("$%d", param.Ordinal)
|
name = fmt.Sprintf("$%d", param.Ordinal)
|
||||||
}
|
}
|
||||||
|
largs = append(largs, fmt.Sprintf("%s=%s", name, param.Value))
|
||||||
largs = append(largs, name, param.Value)
|
|
||||||
}
|
}
|
||||||
return largs
|
return largs
|
||||||
}
|
}
|
||||||
|
130
conn.go
130
conn.go
@ -5,16 +5,28 @@ import (
|
|||||||
"database/sql/driver"
|
"database/sql/driver"
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"go.unistack.org/micro/v3/tracer"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var _ driver.Conn = &wrapperConn{}
|
||||||
|
|
||||||
// wrapperConn defines a wrapper for driver.Conn
|
// wrapperConn defines a wrapper for driver.Conn
|
||||||
type wrapperConn struct {
|
type wrapperConn struct {
|
||||||
conn driver.Conn
|
conn driver.Conn
|
||||||
opts Options
|
opts Options
|
||||||
|
ctx context.Context
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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)
|
||||||
@ -26,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
|
||||||
}
|
}
|
||||||
@ -35,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()
|
||||||
@ -57,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
|
||||||
@ -65,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
|
||||||
@ -76,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
|
||||||
}
|
}
|
||||||
@ -84,14 +110,14 @@ 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
|
||||||
func (w *wrapperConn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error) {
|
func (w *wrapperConn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error) {
|
||||||
nctx, span := w.opts.Tracer.Start(ctx, "BeginTx")
|
nctx, span := w.opts.Tracer.Start(ctx, "Transaction")
|
||||||
span.AddLabels("method", "BeginTx")
|
span.AddLabels("method", "BeginTx")
|
||||||
name := getQueryName(ctx)
|
name := getQueryName(ctx)
|
||||||
if name != "" {
|
if name != "" {
|
||||||
@ -112,15 +138,16 @@ 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)
|
||||||
}
|
}
|
||||||
return &wrapperTx{tx: tx, opts: w.opts, span: span}, nil
|
w.ctx = nctx
|
||||||
|
return &wrapperTx{ctx: ctx, tx: tx, opts: w.opts, span: span, conn: w}, nil
|
||||||
}
|
}
|
||||||
ts := time.Now()
|
ts := time.Now()
|
||||||
// nolint:staticcheck
|
// nolint:staticcheck
|
||||||
@ -138,14 +165,21 @@ 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)
|
||||||
}
|
}
|
||||||
return tx, nil
|
w.ctx = nctx
|
||||||
|
return &wrapperTx{ctx: ctx, tx: tx, opts: w.opts, span: span, conn: w}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// PrepareContext implements driver.ConnPrepareContext PrepareContext
|
// PrepareContext implements driver.ConnPrepareContext PrepareContext
|
||||||
func (w *wrapperConn) PrepareContext(ctx context.Context, query string) (driver.Stmt, error) {
|
func (w *wrapperConn) PrepareContext(ctx context.Context, query string) (driver.Stmt, error) {
|
||||||
nctx, span := w.opts.Tracer.Start(ctx, "PrepareContext")
|
var nctx context.Context
|
||||||
|
var span tracer.Span
|
||||||
|
if w.ctx != nil {
|
||||||
|
nctx, span = w.opts.Tracer.Start(w.ctx, "PrepareContext")
|
||||||
|
} else {
|
||||||
|
nctx, span = w.opts.Tracer.Start(ctx, "PrepareContext")
|
||||||
|
}
|
||||||
span.AddLabels("method", "PrepareContext")
|
span.AddLabels("method", "PrepareContext")
|
||||||
name := getQueryName(ctx)
|
name := getQueryName(ctx)
|
||||||
if name != "" {
|
if name != "" {
|
||||||
@ -166,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
|
||||||
}
|
}
|
||||||
@ -174,9 +208,9 @@ 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}, nil
|
return &wrapperStmt{stmt: stmt, opts: w.opts, ctx: nctx}, nil
|
||||||
}
|
}
|
||||||
ts := time.Now()
|
ts := time.Now()
|
||||||
stmt, err := w.conn.Prepare(query)
|
stmt, err := w.conn.Prepare(query)
|
||||||
@ -193,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 {
|
||||||
@ -215,19 +255,25 @@ 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
|
||||||
}
|
}
|
||||||
|
|
||||||
// Exec implements driver.StmtExecContext ExecContext
|
// Exec implements driver.StmtExecContext ExecContext
|
||||||
func (w *wrapperConn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) {
|
func (w *wrapperConn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) {
|
||||||
nctx, span := w.opts.Tracer.Start(ctx, "ExecContext")
|
var nctx context.Context
|
||||||
|
var span tracer.Span
|
||||||
|
if w.ctx != nil {
|
||||||
|
nctx, span = w.opts.Tracer.Start(w.ctx, "ExecContext")
|
||||||
|
} else {
|
||||||
|
nctx, span = w.opts.Tracer.Start(ctx, "ExecContext")
|
||||||
|
}
|
||||||
span.AddLabels("method", "ExecContext")
|
span.AddLabels("method", "ExecContext")
|
||||||
name := getQueryName(ctx)
|
name := getQueryName(ctx)
|
||||||
if name != "" {
|
if name != "" {
|
||||||
@ -256,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
|
||||||
}
|
}
|
||||||
@ -265,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
|
||||||
}
|
}
|
||||||
@ -284,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
|
||||||
}
|
}
|
||||||
@ -292,7 +338,13 @@ func (w *wrapperConn) ExecContext(ctx context.Context, query string, args []driv
|
|||||||
// Ping implements driver.Pinger Ping
|
// Ping implements driver.Pinger Ping
|
||||||
func (w *wrapperConn) Ping(ctx context.Context) error {
|
func (w *wrapperConn) Ping(ctx context.Context) error {
|
||||||
if conn, ok := w.conn.(driver.Pinger); ok {
|
if conn, ok := w.conn.(driver.Pinger); ok {
|
||||||
nctx, span := w.opts.Tracer.Start(ctx, "Ping")
|
var nctx context.Context
|
||||||
|
var span tracer.Span
|
||||||
|
if w.ctx != nil {
|
||||||
|
nctx, span = w.opts.Tracer.Start(w.ctx, "Ping")
|
||||||
|
} else {
|
||||||
|
nctx, span = w.opts.Tracer.Start(ctx, "Ping")
|
||||||
|
}
|
||||||
defer span.Finish()
|
defer span.Finish()
|
||||||
labels := []string{labelMethod, "Ping"}
|
labels := []string{labelMethod, "Ping"}
|
||||||
ts := time.Now()
|
ts := time.Now()
|
||||||
@ -304,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 {
|
||||||
@ -314,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}
|
||||||
@ -336,19 +394,25 @@ 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
|
||||||
}
|
}
|
||||||
|
|
||||||
// QueryContext implements Driver.QueryerContext QueryContext
|
// QueryContext implements Driver.QueryerContext QueryContext
|
||||||
func (w *wrapperConn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {
|
func (w *wrapperConn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {
|
||||||
nctx, span := w.opts.Tracer.Start(ctx, "QueryContext")
|
var nctx context.Context
|
||||||
|
var span tracer.Span
|
||||||
|
if w.ctx != nil {
|
||||||
|
nctx, span = w.opts.Tracer.Start(w.ctx, "QueryContext")
|
||||||
|
} else {
|
||||||
|
nctx, span = w.opts.Tracer.Start(ctx, "QueryContext")
|
||||||
|
}
|
||||||
span.AddLabels("method", "QueryContext")
|
span.AddLabels("method", "QueryContext")
|
||||||
name := getQueryName(ctx)
|
name := getQueryName(ctx)
|
||||||
if name != "" {
|
if name != "" {
|
||||||
@ -376,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
|
||||||
}
|
}
|
||||||
@ -385,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
|
||||||
}
|
}
|
||||||
@ -404,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
|
||||||
}
|
}
|
||||||
|
@ -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
|
||||||
}
|
}
|
||||||
|
57
stmt.go
57
stmt.go
@ -5,16 +5,27 @@ import (
|
|||||||
"database/sql/driver"
|
"database/sql/driver"
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"go.unistack.org/micro/v3/tracer"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var _ driver.Stmt = &wrapperStmt{}
|
||||||
|
|
||||||
// wrapperStmt defines a wrapper for driver.Stmt
|
// wrapperStmt defines a wrapper for driver.Stmt
|
||||||
type wrapperStmt struct {
|
type wrapperStmt struct {
|
||||||
stmt driver.Stmt
|
stmt driver.Stmt
|
||||||
opts Options
|
opts Options
|
||||||
|
ctx context.Context
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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()
|
||||||
@ -29,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
|
||||||
}
|
}
|
||||||
@ -41,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
|
||||||
@ -56,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
|
||||||
@ -78,14 +101,20 @@ 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
|
||||||
}
|
}
|
||||||
|
|
||||||
// ExecContext implements driver.ExecerContext ExecContext
|
// ExecContext implements driver.ExecerContext ExecContext
|
||||||
func (w *wrapperStmt) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) {
|
func (w *wrapperStmt) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) {
|
||||||
nctx, span := w.opts.Tracer.Start(ctx, "ExecContext")
|
var nctx context.Context
|
||||||
|
var span tracer.Span
|
||||||
|
if w.ctx != nil {
|
||||||
|
nctx, span = w.opts.Tracer.Start(w.ctx, "ExecContext")
|
||||||
|
} else {
|
||||||
|
nctx, span = w.opts.Tracer.Start(ctx, "ExecContext")
|
||||||
|
}
|
||||||
span.AddLabels("method", "ExecContext")
|
span.AddLabels("method", "ExecContext")
|
||||||
name := getQueryName(ctx)
|
name := getQueryName(ctx)
|
||||||
if name != "" {
|
if name != "" {
|
||||||
@ -114,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
|
||||||
}
|
}
|
||||||
@ -125,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
|
||||||
}
|
}
|
||||||
@ -146,14 +175,20 @@ 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
|
||||||
}
|
}
|
||||||
|
|
||||||
// QueryContext implements Driver.QueryerContext QueryContext
|
// QueryContext implements Driver.QueryerContext QueryContext
|
||||||
func (w *wrapperStmt) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {
|
func (w *wrapperStmt) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {
|
||||||
nctx, span := w.opts.Tracer.Start(ctx, "QueryContext")
|
var nctx context.Context
|
||||||
|
var span tracer.Span
|
||||||
|
if w.ctx != nil {
|
||||||
|
nctx, span = w.opts.Tracer.Start(w.ctx, "QueryContext")
|
||||||
|
} else {
|
||||||
|
nctx, span = w.opts.Tracer.Start(ctx, "QueryContext")
|
||||||
|
}
|
||||||
span.AddLabels("method", "QueryContext")
|
span.AddLabels("method", "QueryContext")
|
||||||
name := getQueryName(ctx)
|
name := getQueryName(ctx)
|
||||||
if name != "" {
|
if name != "" {
|
||||||
@ -183,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
|
||||||
}
|
}
|
||||||
@ -195,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
|
||||||
}
|
}
|
||||||
@ -216,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
|
||||||
}
|
}
|
||||||
|
26
tx.go
26
tx.go
@ -8,11 +8,15 @@ import (
|
|||||||
"go.unistack.org/micro/v3/tracer"
|
"go.unistack.org/micro/v3/tracer"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var _ driver.Tx = &wrapperTx{}
|
||||||
|
|
||||||
// wrapperTx defines a wrapper for driver.Tx
|
// wrapperTx defines a wrapper for driver.Tx
|
||||||
type wrapperTx struct {
|
type wrapperTx struct {
|
||||||
tx driver.Tx
|
tx driver.Tx
|
||||||
span tracer.Span
|
span tracer.Span
|
||||||
opts Options
|
opts Options
|
||||||
|
conn *wrapperConn
|
||||||
|
ctx context.Context
|
||||||
}
|
}
|
||||||
|
|
||||||
// Commit implements driver.Tx Commit
|
// Commit implements driver.Tx Commit
|
||||||
@ -24,10 +28,17 @@ func (w *wrapperTx) Commit() error {
|
|||||||
err := w.tx.Commit()
|
err := w.tx.Commit()
|
||||||
td := time.Since(ts)
|
td := time.Since(ts)
|
||||||
|
|
||||||
if w.opts.LoggerEnabled {
|
if err != nil {
|
||||||
w.opts.Logger.Fields(w.opts.LoggerObserver(context.TODO(), "Commit", labelUnknown, td, err)...).Log(context.TODO(), w.opts.LoggerLevel)
|
w.span.AddLabels("error", true)
|
||||||
|
w.span.AddLabels("err", err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if w.opts.LoggerEnabled {
|
||||||
|
w.opts.Logger.Fields(w.opts.LoggerObserver(w.ctx, "Commit", labelUnknown, td, err)...).Log(w.ctx, w.opts.LoggerLevel)
|
||||||
|
}
|
||||||
|
|
||||||
|
w.ctx = nil
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -40,9 +51,16 @@ func (w *wrapperTx) Rollback() error {
|
|||||||
err := w.tx.Rollback()
|
err := w.tx.Rollback()
|
||||||
td := time.Since(ts)
|
td := time.Since(ts)
|
||||||
|
|
||||||
if w.opts.LoggerEnabled {
|
if err != nil {
|
||||||
w.opts.Logger.Fields(w.opts.LoggerObserver(context.TODO(), "Rollback", labelUnknown, td, err)...).Log(context.TODO(), w.opts.LoggerLevel)
|
w.span.AddLabels("error", true)
|
||||||
|
w.span.AddLabels("err", err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if w.opts.LoggerEnabled {
|
||||||
|
w.opts.Logger.Fields(w.opts.LoggerObserver(w.ctx, "Rollback", labelUnknown, td, err)...).Log(w.ctx, w.opts.LoggerLevel)
|
||||||
|
}
|
||||||
|
|
||||||
|
w.ctx = nil
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user