tracing fixes #57

Merged
vtolstov merged 4 commits from fixes into v3 2023-01-16 23:41:26 +03:00
4 changed files with 70 additions and 15 deletions
Showing only changes of commit 7e547cbe45 - Show all commits

View File

@ -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
} }

47
conn.go
View File

@ -5,12 +5,17 @@ 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
@ -91,7 +96,7 @@ 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) {
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 != "" {
@ -120,7 +125,8 @@ func (w *wrapperConn) BeginTx(ctx context.Context, opts driver.TxOptions) (drive
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(context.TODO(), "BeginTx", name, td, err)...).Log(context.TODO(), 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
@ -140,12 +146,19 @@ func (w *wrapperConn) BeginTx(ctx context.Context, opts driver.TxOptions) (drive
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(context.TODO(), "BeginTx", name, td, err)...).Log(context.TODO(), 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 != "" {
@ -176,7 +189,7 @@ func (w *wrapperConn) PrepareContext(ctx context.Context, query string) (driver.
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(context.TODO(), "PrepareContext", name, td, err)...).Log(context.TODO(), 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)
@ -227,7 +240,13 @@ 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) {
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 != "" {
@ -292,7 +311,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()
@ -348,7 +373,13 @@ 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) {
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 != "" {

21
stmt.go
View File

@ -5,12 +5,17 @@ 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
@ -85,7 +90,13 @@ 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) {
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 != "" {
@ -153,7 +164,13 @@ func (w *wrapperStmt) ExecContext(ctx context.Context, query string, args []driv
// 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 != "" {

12
tx.go
View File

@ -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
@ -30,9 +34,11 @@ func (w *wrapperTx) Commit() error {
} }
if w.opts.LoggerEnabled { if w.opts.LoggerEnabled {
w.opts.Logger.Fields(w.opts.LoggerObserver(context.TODO(), "Commit", labelUnknown, td, err)...).Log(context.TODO(), w.opts.LoggerLevel) w.opts.Logger.Fields(w.opts.LoggerObserver(w.ctx, "Commit", labelUnknown, td, err)...).Log(context.TODO(), w.opts.LoggerLevel)
} }
w.ctx = nil
return err return err
} }
@ -51,8 +57,10 @@ func (w *wrapperTx) Rollback() error {
} }
if w.opts.LoggerEnabled { if w.opts.LoggerEnabled {
w.opts.Logger.Fields(w.opts.LoggerObserver(context.TODO(), "Rollback", labelUnknown, td, err)...).Log(context.TODO(), w.opts.LoggerLevel) w.opts.Logger.Fields(w.opts.LoggerObserver(w.ctx, "Rollback", labelUnknown, td, err)...).Log(context.TODO(), w.opts.LoggerLevel)
} }
w.ctx = nil
return err return err
} }