WIP: rewrite wrapper #53
85
conn.go
85
conn.go
@ -4,8 +4,6 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"database/sql/driver"
|
"database/sql/driver"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"go.unistack.org/micro/v3/tracer"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// wrapperConn defines a wrapper for driver.Conn
|
// wrapperConn defines a wrapper for driver.Conn
|
||||||
@ -39,23 +37,16 @@ func (w *wrapperConn) Begin() (driver.Tx, error) {
|
|||||||
|
|
||||||
// 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) {
|
||||||
var nctx context.Context
|
nctx, span := w.opts.Tracer.Start(ctx, "BeginTx")
|
||||||
var span tracer.Span
|
span.AddLabels("op", "BeginTx")
|
||||||
|
if name := getQueryName(ctx); name != "" {
|
||||||
name := getQueryName(ctx)
|
|
||||||
if name != "" {
|
|
||||||
nctx, span = w.opts.Tracer.Start(ctx, "BeginTx "+name)
|
|
||||||
} else {
|
|
||||||
nctx, span = w.opts.Tracer.Start(ctx, "BeginTx")
|
|
||||||
}
|
|
||||||
if name == "" {
|
|
||||||
name = "unknown"
|
|
||||||
}
|
|
||||||
span.AddLabels("query", name)
|
span.AddLabels("query", name)
|
||||||
|
}
|
||||||
if connBeginTx, ok := w.conn.(driver.ConnBeginTx); ok {
|
if connBeginTx, ok := w.conn.(driver.ConnBeginTx); ok {
|
||||||
tx, err := connBeginTx.BeginTx(nctx, opts)
|
tx, err := connBeginTx.BeginTx(nctx, opts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
span.AddLabels("error", true)
|
span.AddLabels("error", true)
|
||||||
|
span.AddLabels("err", err.Error())
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &wrapperTx{tx: tx, opts: w.opts, span: span}, nil
|
return &wrapperTx{tx: tx, opts: w.opts, span: span}, nil
|
||||||
@ -63,29 +54,23 @@ func (w *wrapperConn) BeginTx(ctx context.Context, opts driver.TxOptions) (drive
|
|||||||
tx, err := w.conn.Begin()
|
tx, err := w.conn.Begin()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
span.AddLabels("error", true)
|
span.AddLabels("error", true)
|
||||||
|
span.AddLabels("err", err.Error())
|
||||||
}
|
}
|
||||||
return tx, err
|
return tx, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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) {
|
||||||
var nctx context.Context
|
nctx, span := w.opts.Tracer.Start(ctx, "PrepareContext")
|
||||||
var span tracer.Span
|
span.AddLabels("op", "PrepareContext")
|
||||||
|
if name := getQueryName(ctx); name != "" {
|
||||||
name := getQueryName(ctx)
|
|
||||||
if name != "" {
|
|
||||||
nctx, span = w.opts.Tracer.Start(ctx, "BeginTx "+name)
|
|
||||||
} else {
|
|
||||||
nctx, span = w.opts.Tracer.Start(ctx, "BeginTx")
|
|
||||||
}
|
|
||||||
if name == "" {
|
|
||||||
name = "unknown"
|
|
||||||
}
|
|
||||||
span.AddLabels("query", name)
|
span.AddLabels("query", name)
|
||||||
|
}
|
||||||
if connPrepareContext, ok := w.conn.(driver.ConnPrepareContext); ok {
|
if connPrepareContext, ok := w.conn.(driver.ConnPrepareContext); ok {
|
||||||
stmt, err := connPrepareContext.PrepareContext(nctx, query)
|
stmt, err := connPrepareContext.PrepareContext(nctx, query)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
span.AddLabels("error", true)
|
span.AddLabels("error", true)
|
||||||
|
span.AddLabels("err", err.Error())
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &wrapperStmt{stmt: stmt, opts: w.opts}, nil
|
return &wrapperStmt{stmt: stmt, opts: w.opts}, nil
|
||||||
@ -93,6 +78,7 @@ func (w *wrapperConn) PrepareContext(ctx context.Context, query string) (driver.
|
|||||||
stmt, err := w.conn.Prepare(query)
|
stmt, err := w.conn.Prepare(query)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
span.AddLabels("error", true)
|
span.AddLabels("error", true)
|
||||||
|
span.AddLabels("err", err.Error())
|
||||||
}
|
}
|
||||||
return stmt, err
|
return stmt, err
|
||||||
}
|
}
|
||||||
@ -107,35 +93,33 @@ func (w *wrapperConn) Exec(query string, args []driver.Value) (driver.Result, er
|
|||||||
|
|
||||||
// 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) {
|
||||||
var nctx context.Context
|
nctx, span := w.opts.Tracer.Start(ctx, "ExecContext")
|
||||||
var span tracer.Span
|
span.AddLabels("op", "ExecContext")
|
||||||
name := getQueryName(ctx)
|
if name := getQueryName(ctx); name != "" {
|
||||||
if name != "" {
|
span.AddLabels("query", name)
|
||||||
nctx, span = w.opts.Tracer.Start(ctx, "ExecContext "+name)
|
|
||||||
} else {
|
|
||||||
nctx, span = w.opts.Tracer.Start(ctx, "ExecContext")
|
|
||||||
}
|
}
|
||||||
defer span.Finish()
|
defer span.Finish()
|
||||||
if name == "" {
|
if len(args) > 0 {
|
||||||
name = "unknown"
|
|
||||||
}
|
|
||||||
span.AddLabels("args", fmt.Sprintf("%v", namedValueToLabels(args)))
|
span.AddLabels("args", fmt.Sprintf("%v", namedValueToLabels(args)))
|
||||||
span.AddLabels("query", name)
|
}
|
||||||
if execerContext, ok := w.conn.(driver.ExecerContext); ok {
|
if execerContext, ok := w.conn.(driver.ExecerContext); ok {
|
||||||
res, err := execerContext.ExecContext(nctx, query, args)
|
res, err := execerContext.ExecContext(nctx, query, args)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
span.AddLabels("error", true)
|
span.AddLabels("error", true)
|
||||||
|
span.AddLabels("err", err.Error())
|
||||||
}
|
}
|
||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
values, err := namedValueToValue(args)
|
values, err := namedValueToValue(args)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
span.AddLabels("error", true)
|
span.AddLabels("error", true)
|
||||||
|
span.AddLabels("err", err.Error())
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
res, err := w.Exec(query, values)
|
res, err := w.Exec(query, values)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
span.AddLabels("error", true)
|
span.AddLabels("error", true)
|
||||||
|
span.AddLabels("err", err.Error())
|
||||||
}
|
}
|
||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
@ -145,7 +129,12 @@ func (w *wrapperConn) Ping(ctx context.Context) error {
|
|||||||
if pinger, ok := w.conn.(driver.Pinger); ok {
|
if pinger, ok := w.conn.(driver.Pinger); ok {
|
||||||
nctx, span := w.opts.Tracer.Start(ctx, "Ping")
|
nctx, span := w.opts.Tracer.Start(ctx, "Ping")
|
||||||
defer span.Finish()
|
defer span.Finish()
|
||||||
return pinger.Ping(nctx)
|
err := pinger.Ping(nctx)
|
||||||
|
if err != nil {
|
||||||
|
span.AddLabels("error", true)
|
||||||
|
span.AddLabels("err", err.Error())
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return ErrUnsupported
|
return ErrUnsupported
|
||||||
}
|
}
|
||||||
@ -160,35 +149,33 @@ func (w *wrapperConn) Query(query string, args []driver.Value) (driver.Rows, err
|
|||||||
|
|
||||||
// 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) {
|
||||||
var nctx context.Context
|
nctx, span := w.opts.Tracer.Start(ctx, "QueryContext")
|
||||||
var span tracer.Span
|
span.AddLabels("op", "QueryContext")
|
||||||
name := getQueryName(ctx)
|
if name := getQueryName(ctx); name != "" {
|
||||||
if name != "" {
|
span.AddLabels("query", name)
|
||||||
nctx, span = w.opts.Tracer.Start(ctx, "QueryContext "+name)
|
|
||||||
} else {
|
|
||||||
nctx, span = w.opts.Tracer.Start(ctx, "QueryContext")
|
|
||||||
}
|
}
|
||||||
defer span.Finish()
|
defer span.Finish()
|
||||||
if name == "" {
|
if len(args) > 0 {
|
||||||
name = "unknown"
|
|
||||||
}
|
|
||||||
span.AddLabels("args", fmt.Sprintf("%v", namedValueToLabels(args)))
|
span.AddLabels("args", fmt.Sprintf("%v", namedValueToLabels(args)))
|
||||||
span.AddLabels("query", name)
|
}
|
||||||
if queryerContext, ok := w.conn.(driver.QueryerContext); ok {
|
if queryerContext, ok := w.conn.(driver.QueryerContext); ok {
|
||||||
rows, err := queryerContext.QueryContext(nctx, query, args)
|
rows, err := queryerContext.QueryContext(nctx, query, args)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
span.AddLabels("error", true)
|
span.AddLabels("error", true)
|
||||||
|
span.AddLabels("err", err.Error())
|
||||||
}
|
}
|
||||||
return rows, err
|
return rows, err
|
||||||
}
|
}
|
||||||
values, err := namedValueToValue(args)
|
values, err := namedValueToValue(args)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
span.AddLabels("error", true)
|
span.AddLabels("error", true)
|
||||||
|
span.AddLabels("err", err.Error())
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
rows, err := w.Query(query, values)
|
rows, err := w.Query(query, values)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
span.AddLabels("error", true)
|
span.AddLabels("error", true)
|
||||||
|
span.AddLabels("err", err.Error())
|
||||||
}
|
}
|
||||||
return rows, err
|
return rows, err
|
||||||
}
|
}
|
||||||
|
42
stmt.go
42
stmt.go
@ -4,8 +4,6 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"database/sql/driver"
|
"database/sql/driver"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"go.unistack.org/micro/v3/tracer"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// wrapperStmt defines a wrapper for driver.Stmt
|
// wrapperStmt defines a wrapper for driver.Stmt
|
||||||
@ -36,59 +34,53 @@ func (w *wrapperStmt) Query(args []driver.Value) (driver.Rows, error) {
|
|||||||
|
|
||||||
// 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) {
|
||||||
var nctx context.Context
|
nctx, span := w.opts.Tracer.Start(ctx, "ExecContext")
|
||||||
var span tracer.Span
|
span.AddLabels("op", "ExecContext")
|
||||||
name := getQueryName(ctx)
|
if name := getQueryName(ctx); name != "" {
|
||||||
if name != "" {
|
span.AddLabels("query", name)
|
||||||
nctx, span = w.opts.Tracer.Start(ctx, "ExecContext "+name)
|
|
||||||
} else {
|
|
||||||
nctx, span = w.opts.Tracer.Start(ctx, "ExecContext")
|
|
||||||
}
|
}
|
||||||
defer span.Finish()
|
defer span.Finish()
|
||||||
if name == "" {
|
if len(args) > 0 {
|
||||||
name = "unknown"
|
|
||||||
}
|
|
||||||
span.AddLabels("args", fmt.Sprintf("%v", namedValueToLabels(args)))
|
span.AddLabels("args", fmt.Sprintf("%v", namedValueToLabels(args)))
|
||||||
span.AddLabels("query", name)
|
}
|
||||||
if execerContext, ok := w.stmt.(driver.ExecerContext); ok {
|
if execerContext, ok := w.stmt.(driver.ExecerContext); ok {
|
||||||
res, err := execerContext.ExecContext(nctx, query, args)
|
res, err := execerContext.ExecContext(nctx, query, args)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
span.AddLabels("error", true)
|
span.AddLabels("error", true)
|
||||||
|
span.AddLabels("err", err.Error())
|
||||||
}
|
}
|
||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
values, err := namedValueToValue(args)
|
values, err := namedValueToValue(args)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
span.AddLabels("error", true)
|
span.AddLabels("error", true)
|
||||||
|
span.AddLabels("err", err.Error())
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
res, err := w.Exec(values)
|
res, err := w.Exec(values)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
span.AddLabels("error", true)
|
span.AddLabels("error", true)
|
||||||
|
span.AddLabels("err", err.Error())
|
||||||
}
|
}
|
||||||
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) {
|
||||||
var nctx context.Context
|
nctx, span := w.opts.Tracer.Start(ctx, "QueryContext")
|
||||||
var span tracer.Span
|
span.AddLabels("op", "QueryContext")
|
||||||
name := getQueryName(ctx)
|
if name := getQueryName(ctx); name != "" {
|
||||||
if name != "" {
|
span.AddLabels("query", name)
|
||||||
nctx, span = w.opts.Tracer.Start(ctx, "QueryContext "+name)
|
|
||||||
} else {
|
|
||||||
nctx, span = w.opts.Tracer.Start(ctx, "QueryContext")
|
|
||||||
}
|
}
|
||||||
defer span.Finish()
|
defer span.Finish()
|
||||||
if name == "" {
|
if len(args) > 0 {
|
||||||
name = "unknown"
|
|
||||||
}
|
|
||||||
span.AddLabels("args", fmt.Sprintf("%v", namedValueToLabels(args)))
|
span.AddLabels("args", fmt.Sprintf("%v", namedValueToLabels(args)))
|
||||||
span.AddLabels("query", name)
|
}
|
||||||
if queryerContext, ok := w.stmt.(driver.QueryerContext); ok {
|
if queryerContext, ok := w.stmt.(driver.QueryerContext); ok {
|
||||||
rows, err := queryerContext.QueryContext(nctx, query, args)
|
rows, err := queryerContext.QueryContext(nctx, query, args)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
span.AddLabels("error", true)
|
span.AddLabels("error", true)
|
||||||
|
span.AddLabels("err", err.Error())
|
||||||
}
|
}
|
||||||
return rows, err
|
return rows, err
|
||||||
}
|
}
|
||||||
@ -96,12 +88,14 @@ func (w *wrapperStmt) QueryContext(ctx context.Context, query string, args []dri
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
span.AddLabels("error", true)
|
span.AddLabels("error", true)
|
||||||
|
span.AddLabels("err", err.Error())
|
||||||
}
|
}
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
rows, err := w.Query(values)
|
rows, err := w.Query(values)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
span.AddLabels("error", true)
|
span.AddLabels("error", true)
|
||||||
|
span.AddLabels("err", err.Error())
|
||||||
}
|
}
|
||||||
return rows, err
|
return rows, err
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user