use generated interface
Some checks failed
dependabot-automerge / automerge (pull_request) Has been skipped
autoapprove / autoapprove (pull_request) Failing after 4s
automerge / automerge (pull_request) Failing after 4s
codeql / analyze (go) (pull_request) Failing after 6s
prbuild / test (pull_request) Failing after 5s
prbuild / lint (pull_request) Failing after 6s
Some checks failed
dependabot-automerge / automerge (pull_request) Has been skipped
autoapprove / autoapprove (pull_request) Failing after 4s
automerge / automerge (pull_request) Failing after 4s
codeql / analyze (go) (pull_request) Failing after 6s
prbuild / test (pull_request) Failing after 5s
prbuild / lint (pull_request) Failing after 6s
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
parent
75f272cb84
commit
dbce4a1d9e
@ -6,6 +6,8 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
//go:generate sh -c "go run gen.go > wrap_gen.go"
|
||||||
|
|
||||||
// namedValueToValue converts driver arguments of NamedValue format to Value format. Implemented in the same way as in
|
// namedValueToValue converts driver arguments of NamedValue format to Value format. Implemented in the same way as in
|
||||||
// database/sql ctxutil.go.
|
// database/sql ctxutil.go.
|
||||||
func namedValueToValue(named []driver.NamedValue) ([]driver.Value, error) {
|
func namedValueToValue(named []driver.NamedValue) ([]driver.Value, error) {
|
||||||
|
254
conn.go
254
conn.go
@ -6,10 +6,23 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"go.unistack.org/micro/v3/tracer"
|
"go.unistack.org/micro/v4/tracer"
|
||||||
)
|
)
|
||||||
|
|
||||||
var _ driver.Conn = &wrapperConn{}
|
var (
|
||||||
|
_ driver.Conn = (*wrapperConn)(nil)
|
||||||
|
_ driver.ConnBeginTx = (*wrapperConn)(nil)
|
||||||
|
_ driver.ConnPrepareContext = (*wrapperConn)(nil)
|
||||||
|
_ driver.Pinger = (*wrapperConn)(nil)
|
||||||
|
_ driver.Validator = (*wrapperConn)(nil)
|
||||||
|
_ driver.Queryer = (*wrapperConn)(nil)
|
||||||
|
_ driver.QueryerContext = (*wrapperConn)(nil)
|
||||||
|
_ driver.Execer = (*wrapperConn)(nil)
|
||||||
|
_ driver.ExecerContext = (*wrapperConn)(nil)
|
||||||
|
// _ driver.Connector
|
||||||
|
// _ driver.Driver
|
||||||
|
// _ driver.DriverContext
|
||||||
|
)
|
||||||
|
|
||||||
// wrapperConn defines a wrapper for driver.Conn
|
// wrapperConn defines a wrapper for driver.Conn
|
||||||
type wrapperConn struct {
|
type wrapperConn struct {
|
||||||
@ -18,41 +31,7 @@ type wrapperConn struct {
|
|||||||
conn driver.Conn
|
conn driver.Conn
|
||||||
opts Options
|
opts Options
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
}
|
span tracer.Span
|
||||||
|
|
||||||
// Prepare implements driver.Conn Prepare
|
|
||||||
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}
|
|
||||||
ts := time.Now()
|
|
||||||
stmt, err := w.conn.Prepare(query)
|
|
||||||
td := time.Since(ts)
|
|
||||||
te := td.Seconds()
|
|
||||||
if err != nil {
|
|
||||||
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelFailure)...).Inc()
|
|
||||||
w.opts.Meter.Summary(meterRequestLatencyMicroseconds, labels...).Update(te)
|
|
||||||
w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te)
|
|
||||||
|
|
||||||
if w.opts.LoggerEnabled {
|
|
||||||
w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "Prepare", labelUnknown, td, err)...).Log(ctx, w.opts.LoggerLevel)
|
|
||||||
}
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelSuccess)...).Inc()
|
|
||||||
w.opts.Meter.Summary(meterRequestLatencyMicroseconds, labels...).Update(te)
|
|
||||||
w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te)
|
|
||||||
|
|
||||||
if w.opts.LoggerEnabled {
|
|
||||||
w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "Prepare", labelUnknown, td, err)...).Log(ctx, w.opts.LoggerLevel)
|
|
||||||
}
|
|
||||||
|
|
||||||
return &wrapperStmt{stmt: stmt, opts: w.opts, ctx: ctx}, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close implements driver.Conn Close
|
// Close implements driver.Conn Close
|
||||||
@ -77,7 +56,7 @@ func (w *wrapperConn) Close() 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.V(w.opts.LoggerLevel) {
|
||||||
w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "Close", labelUnknown, td, err)...).Log(ctx, w.opts.LoggerLevel)
|
w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "Close", labelUnknown, td, err)...).Log(ctx, w.opts.LoggerLevel)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -95,15 +74,14 @@ func (w *wrapperConn) Begin() (driver.Tx, error) {
|
|||||||
|
|
||||||
labels := []string{labelMethod, "Begin"}
|
labels := []string{labelMethod, "Begin"}
|
||||||
ts := time.Now()
|
ts := time.Now()
|
||||||
// nolint:staticcheck
|
tx, err := w.conn.Begin() // nolint:staticcheck
|
||||||
tx, err := w.conn.Begin()
|
|
||||||
td := time.Since(ts)
|
td := time.Since(ts)
|
||||||
te := td.Seconds()
|
te := td.Seconds()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelFailure)...).Inc()
|
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelFailure)...).Inc()
|
||||||
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.V(w.opts.LoggerLevel) {
|
||||||
w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "Begin", labelUnknown, td, err)...).Log(ctx, 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
|
||||||
@ -111,9 +89,10 @@ func (w *wrapperConn) Begin() (driver.Tx, error) {
|
|||||||
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelSuccess)...).Inc()
|
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelSuccess)...).Inc()
|
||||||
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.V(w.opts.LoggerLevel) {
|
||||||
w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "Begin", labelUnknown, td, err)...).Log(ctx, 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, ctx: ctx}, nil
|
return &wrapperTx{tx: tx, opts: w.opts, ctx: ctx}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -128,7 +107,12 @@ func (w *wrapperConn) BeginTx(ctx context.Context, opts driver.TxOptions) (drive
|
|||||||
name = labelUnknown
|
name = labelUnknown
|
||||||
}
|
}
|
||||||
labels := []string{labelMethod, "BeginTx", labelQuery, name}
|
labels := []string{labelMethod, "BeginTx", labelQuery, name}
|
||||||
if connBeginTx, ok := w.conn.(driver.ConnBeginTx); ok {
|
|
||||||
|
connBeginTx, ok := w.conn.(driver.ConnBeginTx)
|
||||||
|
if !ok {
|
||||||
|
return w.Begin()
|
||||||
|
}
|
||||||
|
|
||||||
ts := time.Now()
|
ts := time.Now()
|
||||||
tx, err := connBeginTx.BeginTx(nctx, opts)
|
tx, err := connBeginTx.BeginTx(nctx, opts)
|
||||||
td := time.Since(ts)
|
td := time.Since(ts)
|
||||||
@ -139,38 +123,51 @@ func (w *wrapperConn) BeginTx(ctx context.Context, opts driver.TxOptions) (drive
|
|||||||
w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te)
|
w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te)
|
||||||
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.V(w.opts.LoggerLevel) {
|
||||||
w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "BeginTx", name, td, err)...).Log(ctx, 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.V(w.opts.LoggerLevel) {
|
||||||
w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "BeginTx", name, td, err)...).Log(ctx, w.opts.LoggerLevel)
|
w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "BeginTx", name, td, err)...).Log(ctx, w.opts.LoggerLevel)
|
||||||
}
|
}
|
||||||
w.ctx = nctx
|
return &wrapperTx{tx: tx, opts: w.opts, ctx: ctx, span: span}, nil
|
||||||
return &wrapperTx{ctx: ctx, tx: tx, opts: w.opts, span: span, conn: w}, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Prepare implements driver.Conn Prepare
|
||||||
|
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}
|
||||||
ts := time.Now()
|
ts := time.Now()
|
||||||
// nolint:staticcheck
|
stmt, err := w.conn.Prepare(query)
|
||||||
tx, err := w.conn.Begin()
|
|
||||||
td := time.Since(ts)
|
td := time.Since(ts)
|
||||||
te := td.Seconds()
|
te := td.Seconds()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelFailure)...).Inc()
|
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelFailure)...).Inc()
|
||||||
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)
|
||||||
span.AddLabels("error", true)
|
|
||||||
span.AddLabels("err", err.Error())
|
if w.opts.LoggerEnabled && w.opts.Logger.V(w.opts.LoggerLevel) {
|
||||||
|
w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "Prepare", labelUnknown, td, err)...).Log(ctx, w.opts.LoggerLevel)
|
||||||
|
}
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelSuccess)...).Inc()
|
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelSuccess)...).Inc()
|
||||||
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 {
|
|
||||||
w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "BeginTx", name, td, err)...).Log(ctx, w.opts.LoggerLevel)
|
if w.opts.LoggerEnabled && w.opts.Logger.V(w.opts.LoggerLevel) {
|
||||||
|
w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "Prepare", labelUnknown, td, err)...).Log(ctx, w.opts.LoggerLevel)
|
||||||
}
|
}
|
||||||
w.ctx = nctx
|
|
||||||
return &wrapperTx{ctx: ctx, tx: tx, opts: w.opts, span: span, conn: w}, nil
|
return wrapStmt(stmt, query, w.opts), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// PrepareContext implements driver.ConnPrepareContext PrepareContext
|
// PrepareContext implements driver.ConnPrepareContext PrepareContext
|
||||||
@ -189,8 +186,13 @@ func (w *wrapperConn) PrepareContext(ctx context.Context, query string) (driver.
|
|||||||
} else {
|
} else {
|
||||||
name = labelUnknown
|
name = labelUnknown
|
||||||
}
|
}
|
||||||
|
|
||||||
labels := []string{labelMethod, "PrepareContext", labelQuery, name}
|
labels := []string{labelMethod, "PrepareContext", labelQuery, name}
|
||||||
if conn, ok := w.conn.(driver.ConnPrepareContext); ok {
|
conn, ok := w.conn.(driver.ConnPrepareContext)
|
||||||
|
if !ok {
|
||||||
|
return w.Prepare(query)
|
||||||
|
}
|
||||||
|
|
||||||
ts := time.Now()
|
ts := time.Now()
|
||||||
stmt, err := conn.PrepareContext(nctx, query)
|
stmt, err := conn.PrepareContext(nctx, query)
|
||||||
td := time.Since(ts)
|
td := time.Since(ts)
|
||||||
@ -201,7 +203,7 @@ func (w *wrapperConn) PrepareContext(ctx context.Context, query string) (driver.
|
|||||||
w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te)
|
w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te)
|
||||||
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.V(w.opts.LoggerLevel) {
|
||||||
w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "PrepareContext", name, td, err)...).Log(ctx, 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
|
||||||
@ -209,29 +211,11 @@ func (w *wrapperConn) PrepareContext(ctx context.Context, query string) (driver.
|
|||||||
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelSuccess)...).Inc()
|
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelSuccess)...).Inc()
|
||||||
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.V(w.opts.LoggerLevel) {
|
||||||
w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "PrepareContext", name, td, err)...).Log(ctx, 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 wrapStmt(stmt, query, w.opts), nil
|
||||||
ts := time.Now()
|
|
||||||
stmt, err := w.conn.Prepare(query)
|
|
||||||
td := time.Since(ts)
|
|
||||||
te := td.Seconds()
|
|
||||||
if err != nil {
|
|
||||||
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelFailure)...).Inc()
|
|
||||||
w.opts.Meter.Summary(meterRequestLatencyMicroseconds, labels...).Update(te)
|
|
||||||
w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te)
|
|
||||||
span.AddLabels("error", true)
|
|
||||||
span.AddLabels("err", err.Error())
|
|
||||||
}
|
|
||||||
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelSuccess)...).Inc()
|
|
||||||
w.opts.Meter.Summary(meterRequestLatencyMicroseconds, labels...).Update(te)
|
|
||||||
w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te)
|
|
||||||
if w.opts.LoggerEnabled {
|
|
||||||
w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "PrepareContext", name, td, err)...).Log(ctx, w.opts.LoggerLevel)
|
|
||||||
}
|
|
||||||
return stmt, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Exec implements driver.Execer Exec
|
// Exec implements driver.Execer Exec
|
||||||
@ -242,15 +226,16 @@ func (w *wrapperConn) Exec(query string, args []driver.Value) (driver.Result, er
|
|||||||
} else {
|
} else {
|
||||||
ctx = context.Background()
|
ctx = context.Background()
|
||||||
}
|
}
|
||||||
|
labels := []string{labelMethod, "Exec", labelQuery, labelUnknown}
|
||||||
|
|
||||||
// nolint:staticcheck
|
// nolint:staticcheck
|
||||||
execer, ok := w.conn.(driver.Execer)
|
conn, ok := w.conn.(driver.Execer)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, driver.ErrSkip
|
return nil, driver.ErrSkip
|
||||||
}
|
}
|
||||||
|
|
||||||
labels := []string{labelMethod, "Exec", labelQuery, labelUnknown}
|
|
||||||
ts := time.Now()
|
ts := time.Now()
|
||||||
res, err := execer.Exec(query, args)
|
res, err := conn.Exec(query, args)
|
||||||
td := time.Since(ts)
|
td := time.Since(ts)
|
||||||
te := td.Seconds()
|
te := td.Seconds()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -260,7 +245,7 @@ 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.V(w.opts.LoggerLevel) {
|
||||||
w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "Exec", labelUnknown, td, err)...).Log(ctx, 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
|
||||||
@ -270,6 +255,7 @@ func (w *wrapperConn) Exec(query string, args []driver.Value) (driver.Result, er
|
|||||||
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) {
|
||||||
var nctx context.Context
|
var nctx context.Context
|
||||||
var span tracer.Span
|
var span tracer.Span
|
||||||
|
|
||||||
if w.ctx != nil {
|
if w.ctx != nil {
|
||||||
nctx, span = w.opts.Tracer.Start(w.ctx, "ExecContext")
|
nctx, span = w.opts.Tracer.Start(w.ctx, "ExecContext")
|
||||||
} else {
|
} else {
|
||||||
@ -287,7 +273,14 @@ func (w *wrapperConn) ExecContext(ctx context.Context, query string, args []driv
|
|||||||
span.AddLabels("args", fmt.Sprintf("%v", namedValueToLabels(args)))
|
span.AddLabels("args", fmt.Sprintf("%v", namedValueToLabels(args)))
|
||||||
}
|
}
|
||||||
labels := []string{labelMethod, "ExecContext", labelQuery, name}
|
labels := []string{labelMethod, "ExecContext", labelQuery, name}
|
||||||
if conn, ok := w.conn.(driver.ExecerContext); ok {
|
fmt.Printf("EXECCONTETX args %#+v\n", args)
|
||||||
|
|
||||||
|
conn, ok := w.conn.(driver.ExecerContext)
|
||||||
|
if !ok {
|
||||||
|
// nolint:staticcheck
|
||||||
|
return nil, driver.ErrSkip
|
||||||
|
}
|
||||||
|
|
||||||
ts := time.Now()
|
ts := time.Now()
|
||||||
res, err := conn.ExecContext(nctx, query, args)
|
res, err := conn.ExecContext(nctx, query, args)
|
||||||
td := time.Since(ts)
|
td := time.Since(ts)
|
||||||
@ -302,49 +295,24 @@ func (w *wrapperConn) ExecContext(ctx context.Context, query string, args []driv
|
|||||||
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelSuccess)...).Inc()
|
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelSuccess)...).Inc()
|
||||||
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.V(w.opts.LoggerLevel) {
|
||||||
w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "ExecContext", name, td, err)...).Log(ctx, w.opts.LoggerLevel)
|
|
||||||
}
|
|
||||||
return res, err
|
|
||||||
}
|
|
||||||
values, err := namedValueToValue(args)
|
|
||||||
if err != nil {
|
|
||||||
span.AddLabels("error", true)
|
|
||||||
span.AddLabels("err", err.Error())
|
|
||||||
if w.opts.LoggerEnabled {
|
|
||||||
w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "ExecContext", labelUnknown, 0, err)...).Log(ctx, w.opts.LoggerLevel)
|
|
||||||
}
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
ts := time.Now()
|
|
||||||
// nolint:staticcheck
|
|
||||||
res, err := w.Exec(query, values)
|
|
||||||
td := time.Since(ts)
|
|
||||||
te := td.Seconds()
|
|
||||||
if err != nil {
|
|
||||||
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelFailure)...).Inc()
|
|
||||||
span.AddLabels("error", true)
|
|
||||||
span.AddLabels("err", err.Error())
|
|
||||||
} else {
|
|
||||||
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelSuccess)...).Inc()
|
|
||||||
}
|
|
||||||
w.opts.Meter.Summary(meterRequestLatencyMicroseconds, labels...).Update(te)
|
|
||||||
w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te)
|
|
||||||
if w.opts.LoggerEnabled {
|
|
||||||
w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "ExecContext", name, td, err)...).Log(ctx, 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
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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 {
|
||||||
conn, ok := w.conn.(driver.Pinger)
|
conn, ok := w.conn.(driver.Pinger)
|
||||||
|
|
||||||
if !ok {
|
if !ok {
|
||||||
wc, err := w.d.Open(w.dname)
|
// fallback path to check db alive
|
||||||
|
pc, err := w.d.Open(w.dname)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return wc.Close()
|
return pc.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
var nctx context.Context
|
var nctx context.Context
|
||||||
@ -364,7 +332,7 @@ func (w *wrapperConn) Ping(ctx context.Context) error {
|
|||||||
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelFailure)...).Inc()
|
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelFailure)...).Inc()
|
||||||
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.V(w.opts.LoggerLevel) {
|
||||||
w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "Ping", labelUnknown, td, err)...).Log(ctx, w.opts.LoggerLevel)
|
w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "Ping", labelUnknown, td, err)...).Log(ctx, w.opts.LoggerLevel)
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
@ -385,7 +353,6 @@ func (w *wrapperConn) Query(query string, args []driver.Value) (driver.Rows, err
|
|||||||
} else {
|
} else {
|
||||||
ctx = context.Background()
|
ctx = context.Background()
|
||||||
}
|
}
|
||||||
|
|
||||||
// nolint:staticcheck
|
// nolint:staticcheck
|
||||||
conn, ok := w.conn.(driver.Queryer)
|
conn, ok := w.conn.(driver.Queryer)
|
||||||
if !ok {
|
if !ok {
|
||||||
@ -404,7 +371,7 @@ 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.V(w.opts.LoggerLevel) {
|
||||||
w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "Query", labelUnknown, td, err)...).Log(ctx, 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
|
||||||
@ -431,7 +398,11 @@ func (w *wrapperConn) QueryContext(ctx context.Context, query string, args []dri
|
|||||||
span.AddLabels("args", fmt.Sprintf("%v", namedValueToLabels(args)))
|
span.AddLabels("args", fmt.Sprintf("%v", namedValueToLabels(args)))
|
||||||
}
|
}
|
||||||
labels := []string{labelMethod, "QueryContext", labelQuery, name}
|
labels := []string{labelMethod, "QueryContext", labelQuery, name}
|
||||||
if conn, ok := w.conn.(driver.QueryerContext); ok {
|
conn, ok := w.conn.(driver.QueryerContext)
|
||||||
|
if !ok {
|
||||||
|
return nil, driver.ErrSkip
|
||||||
|
}
|
||||||
|
|
||||||
ts := time.Now()
|
ts := time.Now()
|
||||||
rows, err := conn.QueryContext(nctx, query, args)
|
rows, err := conn.QueryContext(nctx, query, args)
|
||||||
td := time.Since(ts)
|
td := time.Since(ts)
|
||||||
@ -445,36 +416,35 @@ 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.V(w.opts.LoggerLevel) {
|
||||||
w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "QueryContext", name, td, err)...).Log(ctx, 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
|
||||||
}
|
}
|
||||||
values, err := namedValueToValue(args)
|
|
||||||
if err != nil {
|
// CheckNamedValue implements driver.NamedValueChecker
|
||||||
span.AddLabels("error", true)
|
func (w *wrapperConn) CheckNamedValue(v *driver.NamedValue) error {
|
||||||
span.AddLabels("err", err.Error())
|
s, ok := w.conn.(driver.NamedValueChecker)
|
||||||
if w.opts.LoggerEnabled {
|
if !ok {
|
||||||
w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "QueryContext", name, 0, err)...).Log(ctx, w.opts.LoggerLevel)
|
return driver.ErrSkip
|
||||||
}
|
}
|
||||||
return nil, err
|
return s.CheckNamedValue(v)
|
||||||
}
|
}
|
||||||
ts := time.Now()
|
|
||||||
// nolint:staticcheck
|
// IsValid implements driver.Validator
|
||||||
rows, err := w.Query(query, values)
|
func (w *wrapperConn) IsValid() bool {
|
||||||
td := time.Since(ts)
|
v, ok := w.conn.(driver.Validator)
|
||||||
te := td.Seconds()
|
if !ok {
|
||||||
if err != nil {
|
return w.conn != nil
|
||||||
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelFailure)...).Inc()
|
|
||||||
span.AddLabels("error", true)
|
|
||||||
span.AddLabels("err", err.Error())
|
|
||||||
} else {
|
|
||||||
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelSuccess)...).Inc()
|
|
||||||
}
|
}
|
||||||
w.opts.Meter.Summary(meterRequestLatencyMicroseconds, labels...).Update(te)
|
return v.IsValid()
|
||||||
w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te)
|
|
||||||
if w.opts.LoggerEnabled {
|
|
||||||
w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "QueryContext", name, td, err)...).Log(ctx, w.opts.LoggerLevel)
|
|
||||||
}
|
}
|
||||||
return rows, err
|
|
||||||
|
func (w *wrapperConn) ResetSession(ctx context.Context) error {
|
||||||
|
s, ok := w.conn.(driver.SessionResetter)
|
||||||
|
if !ok {
|
||||||
|
return driver.ErrSkip
|
||||||
|
}
|
||||||
|
return s.ResetSession(ctx)
|
||||||
}
|
}
|
||||||
|
2
doc.go
2
doc.go
@ -1,2 +1,2 @@
|
|||||||
// package wrapper provides SQL driver wrapper with micro tracing, logging, metering capabilities
|
// package wrapper provides SQL driver wrapper with micro tracing, logging, metering capabilities
|
||||||
package wrapper // import "go.unistack.org/micro-wrapper-sql/v3"
|
package wrapper
|
||||||
|
56
driver.go
56
driver.go
@ -6,20 +6,73 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
// _ driver.DriverContext = (*wrapperDriver)(nil)
|
||||||
|
// _ driver.Connector = (*wrapperDriver)(nil)
|
||||||
|
)
|
||||||
|
|
||||||
|
type conn interface {
|
||||||
|
driver.Pinger
|
||||||
|
driver.Execer
|
||||||
|
driver.ExecerContext
|
||||||
|
driver.Queryer
|
||||||
|
driver.QueryerContext
|
||||||
|
driver.Conn
|
||||||
|
driver.ConnPrepareContext
|
||||||
|
driver.ConnBeginTx
|
||||||
|
}
|
||||||
|
|
||||||
// wrapperDriver defines a wrapper for driver.Driver
|
// wrapperDriver defines a wrapper for driver.Driver
|
||||||
type wrapperDriver struct {
|
type wrapperDriver struct {
|
||||||
driver driver.Driver
|
driver driver.Driver
|
||||||
|
connector driver.Connector
|
||||||
opts Options
|
opts Options
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type wrapperConnector struct {
|
||||||
|
driver driver.Driver
|
||||||
|
name string
|
||||||
|
opts Options
|
||||||
|
}
|
||||||
|
|
||||||
// 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...), ctx: context.Background()}
|
return &wrapperDriver{driver: d, opts: NewOptions(opts...), ctx: context.Background()}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
// Connect implements driver.Driver Connect
|
||||||
|
func (w *wrapperConnector) Connect(ctx context.Context) (driver.Conn, error) {
|
||||||
|
return w.driver.Connect(ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Driver implements driver.Driver Driver
|
||||||
|
func (w *wrapperConnector) Driver() driver.Driver {
|
||||||
|
return w.driver
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
// Connect implements driver.Driver OpenConnector
|
||||||
|
func (w *wrapperDriver) OpenConnector(name string) (driver.Conn, error) {
|
||||||
|
return &wrapperConnector{driver: w.driver, name: name, opts: w.opts}, nil
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
// Open implements driver.Driver Open
|
// Open implements driver.Driver Open
|
||||||
func (w *wrapperDriver) Open(name string) (driver.Conn, error) {
|
func (w *wrapperDriver) Open(name string) (driver.Conn, error) {
|
||||||
|
// ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second) // Ensure eventual timeout
|
||||||
|
// defer cancel()
|
||||||
|
|
||||||
|
/*
|
||||||
|
connector, err := w.OpenConnector(name)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return connector.Connect(ctx)
|
||||||
|
*/
|
||||||
|
|
||||||
ts := time.Now()
|
ts := time.Now()
|
||||||
c, err := w.driver.Open(name)
|
c, err := w.driver.Open(name)
|
||||||
td := time.Since(ts)
|
td := time.Since(ts)
|
||||||
@ -27,10 +80,9 @@ func (w *wrapperDriver) Open(name string) (driver.Conn, error) {
|
|||||||
if w.opts.LoggerEnabled {
|
if w.opts.LoggerEnabled {
|
||||||
w.opts.Logger.Fields(w.opts.LoggerObserver(w.ctx, "Open", labelUnknown, td, err)...).Log(w.ctx, 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{d: w, dname: name, conn: c, opts: w.opts, ctx: w.ctx}, nil
|
return wrapConn(c, w.opts), nil
|
||||||
}
|
}
|
||||||
|
167
gen.go
Normal file
167
gen.go
Normal file
@ -0,0 +1,167 @@
|
|||||||
|
//go:build ignore
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"crypto/md5"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"sort"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
var connIfaces = []string{
|
||||||
|
"driver.ConnBeginTx",
|
||||||
|
"driver.ConnPrepareContext",
|
||||||
|
"driver.Execer",
|
||||||
|
"driver.ExecerContext",
|
||||||
|
"driver.NamedValueChecker",
|
||||||
|
"driver.Pinger",
|
||||||
|
"driver.Queryer",
|
||||||
|
"driver.QueryerContext",
|
||||||
|
"driver.SessionResetter",
|
||||||
|
"driver.Validator",
|
||||||
|
}
|
||||||
|
|
||||||
|
var stmtIfaces = []string{
|
||||||
|
"driver.StmtExecContext",
|
||||||
|
"driver.StmtQueryContext",
|
||||||
|
"driver.ColumnConverter",
|
||||||
|
"driver.NamedValueChecker",
|
||||||
|
}
|
||||||
|
|
||||||
|
func getHash(s []string) string {
|
||||||
|
h := md5.New()
|
||||||
|
io.WriteString(h, strings.Join(s, "|"))
|
||||||
|
return fmt.Sprintf("%x", h.Sum(nil))
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
comboConn := all(connIfaces)
|
||||||
|
|
||||||
|
sort.Slice(comboConn, func(i, j int) bool {
|
||||||
|
return len(comboConn[i]) < len(comboConn[j])
|
||||||
|
})
|
||||||
|
|
||||||
|
comboStmt := all(stmtIfaces)
|
||||||
|
|
||||||
|
sort.Slice(comboStmt, func(i, j int) bool {
|
||||||
|
return len(comboStmt[i]) < len(comboStmt[j])
|
||||||
|
})
|
||||||
|
|
||||||
|
b := bytes.NewBuffer(nil)
|
||||||
|
b.WriteString("// Code generated. DO NOT EDIT.\n\n")
|
||||||
|
b.WriteString("package wrapper\n\n")
|
||||||
|
b.WriteString(`import "database/sql/driver"`)
|
||||||
|
b.WriteString("\n\n")
|
||||||
|
|
||||||
|
b.WriteString("func wrapConn(dc driver.Conn, opts Options) driver.Conn {\n")
|
||||||
|
b.WriteString("\tc := &wrapperConn{conn: dc, opts: opts}\n")
|
||||||
|
|
||||||
|
for idx := len(comboConn) - 1; idx >= 0; idx-- {
|
||||||
|
ifaces := comboConn[idx]
|
||||||
|
n := len(ifaces)
|
||||||
|
if n == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
h := getHash(ifaces)
|
||||||
|
b.WriteString(fmt.Sprintf("\tif v, ok := dc.(wrapConn%04d_%s); ok {\n", n, h))
|
||||||
|
b.WriteString("\treturn struct {\n")
|
||||||
|
b.WriteString("\t\tdriver.Conn\n")
|
||||||
|
b.WriteString(fmt.Sprintf("\t\t\t%s", strings.Join(ifaces, "\n\t\t\t")))
|
||||||
|
b.WriteString("\t\t\n}{")
|
||||||
|
for idx := range ifaces {
|
||||||
|
if idx > 0 {
|
||||||
|
b.WriteString(", ")
|
||||||
|
b.WriteString("v")
|
||||||
|
} else if idx == 0 {
|
||||||
|
b.WriteString("c")
|
||||||
|
} else {
|
||||||
|
b.WriteString("v")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
b.WriteString(", v}\n")
|
||||||
|
b.WriteString("}\n\n")
|
||||||
|
}
|
||||||
|
b.WriteString("return c\n")
|
||||||
|
b.WriteString("}\n")
|
||||||
|
|
||||||
|
for idx := len(comboConn) - 1; idx >= 0; idx-- {
|
||||||
|
ifaces := comboConn[idx]
|
||||||
|
n := len(ifaces)
|
||||||
|
if n == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
h := getHash(ifaces)
|
||||||
|
b.WriteString(fmt.Sprintf("// %s\n", strings.Join(ifaces, "|")))
|
||||||
|
b.WriteString(fmt.Sprintf("type wrapConn%04d_%s interface {\n", n, h))
|
||||||
|
for _, iface := range ifaces {
|
||||||
|
b.WriteString(fmt.Sprintf("\t%s\n", iface))
|
||||||
|
}
|
||||||
|
b.WriteString("}\n\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
b.WriteString("func wrapStmt(stmt driver.Stmt, query string, opts Options) driver.Stmt {\n")
|
||||||
|
b.WriteString("\tc := &wrapperStmt{stmt: stmt, query: query, opts: opts}\n")
|
||||||
|
|
||||||
|
for idx := len(comboStmt) - 1; idx >= 0; idx-- {
|
||||||
|
ifaces := comboStmt[idx]
|
||||||
|
n := len(ifaces)
|
||||||
|
if n == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
h := getHash(ifaces)
|
||||||
|
b.WriteString(fmt.Sprintf("\tif v, ok := stmt.(wrapStmt%04d_%s); ok {\n", n, h))
|
||||||
|
b.WriteString("\treturn struct {\n")
|
||||||
|
b.WriteString("\t\tdriver.Stmt\n")
|
||||||
|
b.WriteString(fmt.Sprintf("\t\t\t%s", strings.Join(ifaces, "\n\t\t\t")))
|
||||||
|
b.WriteString("\t\t\n}{")
|
||||||
|
for idx := range ifaces {
|
||||||
|
if idx > 0 {
|
||||||
|
b.WriteString(", ")
|
||||||
|
b.WriteString("v")
|
||||||
|
} else if idx == 0 {
|
||||||
|
b.WriteString("c")
|
||||||
|
} else {
|
||||||
|
b.WriteString("v")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
b.WriteString(", v}\n")
|
||||||
|
b.WriteString("}\n\n")
|
||||||
|
}
|
||||||
|
b.WriteString("return c\n")
|
||||||
|
b.WriteString("}\n")
|
||||||
|
|
||||||
|
for idx := len(comboStmt) - 1; idx >= 0; idx-- {
|
||||||
|
ifaces := comboStmt[idx]
|
||||||
|
n := len(ifaces)
|
||||||
|
if n == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
h := getHash(ifaces)
|
||||||
|
b.WriteString(fmt.Sprintf("// %s\n", strings.Join(ifaces, "|")))
|
||||||
|
b.WriteString(fmt.Sprintf("type wrapStmt%04d_%s interface {\n", n, h))
|
||||||
|
for _, iface := range ifaces {
|
||||||
|
b.WriteString(fmt.Sprintf("\t%s\n", iface))
|
||||||
|
}
|
||||||
|
b.WriteString("}\n\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("%s\n", b.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
// all returns all combinations for a given string array.
|
||||||
|
func all[T any](set []T) (subsets [][]T) {
|
||||||
|
length := uint(len(set))
|
||||||
|
for subsetBits := 1; subsetBits < (1 << length); subsetBits++ {
|
||||||
|
var subset []T
|
||||||
|
for object := uint(0); object < length; object++ {
|
||||||
|
if (subsetBits>>object)&1 == 1 {
|
||||||
|
subset = append(subset, set[object])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
subsets = append(subsets, subset)
|
||||||
|
}
|
||||||
|
return subsets
|
||||||
|
}
|
6
go.mod
6
go.mod
@ -1,5 +1,5 @@
|
|||||||
module go.unistack.org/micro-wrapper-sql/v3
|
module go.unistack.org/micro-wrapper-sql/v4
|
||||||
|
|
||||||
go 1.16
|
go 1.19
|
||||||
|
|
||||||
require go.unistack.org/micro/v3 v3.10.14
|
require go.unistack.org/micro/v4 v4.0.3
|
||||||
|
10
go.sum
10
go.sum
@ -1,8 +1,2 @@
|
|||||||
github.com/imdario/mergo v0.3.13/go.mod h1:4lJ1jqUDcsbIECGy0RUJAXNIhg+6ocWgb1ALK2O4oXg=
|
go.unistack.org/micro/v4 v4.0.3 h1:AFr21ua3IrkuxH26kNYVrs7Kpsrm+4aylE/PfjLdCWM=
|
||||||
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
|
go.unistack.org/micro/v4 v4.0.3/go.mod h1:+wBa98rSf+mRXb/MuSVFPXtDrqN0k8rzPQiC8wRCwCo=
|
||||||
github.com/silas/dag v0.0.0-20211117232152-9d50aa809f35/go.mod h1:7RTUFBdIRC9nZ7/3RyRNH1bdqIShrDejd1YbLwgPS+I=
|
|
||||||
go.unistack.org/micro/v3 v3.10.14 h1:7fgLpwGlCN67twhwtngJDEQvrMkUBDSA5vzZqxIDqNE=
|
|
||||||
go.unistack.org/micro/v3 v3.10.14/go.mod h1:uMAc0U/x7dmtICCrblGf0ZLgYegu3VwQAquu+OFCw1Q=
|
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
|
||||||
gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
|
||||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
|
||||||
|
@ -5,9 +5,9 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"go.unistack.org/micro/v3/logger"
|
"go.unistack.org/micro/v4/logger"
|
||||||
"go.unistack.org/micro/v3/meter"
|
"go.unistack.org/micro/v4/meter"
|
||||||
"go.unistack.org/micro/v3/tracer"
|
"go.unistack.org/micro/v4/tracer"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
2
stats.go
2
stats.go
@ -1,4 +1,4 @@
|
|||||||
package wrapper // import "go.unistack.org/micro-wrapper-sql/v3"
|
package wrapper // import "go.unistack.org/micro-wrapper-sql-1/v3"
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
70
stmt.go
70
stmt.go
@ -6,15 +6,21 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"go.unistack.org/micro/v3/tracer"
|
"go.unistack.org/micro/v4/tracer"
|
||||||
)
|
)
|
||||||
|
|
||||||
var _ driver.Stmt = &wrapperStmt{}
|
var (
|
||||||
|
_ driver.Stmt = (*wrapperStmt)(nil)
|
||||||
|
_ driver.StmtQueryContext = (*wrapperStmt)(nil)
|
||||||
|
_ driver.StmtExecContext = (*wrapperStmt)(nil)
|
||||||
|
_ driver.NamedValueChecker = (*wrapperStmt)(nil)
|
||||||
|
)
|
||||||
|
|
||||||
// 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
|
||||||
|
query string
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -39,7 +45,7 @@ func (w *wrapperStmt) Close() 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.V(w.opts.LoggerLevel) {
|
||||||
w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "Close", labelUnknown, td, err)...).Log(ctx, w.opts.LoggerLevel)
|
w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "Close", labelUnknown, td, err)...).Log(ctx, w.opts.LoggerLevel)
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
@ -50,6 +56,15 @@ func (w *wrapperStmt) NumInput() int {
|
|||||||
return w.stmt.NumInput()
|
return w.stmt.NumInput()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CheckNamedValue implements driver.NamedValueChecker
|
||||||
|
func (w *wrapperStmt) CheckNamedValue(v *driver.NamedValue) error {
|
||||||
|
s, ok := w.stmt.(driver.NamedValueChecker)
|
||||||
|
if !ok {
|
||||||
|
return driver.ErrSkip
|
||||||
|
}
|
||||||
|
return s.CheckNamedValue(v)
|
||||||
|
}
|
||||||
|
|
||||||
// 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
|
var ctx context.Context
|
||||||
@ -72,9 +87,10 @@ func (w *wrapperStmt) Exec(args []driver.Value) (driver.Result, 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.V(w.opts.LoggerLevel) {
|
||||||
w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "Exec", labelUnknown, td, err)...).Log(ctx, 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
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -100,14 +116,15 @@ func (w *wrapperStmt) Query(args []driver.Value) (driver.Rows, 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.V(w.opts.LoggerLevel) {
|
||||||
w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "Query", labelUnknown, td, err)...).Log(ctx, 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.StmtExecContext ExecContext
|
||||||
func (w *wrapperStmt) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) {
|
func (w *wrapperStmt) ExecContext(ctx context.Context, args []driver.NamedValue) (driver.Result, error) {
|
||||||
var nctx context.Context
|
var nctx context.Context
|
||||||
var span tracer.Span
|
var span tracer.Span
|
||||||
if w.ctx != nil {
|
if w.ctx != nil {
|
||||||
@ -127,9 +144,10 @@ func (w *wrapperStmt) ExecContext(ctx context.Context, query string, args []driv
|
|||||||
span.AddLabels("args", fmt.Sprintf("%v", namedValueToLabels(args)))
|
span.AddLabels("args", fmt.Sprintf("%v", namedValueToLabels(args)))
|
||||||
}
|
}
|
||||||
labels := []string{labelMethod, "ExecContext", labelQuery, name}
|
labels := []string{labelMethod, "ExecContext", labelQuery, name}
|
||||||
if conn, ok := w.stmt.(driver.ExecerContext); ok {
|
|
||||||
|
if conn, ok := w.stmt.(driver.StmtExecContext); ok {
|
||||||
ts := time.Now()
|
ts := time.Now()
|
||||||
res, err := conn.ExecContext(nctx, query, args)
|
res, err := conn.ExecContext(nctx, args)
|
||||||
td := time.Since(ts)
|
td := time.Since(ts)
|
||||||
te := td.Seconds()
|
te := td.Seconds()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -142,25 +160,25 @@ func (w *wrapperStmt) 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.V(w.opts.LoggerLevel) {
|
||||||
w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "ExecContext", name, td, err)...).Log(ctx, 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
|
||||||
}
|
}
|
||||||
|
|
||||||
values, err := namedValueToValue(args)
|
values, err := namedValueToValue(args)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelFailure)...).Inc()
|
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelFailure)...).Inc()
|
||||||
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.V(w.opts.LoggerLevel) {
|
||||||
w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "ExecContext", name, 0, err)...).Log(ctx, 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
|
||||||
}
|
}
|
||||||
ts := time.Now()
|
ts := time.Now()
|
||||||
// nolint:staticcheck
|
res, err := w.Exec(values) // nolint:staticcheck
|
||||||
res, err := w.Exec(values)
|
|
||||||
td := time.Since(ts)
|
td := time.Since(ts)
|
||||||
te := td.Seconds()
|
te := td.Seconds()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -174,14 +192,15 @@ func (w *wrapperStmt) 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.V(w.opts.LoggerLevel) {
|
||||||
w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "ExecContext", name, td, err)).Log(ctx, 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.StmtQueryContext StmtQueryContext
|
||||||
func (w *wrapperStmt) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {
|
func (w *wrapperStmt) QueryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error) {
|
||||||
var nctx context.Context
|
var nctx context.Context
|
||||||
var span tracer.Span
|
var span tracer.Span
|
||||||
if w.ctx != nil {
|
if w.ctx != nil {
|
||||||
@ -201,9 +220,9 @@ func (w *wrapperStmt) QueryContext(ctx context.Context, query string, args []dri
|
|||||||
span.AddLabels("args", fmt.Sprintf("%v", namedValueToLabels(args)))
|
span.AddLabels("args", fmt.Sprintf("%v", namedValueToLabels(args)))
|
||||||
}
|
}
|
||||||
labels := []string{labelMethod, "QueryContext", labelQuery, name}
|
labels := []string{labelMethod, "QueryContext", labelQuery, name}
|
||||||
if conn, ok := w.stmt.(driver.QueryerContext); ok {
|
if conn, ok := w.stmt.(driver.StmtQueryContext); ok {
|
||||||
ts := time.Now()
|
ts := time.Now()
|
||||||
rows, err := conn.QueryContext(nctx, query, args)
|
rows, err := conn.QueryContext(nctx, args)
|
||||||
td := time.Since(ts)
|
td := time.Since(ts)
|
||||||
te := td.Seconds()
|
te := td.Seconds()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -217,11 +236,12 @@ func (w *wrapperStmt) 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.V(w.opts.LoggerLevel) {
|
||||||
w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "QueryContext", name, td, err)).Log(ctx, 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
|
||||||
}
|
}
|
||||||
|
|
||||||
values, err := namedValueToValue(args)
|
values, err := namedValueToValue(args)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelFailure)...).Inc()
|
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelFailure)...).Inc()
|
||||||
@ -229,14 +249,13 @@ func (w *wrapperStmt) 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.V(w.opts.LoggerLevel) {
|
||||||
w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "QueryContext", name, 0, err)...).Log(ctx, 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
|
||||||
}
|
}
|
||||||
ts := time.Now()
|
ts := time.Now()
|
||||||
// nolint:staticcheck
|
rows, err := w.Query(values) // nolint:staticcheck
|
||||||
rows, err := w.Query(values)
|
|
||||||
td := time.Since(ts)
|
td := time.Since(ts)
|
||||||
te := td.Seconds()
|
te := td.Seconds()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -250,8 +269,9 @@ func (w *wrapperStmt) 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.V(w.opts.LoggerLevel) {
|
||||||
w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "QueryContext", name, td, err)...).Log(ctx, 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
|
||||||
}
|
}
|
||||||
|
23
tx.go
23
tx.go
@ -5,64 +5,61 @@ import (
|
|||||||
"database/sql/driver"
|
"database/sql/driver"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"go.unistack.org/micro/v3/tracer"
|
"go.unistack.org/micro/v4/tracer"
|
||||||
)
|
)
|
||||||
|
|
||||||
var _ driver.Tx = &wrapperTx{}
|
var _ driver.Tx = (*wrapperTx)(nil)
|
||||||
|
|
||||||
// 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
|
ctx context.Context
|
||||||
}
|
}
|
||||||
|
|
||||||
// Commit implements driver.Tx Commit
|
// Commit implements driver.Tx Commit
|
||||||
func (w *wrapperTx) Commit() error {
|
func (w *wrapperTx) Commit() error {
|
||||||
if w.span != nil {
|
|
||||||
defer w.span.Finish()
|
|
||||||
}
|
|
||||||
ts := time.Now()
|
ts := time.Now()
|
||||||
err := w.tx.Commit()
|
err := w.tx.Commit()
|
||||||
td := time.Since(ts)
|
td := time.Since(ts)
|
||||||
|
|
||||||
|
if w.span != nil {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.span.AddLabels("error", true)
|
w.span.AddLabels("error", true)
|
||||||
w.span.AddLabels("err", err.Error())
|
w.span.AddLabels("err", err.Error())
|
||||||
}
|
}
|
||||||
|
w.span.Finish()
|
||||||
|
}
|
||||||
|
|
||||||
if w.opts.LoggerEnabled {
|
if w.opts.LoggerEnabled && w.opts.Logger.V(w.opts.LoggerLevel) {
|
||||||
w.opts.Logger.Fields(w.opts.LoggerObserver(w.ctx, "Commit", labelUnknown, td, err)...).Log(w.ctx, 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
|
||||||
w.conn.ctx = nil
|
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Rollback implements driver.Tx Rollback
|
// Rollback implements driver.Tx Rollback
|
||||||
func (w *wrapperTx) Rollback() error {
|
func (w *wrapperTx) Rollback() error {
|
||||||
if w.span != nil {
|
|
||||||
defer w.span.Finish()
|
|
||||||
}
|
|
||||||
ts := time.Now()
|
ts := time.Now()
|
||||||
err := w.tx.Rollback()
|
err := w.tx.Rollback()
|
||||||
td := time.Since(ts)
|
td := time.Since(ts)
|
||||||
|
|
||||||
|
if w.span != nil {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.span.AddLabels("error", true)
|
w.span.AddLabels("error", true)
|
||||||
w.span.AddLabels("err", err.Error())
|
w.span.AddLabels("err", err.Error())
|
||||||
}
|
}
|
||||||
|
w.span.Finish()
|
||||||
|
}
|
||||||
|
|
||||||
if w.opts.LoggerEnabled {
|
if w.opts.LoggerEnabled && w.opts.Logger.V(w.opts.LoggerLevel) {
|
||||||
w.opts.Logger.Fields(w.opts.LoggerObserver(w.ctx, "Rollback", labelUnknown, td, err)...).Log(w.ctx, 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
|
||||||
w.conn.ctx = nil
|
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
129
wrap.go
Normal file
129
wrap.go
Normal file
@ -0,0 +1,129 @@
|
|||||||
|
package wrapper
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql/driver"
|
||||||
|
)
|
||||||
|
|
||||||
|
func wrapDriver(d driver.Driver, opts Options) driver.Driver {
|
||||||
|
if _, ok := d.(driver.DriverContext); ok {
|
||||||
|
return &wrapperDriver{driver: d, opts: opts}
|
||||||
|
}
|
||||||
|
return struct{ driver.Driver }{&wrapperDriver{driver: d, opts: opts}}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
func wrapStmt(stmt driver.Stmt, query string, opts Options) driver.Stmt {
|
||||||
|
var (
|
||||||
|
_, hasExeCtx = stmt.(driver.StmtExecContext)
|
||||||
|
_, hasQryCtx = stmt.(driver.StmtQueryContext)
|
||||||
|
wc, hasColConv = stmt.(driver.ColumnConverter) //nolint:staticcheck
|
||||||
|
wn, hasNamValChk = stmt.(driver.NamedValueChecker)
|
||||||
|
)
|
||||||
|
|
||||||
|
ws := &wrapperStmt{stmt: stmt, query: query, opts: opts}
|
||||||
|
switch {
|
||||||
|
case !hasExeCtx && !hasQryCtx && !hasColConv && !hasNamValChk:
|
||||||
|
return struct {
|
||||||
|
driver.Stmt
|
||||||
|
}{ws}
|
||||||
|
case !hasExeCtx && hasQryCtx && !hasColConv && !hasNamValChk:
|
||||||
|
return struct {
|
||||||
|
driver.Stmt
|
||||||
|
driver.StmtQueryContext
|
||||||
|
}{ws, ws}
|
||||||
|
case hasExeCtx && !hasQryCtx && !hasColConv && !hasNamValChk:
|
||||||
|
return struct {
|
||||||
|
driver.Stmt
|
||||||
|
driver.StmtExecContext
|
||||||
|
}{ws, ws}
|
||||||
|
case hasExeCtx && hasQryCtx && !hasColConv && !hasNamValChk:
|
||||||
|
return struct {
|
||||||
|
driver.Stmt
|
||||||
|
driver.StmtExecContext
|
||||||
|
driver.StmtQueryContext
|
||||||
|
}{ws, ws, ws}
|
||||||
|
case !hasExeCtx && !hasQryCtx && hasColConv && !hasNamValChk:
|
||||||
|
return struct {
|
||||||
|
driver.Stmt
|
||||||
|
driver.ColumnConverter
|
||||||
|
}{ws, wc}
|
||||||
|
case !hasExeCtx && hasQryCtx && hasColConv && !hasNamValChk:
|
||||||
|
return struct {
|
||||||
|
driver.Stmt
|
||||||
|
driver.StmtQueryContext
|
||||||
|
driver.ColumnConverter
|
||||||
|
}{ws, ws, wc}
|
||||||
|
case hasExeCtx && !hasQryCtx && hasColConv && !hasNamValChk:
|
||||||
|
return struct {
|
||||||
|
driver.Stmt
|
||||||
|
driver.StmtExecContext
|
||||||
|
driver.ColumnConverter
|
||||||
|
}{ws, ws, wc}
|
||||||
|
case hasExeCtx && hasQryCtx && hasColConv && !hasNamValChk:
|
||||||
|
return struct {
|
||||||
|
driver.Stmt
|
||||||
|
driver.StmtExecContext
|
||||||
|
driver.StmtQueryContext
|
||||||
|
driver.ColumnConverter
|
||||||
|
}{ws, ws, ws, wc}
|
||||||
|
case !hasExeCtx && !hasQryCtx && !hasColConv && hasNamValChk:
|
||||||
|
return struct {
|
||||||
|
driver.Stmt
|
||||||
|
driver.NamedValueChecker
|
||||||
|
}{ws, wn}
|
||||||
|
case !hasExeCtx && hasQryCtx && !hasColConv && hasNamValChk:
|
||||||
|
return struct {
|
||||||
|
driver.Stmt
|
||||||
|
driver.StmtQueryContext
|
||||||
|
driver.NamedValueChecker
|
||||||
|
}{ws, ws, wn}
|
||||||
|
case hasExeCtx && !hasQryCtx && !hasColConv && hasNamValChk:
|
||||||
|
return struct {
|
||||||
|
driver.Stmt
|
||||||
|
driver.StmtExecContext
|
||||||
|
driver.NamedValueChecker
|
||||||
|
}{ws, ws, wn}
|
||||||
|
case hasExeCtx && hasQryCtx && !hasColConv && hasNamValChk:
|
||||||
|
return struct {
|
||||||
|
driver.Stmt
|
||||||
|
driver.StmtExecContext
|
||||||
|
driver.StmtQueryContext
|
||||||
|
driver.NamedValueChecker
|
||||||
|
}{ws, ws, ws, wn}
|
||||||
|
case !hasExeCtx && !hasQryCtx && hasColConv && hasNamValChk:
|
||||||
|
return struct {
|
||||||
|
driver.Stmt
|
||||||
|
driver.ColumnConverter
|
||||||
|
driver.NamedValueChecker
|
||||||
|
}{ws, wc, wn}
|
||||||
|
case !hasExeCtx && hasQryCtx && hasColConv && hasNamValChk:
|
||||||
|
return struct {
|
||||||
|
driver.Stmt
|
||||||
|
driver.StmtQueryContext
|
||||||
|
driver.ColumnConverter
|
||||||
|
driver.NamedValueChecker
|
||||||
|
}{ws, ws, wc, wn}
|
||||||
|
case hasExeCtx && !hasQryCtx && hasColConv && hasNamValChk:
|
||||||
|
return struct {
|
||||||
|
driver.Stmt
|
||||||
|
driver.StmtExecContext
|
||||||
|
driver.ColumnConverter
|
||||||
|
driver.NamedValueChecker
|
||||||
|
}{ws, ws, wc, wn}
|
||||||
|
case hasExeCtx && hasQryCtx && hasColConv && hasNamValChk:
|
||||||
|
return struct {
|
||||||
|
driver.Stmt
|
||||||
|
driver.StmtExecContext
|
||||||
|
driver.StmtQueryContext
|
||||||
|
driver.ColumnConverter
|
||||||
|
driver.NamedValueChecker
|
||||||
|
}{ws, ws, ws, wc, wn}
|
||||||
|
}
|
||||||
|
panic("unreachable")
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
// WrapConn allows an existing driver.Conn to be wrapped.
|
||||||
|
func WrapConn(c driver.Conn, opts ...Option) driver.Conn {
|
||||||
|
return wrapConn(c, NewOptions(opts...))
|
||||||
|
}
|
20699
wrap_gen.go
Normal file
20699
wrap_gen.go
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user