Merge pull request 'tracing updates' (#79) from updates into v3
Reviewed-on: #79
This commit is contained in:
commit
b31e3e7ffa
@ -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 {
|
||||||
|
38
conn.go
38
conn.go
@ -6,6 +6,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
requestid "go.unistack.org/micro-wrapper-requestid/v3"
|
||||||
"go.unistack.org/micro/v3/tracer"
|
"go.unistack.org/micro/v3/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)
|
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 {
|
||||||
|
5
go.mod
5
go.mod
@ -2,4 +2,7 @@ module go.unistack.org/micro-wrapper-sql/v3
|
|||||||
|
|
||||||
go 1.20
|
go 1.20
|
||||||
|
|
||||||
require go.unistack.org/micro/v3 v3.10.25
|
require (
|
||||||
|
go.unistack.org/micro-wrapper-requestid/v3 v3.8.7
|
||||||
|
go.unistack.org/micro/v3 v3.10.25
|
||||||
|
)
|
||||||
|
8
go.sum
8
go.sum
@ -1,2 +1,10 @@
|
|||||||
|
github.com/imdario/mergo v0.3.14/go.mod h1:WBLT9ZmE3lPoWsEzCh9LPo3TiwVN+ZKEjmz+hD27ysY=
|
||||||
|
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
|
||||||
|
github.com/silas/dag v0.0.0-20211117232152-9d50aa809f35/go.mod h1:7RTUFBdIRC9nZ7/3RyRNH1bdqIShrDejd1YbLwgPS+I=
|
||||||
|
go.unistack.org/micro-wrapper-requestid/v3 v3.8.7 h1:gXyiO7gPT0wNE7XEJIrwS2Y4cqylbeBMmlNYGwIS3io=
|
||||||
|
go.unistack.org/micro-wrapper-requestid/v3 v3.8.7/go.mod h1:W92jAyoOoTFgGXgYu26BZi+R+IwJVH5N+BPnnp0NuiY=
|
||||||
|
go.unistack.org/micro/v3 v3.10.19/go.mod h1:XIArw29f0b3uvF4cq96X/nQt2f0J2OGnjh8J+DBbC0s=
|
||||||
go.unistack.org/micro/v3 v3.10.25 h1:A0epdZHOqjnXx103wwFhPKgmvVVbScvfbmn3HmHz1wE=
|
go.unistack.org/micro/v3 v3.10.25 h1:A0epdZHOqjnXx103wwFhPKgmvVVbScvfbmn3HmHz1wE=
|
||||||
go.unistack.org/micro/v3 v3.10.25/go.mod h1:ALkeXpqChYDjx8KPi7tz9mmIyOnob6nlNswsg8BnZjQ=
|
go.unistack.org/micro/v3 v3.10.25/go.mod h1:ALkeXpqChYDjx8KPi7tz9mmIyOnob6nlNswsg8BnZjQ=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
12
options.go
12
options.go
@ -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()
|
||||||
}
|
}
|
||||||
|
23
stmt.go
23
stmt.go
@ -6,6 +6,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
requestid "go.unistack.org/micro-wrapper-requestid/v3"
|
||||||
"go.unistack.org/micro/v3/tracer"
|
"go.unistack.org/micro/v3/tracer"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -140,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 {
|
||||||
@ -214,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()
|
||||||
|
Loading…
Reference in New Issue
Block a user