updates from testing
Some checks failed
automerge / automerge (pull_request) Failing after 4s
codeql / analyze (go) (pull_request) Failing after 3m20s
dependabot-automerge / automerge (pull_request) Has been skipped
prbuild / test (pull_request) Failing after 1m28s
prbuild / lint (pull_request) Failing after 2m34s
autoapprove / autoapprove (pull_request) Failing after 1m25s

This commit is contained in:
Василий Толстов 2023-09-01 14:48:57 +03:00
parent a81da5bd2d
commit 7d2787c3a6
8 changed files with 45 additions and 158 deletions

View File

@ -39,7 +39,7 @@ func namedValueToLabels(named []driver.NamedValue) []interface{} {
// getCallerName get the name of the function A where A() -> B() -> GetFunctionCallerName() // getCallerName get the name of the function A where A() -> B() -> GetFunctionCallerName()
func getCallerName() string { func getCallerName() string {
pc, _, _, ok := runtime.Caller(2) pc, _, _, ok := runtime.Caller(3)
details := runtime.FuncForPC(pc) details := runtime.FuncForPC(pc)
var callerName string var callerName string
if ok && details != nil { if ok && details != nil {

36
conn.go
View File

@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"time" "time"
requestid "go.unistack.org/micro-wrapper-requestid/v4"
"go.unistack.org/micro/v4/tracer" "go.unistack.org/micro/v4/tracer"
) )
@ -99,12 +100,11 @@ 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, "sdk.database", tracer.WithSpanKind(tracer.SpanKindClient)) nctx, span := w.opts.Tracer.Start(ctx, "sdk.database", tracer.WithSpanKind(tracer.SpanKindClient))
span.AddLabels("method", "BeginTx") span.AddLabels("db.method", "BeginTx")
name := getQueryName(ctx) name := getQueryName(ctx)
if name != "" { span.AddLabels("db.statement", name)
span.AddLabels("db.query", name) if id, ok := ctx.Value(requestid.XRequestIDKey).(string); ok {
} else { span.AddLabels("x-request-id", id)
name = getCallerName()
} }
labels := []string{labelMethod, "BeginTx", labelQuery, name} labels := []string{labelMethod, "BeginTx", labelQuery, name}
@ -178,14 +178,12 @@ func (w *wrapperConn) PrepareContext(ctx context.Context, query string) (driver.
} else { } else {
nctx, span = w.opts.Tracer.Start(ctx, "sdk.database", tracer.WithSpanKind(tracer.SpanKindClient)) nctx, span = w.opts.Tracer.Start(ctx, "sdk.database", tracer.WithSpanKind(tracer.SpanKindClient))
} }
span.AddLabels("method", "PrepareContext") span.AddLabels("db.method", "PrepareContext")
name := getQueryName(ctx) name := getQueryName(ctx)
if name != "" { span.AddLabels("db.statement", name)
span.AddLabels("db.query", name) if id, ok := ctx.Value(requestid.XRequestIDKey).(string); ok {
} else { span.AddLabels("x-request-id", id)
name = getCallerName()
} }
labels := []string{labelMethod, "PrepareContext", labelQuery, name} labels := []string{labelMethod, "PrepareContext", labelQuery, name}
conn, ok := w.conn.(driver.ConnPrepareContext) conn, ok := w.conn.(driver.ConnPrepareContext)
if !ok { if !ok {
@ -259,12 +257,11 @@ func (w *wrapperConn) ExecContext(ctx context.Context, query string, args []driv
} else { } else {
nctx, span = w.opts.Tracer.Start(ctx, "sdk.database", tracer.WithSpanKind(tracer.SpanKindClient)) nctx, span = w.opts.Tracer.Start(ctx, "sdk.database", tracer.WithSpanKind(tracer.SpanKindClient))
} }
span.AddLabels("method", "ExecContext") span.AddLabels("db.method", "ExecContext")
name := getQueryName(ctx) name := getQueryName(ctx)
if name != "" { span.AddLabels("db.statement", name)
span.AddLabels("db.query", name) if id, ok := ctx.Value(requestid.XRequestIDKey).(string); ok {
} else { span.AddLabels("x-request-id", id)
name = getCallerName()
} }
defer span.Finish() defer span.Finish()
if len(args) > 0 { if len(args) > 0 {
@ -382,12 +379,11 @@ func (w *wrapperConn) QueryContext(ctx context.Context, query string, args []dri
} else { } else {
nctx, span = w.opts.Tracer.Start(ctx, "sdk.database", tracer.WithSpanKind(tracer.SpanKindClient)) nctx, span = w.opts.Tracer.Start(ctx, "sdk.database", tracer.WithSpanKind(tracer.SpanKindClient))
} }
span.AddLabels("method", "QueryContext") span.AddLabels("db.method", "QueryContext")
name := getQueryName(ctx) name := getQueryName(ctx)
if name != "" {
span.AddLabels("db.statement", name) span.AddLabels("db.statement", name)
} else { if id, ok := ctx.Value(requestid.XRequestIDKey).(string); ok {
name = getCallerName() span.AddLabels("x-request-id", id)
} }
defer span.Finish() defer span.Finish()
if len(args) > 0 { if len(args) > 0 {

5
go.mod
View File

@ -2,4 +2,7 @@ module go.unistack.org/micro-wrapper-sql/v4
go 1.20 go 1.20
require go.unistack.org/micro/v4 v4.0.3 require (
go.unistack.org/micro-wrapper-requestid/v4 v4.0.3
go.unistack.org/micro/v4 v4.0.8
)

6
go.sum
View File

@ -1,2 +1,4 @@
go.unistack.org/micro/v4 v4.0.3 h1:AFr21ua3IrkuxH26kNYVrs7Kpsrm+4aylE/PfjLdCWM= go.unistack.org/micro-wrapper-requestid/v4 v4.0.3 h1:LhloK8yLLsov4XiKw9i20wdBUfvWCG1TaBNgNVtNa8Q=
go.unistack.org/micro/v4 v4.0.3/go.mod h1:+wBa98rSf+mRXb/MuSVFPXtDrqN0k8rzPQiC8wRCwCo= go.unistack.org/micro-wrapper-requestid/v4 v4.0.3/go.mod h1:vRIkCMyhbBVus1xQlr/Fef65n07znrBGkpSCpjkEiqk=
go.unistack.org/micro/v4 v4.0.8 h1:D5n18+fhyRjJkKSPdIcW1ratWUuzy9TzrS5ud/qImWg=
go.unistack.org/micro/v4 v4.0.8/go.mod h1:QT3gOIE4qGgBiQGm2Pad/62Sl5R53QfrgYHD448aX14=

View File

@ -17,7 +17,7 @@ var (
DefaultMeterMetricPrefix = "micro_sql_" DefaultMeterMetricPrefix = "micro_sql_"
// DefaultLoggerObserver used to prepare labels for logger // DefaultLoggerObserver used to prepare labels for logger
DefaultLoggerObserver = func(ctx context.Context, method string, query string, td time.Duration, err error) []interface{} { DefaultLoggerObserver = func(ctx context.Context, method string, query string, td time.Duration, err error) []interface{} {
labels := []interface{}{"method", method, "took", fmt.Sprintf("%v", td)} labels := []interface{}{"db.method", method, "took", fmt.Sprintf("%v", td)}
if err != nil { if err != nil {
labels = append(labels, "error", err.Error()) labels = append(labels, "error", err.Error())
} }
@ -44,13 +44,13 @@ var (
meterRequestDurationSeconds = "request_duration_seconds" meterRequestDurationSeconds = "request_duration_seconds"
labelUnknown = "unknown" labelUnknown = "unknown"
labelQuery = "query" labelQuery = "db.statement"
labelMethod = "method" labelMethod = "db.method"
labelStatus = "status" labelStatus = "status"
labelSuccess = "success" labelSuccess = "success"
labelFailure = "failure" labelFailure = "failure"
labelHost = "db_host" labelHost = "db.host"
labelDatabase = "db_name" labelDatabase = "db.name"
) )
// Options struct holds wrapper options // Options struct holds wrapper options
@ -180,5 +180,5 @@ func getQueryName(ctx context.Context) string {
if v, ok := ctx.Value(queryNameKey{}).(string); ok && v != labelUnknown { if v, ok := ctx.Value(queryNameKey{}).(string); ok && v != labelUnknown {
return v return v
} }
return "" return getCallerName()
} }

View File

@ -1,4 +1,4 @@
package wrapper // import "go.unistack.org/micro-wrapper-sql-1/v3" package wrapper // import "go.unistack.org/micro-wrapper-sql/v4"
import ( import (
"context" "context"

26
stmt.go
View File

@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"time" "time"
requestid "go.unistack.org/micro-wrapper-requestid/v4"
"go.unistack.org/micro/v4/tracer" "go.unistack.org/micro/v4/tracer"
) )
@ -75,8 +76,7 @@ func (w *wrapperStmt) Exec(args []driver.Value) (driver.Result, error) {
} }
labels := []string{labelMethod, "Exec"} labels := []string{labelMethod, "Exec"}
ts := time.Now() ts := time.Now()
// nolint:staticcheck res, err := w.stmt.Exec(args) // nolint:staticcheck
res, err := w.stmt.Exec(args)
td := time.Since(ts) td := time.Since(ts)
te := td.Seconds() te := td.Seconds()
if err != nil { if err != nil {
@ -141,17 +141,16 @@ func (w *wrapperStmt) ExecContext(ctx context.Context, args []driver.NamedValue)
} else { } else {
nctx, span = w.opts.Tracer.Start(ctx, "sdk.database", tracer.WithSpanKind(tracer.SpanKindClient)) nctx, span = w.opts.Tracer.Start(ctx, "sdk.database", tracer.WithSpanKind(tracer.SpanKindClient))
} }
span.AddLabels("method", "ExecContext") span.AddLabels("db.method", "ExecContext")
name := getQueryName(ctx) name := getQueryName(ctx)
if name != "" { span.AddLabels("db.statement", name)
span.AddLabels("db.query", name)
} else {
name = getCallerName()
}
defer span.Finish() defer span.Finish()
if len(args) > 0 { if len(args) > 0 {
span.AddLabels("db.args", fmt.Sprintf("%v", namedValueToLabels(args))) span.AddLabels("db.args", fmt.Sprintf("%v", namedValueToLabels(args)))
} }
if id, ok := ctx.Value(requestid.XRequestIDKey).(string); ok {
span.AddLabels("x-request-id", id)
}
labels := []string{labelMethod, "ExecContext", labelQuery, name} labels := []string{labelMethod, "ExecContext", labelQuery, name}
if conn, ok := w.stmt.(driver.StmtExecContext); ok { if conn, ok := w.stmt.(driver.StmtExecContext); ok {
@ -215,17 +214,16 @@ func (w *wrapperStmt) QueryContext(ctx context.Context, args []driver.NamedValue
} else { } else {
nctx, span = w.opts.Tracer.Start(ctx, "sdk.database", tracer.WithSpanKind(tracer.SpanKindClient)) nctx, span = w.opts.Tracer.Start(ctx, "sdk.database", tracer.WithSpanKind(tracer.SpanKindClient))
} }
span.AddLabels("method", "QueryContext") span.AddLabels("db.method", "QueryContext")
name := getQueryName(ctx) name := getQueryName(ctx)
if name != "" { span.AddLabels("db.statement", name)
span.AddLabels("db.query", name)
} else {
name = getCallerName()
}
defer span.Finish() defer span.Finish()
if len(args) > 0 { if len(args) > 0 {
span.AddLabels("db.args", fmt.Sprintf("%v", namedValueToLabels(args))) span.AddLabels("db.args", fmt.Sprintf("%v", namedValueToLabels(args)))
} }
if id, ok := ctx.Value(requestid.XRequestIDKey).(string); ok {
span.AddLabels("x-request-id", id)
}
labels := []string{labelMethod, "QueryContext", labelQuery, name} labels := []string{labelMethod, "QueryContext", labelQuery, name}
if conn, ok := w.stmt.(driver.StmtQueryContext); ok { if conn, ok := w.stmt.(driver.StmtQueryContext); ok {
ts := time.Now() ts := time.Now()

112
wrap.go
View File

@ -11,118 +11,6 @@ func wrapDriver(d driver.Driver, opts Options) driver.Driver {
return struct{ driver.Driver }{&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. // WrapConn allows an existing driver.Conn to be wrapped.
func WrapConn(c driver.Conn, opts ...Option) driver.Conn { func WrapConn(c driver.Conn, opts ...Option) driver.Conn {
return wrapConn(c, NewOptions(opts...)) return wrapConn(c, NewOptions(opts...))