Compare commits

..

8 Commits
v3 ... master

Author SHA1 Message Date
bab2833cf7 Merge pull request 'add err skip' (#81) from devstigneev/micro-wrapper-sql:issue_73 into master
Some checks failed
build / test (push) Successful in 1m54s
build / lint (push) Successful in 9m17s
codeql / analyze (go) (push) Failing after 13m50s
Reviewed-on: #81
2024-04-11 11:40:28 +03:00
564efef60b moved compare error to LoggerObserver
Some checks failed
automerge / automerge (pull_request) Has been skipped
dependabot-automerge / automerge (pull_request) Has been skipped
autoapprove / autoapprove (pull_request) Successful in 8s
codeql / analyze (go) (pull_request) Has been cancelled
prbuild / test (pull_request) Has been cancelled
prbuild / lint (pull_request) Has been cancelled
2024-04-11 11:38:09 +03:00
cb9d32bf1c add err skip
Some checks failed
automerge / automerge (pull_request) Has been skipped
dependabot-automerge / automerge (pull_request) Has been skipped
autoapprove / autoapprove (pull_request) Successful in 8s
codeql / analyze (go) (pull_request) Has been cancelled
prbuild / test (pull_request) Has been cancelled
prbuild / lint (pull_request) Has been cancelled
2024-04-07 22:12:39 +03:00
c363033abd fix label names
Some checks failed
build / test (push) Failing after 1m29s
build / lint (push) Failing after 2m34s
codeql / analyze (go) (push) Failing after 3m22s
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2023-09-05 07:08:08 +03:00
dd914ed9fb Merge pull request 'updatev4' (#80) from updatev4 into master
Some checks failed
build / test (push) Failing after 1m28s
build / lint (push) Failing after 2m38s
codeql / analyze (go) (push) Failing after 3m21s
Reviewed-on: #80
2023-09-01 14:49:52 +03:00
7d2787c3a6 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
2023-09-01 14:48:57 +03:00
a81da5bd2d fixup metrics and tracing
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2023-09-01 00:23:26 +03:00
44cd4a1e16 Merge pull request 'use generated interface' (#76) from generator into master
Some checks failed
build / test (push) Failing after 5s
build / lint (push) Failing after 5s
codeql / analyze (go) (push) Failing after 6s
Reviewed-on: #76
2023-06-10 13:10:15 +03:00
9 changed files with 182 additions and 240 deletions

9
.gitignore vendored
View File

@ -4,6 +4,7 @@
*.dll *.dll
*.so *.so
*.dylib *.dylib
bin
# Test binary, built with `go test -c` # Test binary, built with `go test -c`
*.test *.test
@ -13,3 +14,11 @@
# Dependency directories (remove the comment below to include it) # Dependency directories (remove the comment below to include it)
# vendor/ # vendor/
# Go workspace file
go.work
# General
.DS_Store
.idea
.vscode

96
conn.go
View File

@ -6,8 +6,8 @@ import (
"fmt" "fmt"
"time" "time"
requestid "go.unistack.org/micro-wrapper-requestid/v3" requestid "go.unistack.org/micro-wrapper-requestid/v4"
"go.unistack.org/micro/v3/tracer" "go.unistack.org/micro/v4/tracer"
) )
var ( var (
@ -43,7 +43,7 @@ func (w *wrapperConn) Close() error {
} else { } else {
ctx = context.Background() ctx = context.Background()
} }
_ = ctx
labels := []string{labelMethod, "Close"} labels := []string{labelMethod, "Close"}
ts := time.Now() ts := time.Now()
err := w.conn.Close() err := w.conn.Close()
@ -56,11 +56,11 @@ 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 && w.opts.Logger.V(w.opts.LoggerLevel) { if w.opts.LoggerEnabled && w.opts.Logger.V(w.opts.LoggerLevel) {
w.opts.Logger.Log(ctx, w.opts.LoggerLevel, w.opts.LoggerObserver(ctx, "Close", getCallerName(), td, err)...) w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "Close", getCallerName(), td, err)...).Log(ctx, w.opts.LoggerLevel)
} }
*/
return err return err
} }
@ -82,31 +82,28 @@ func (w *wrapperConn) Begin() (driver.Tx, error) {
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 && w.opts.Logger.V(w.opts.LoggerLevel) { if w.opts.LoggerEnabled && w.opts.Logger.V(w.opts.LoggerLevel) {
w.opts.Logger.Log(ctx, w.opts.LoggerLevel, w.opts.LoggerObserver(ctx, "Begin", getCallerName(), td, err)...) w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "Begin", getCallerName(), 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()
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.V(w.opts.LoggerLevel) { if w.opts.LoggerEnabled && w.opts.Logger.V(w.opts.LoggerLevel) {
w.opts.Logger.Log(ctx, w.opts.LoggerLevel, w.opts.LoggerObserver(ctx, "Begin", getCallerName(), td, err)...) w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "Begin", getCallerName(), 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
} }
// 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) {
name := getQueryName(ctx)
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("db.method", "BeginTx") span.AddLabels("db.method", "BeginTx")
name := getQueryName(ctx)
span.AddLabels("db.statement", name) span.AddLabels("db.statement", name)
if id, ok := ctx.Value(requestid.XRequestIDKey{}).(string); ok { if id, ok := ctx.Value(requestid.XRequestIDKey).(string); ok {
span.AddLabels("x-request-id", id) span.AddLabels("x-request-id", id)
} }
labels := []string{labelMethod, "BeginTx", labelQuery, name} labels := []string{labelMethod, "BeginTx", labelQuery, name}
@ -125,19 +122,15 @@ func (w *wrapperConn) BeginTx(ctx context.Context, opts driver.TxOptions) (drive
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.SetStatus(tracer.SpanStatusError, err.Error()) span.SetStatus(tracer.SpanStatusError, err.Error())
/*
if w.opts.LoggerEnabled && w.opts.Logger.V(w.opts.LoggerLevel) { if w.opts.LoggerEnabled && w.opts.Logger.V(w.opts.LoggerLevel) {
w.opts.Logger.Log(ctx, w.opts.LoggerLevel, w.opts.LoggerObserver(ctx, "BeginTx", getCallerName(), td, err)...) w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "BeginTx", getCallerName(), 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 && w.opts.Logger.V(w.opts.LoggerLevel) { if w.opts.LoggerEnabled && w.opts.Logger.V(w.opts.LoggerLevel) {
w.opts.Logger.Log(ctx, w.opts.LoggerLevel, w.opts.LoggerObserver(ctx, "BeginTx", getCallerName(), td, err)...) w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "BeginTx", getCallerName(), td, err)...).Log(ctx, w.opts.LoggerLevel)
} }
*/
return &wrapperTx{tx: tx, opts: w.opts, ctx: ctx, span: span}, nil return &wrapperTx{tx: tx, opts: w.opts, ctx: ctx, span: span}, nil
} }
@ -149,7 +142,7 @@ func (w *wrapperConn) Prepare(query string) (driver.Stmt, error) {
} else { } else {
ctx = context.Background() ctx = context.Background()
} }
_ = ctx
labels := []string{labelMethod, "Prepare", labelQuery, getCallerName()} labels := []string{labelMethod, "Prepare", labelQuery, getCallerName()}
ts := time.Now() ts := time.Now()
stmt, err := w.conn.Prepare(query) stmt, err := w.conn.Prepare(query)
@ -159,21 +152,19 @@ func (w *wrapperConn) Prepare(query string) (driver.Stmt, error) {
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 && w.opts.Logger.V(w.opts.LoggerLevel) { if w.opts.LoggerEnabled && w.opts.Logger.V(w.opts.LoggerLevel) {
w.opts.Logger.Log(ctx, w.opts.LoggerLevel, w.opts.LoggerObserver(ctx, "Prepare", getCallerName(), td, err)...) w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "Prepare", getCallerName(), 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()
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.V(w.opts.LoggerLevel) { if w.opts.LoggerEnabled && w.opts.Logger.V(w.opts.LoggerLevel) {
w.opts.Logger.Log(ctx, w.opts.LoggerLevel, w.opts.LoggerObserver(ctx, "Prepare", getCallerName(), td, err)...) w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "Prepare", getCallerName(), td, err)...).Log(ctx, w.opts.LoggerLevel)
} }
*/
return wrapStmt(stmt, query, w.opts), nil return wrapStmt(stmt, query, w.opts), nil
} }
@ -182,15 +173,15 @@ func (w *wrapperConn) PrepareContext(ctx context.Context, query string) (driver.
var nctx context.Context var nctx context.Context
var span tracer.Span var span tracer.Span
name := getQueryName(ctx)
if w.ctx != nil { if w.ctx != nil {
nctx, span = w.opts.Tracer.Start(w.ctx, "sdk.database", tracer.WithSpanKind(tracer.SpanKindClient)) nctx, span = w.opts.Tracer.Start(w.ctx, "sdk.database", tracer.WithSpanKind(tracer.SpanKindClient))
} 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("db.method", "PrepareContext") span.AddLabels("db.method", "PrepareContext")
name := getQueryName(ctx)
span.AddLabels("db.statement", name) span.AddLabels("db.statement", name)
if id, ok := ctx.Value(requestid.XRequestIDKey{}).(string); ok { if id, ok := ctx.Value(requestid.XRequestIDKey).(string); ok {
span.AddLabels("x-request-id", id) span.AddLabels("x-request-id", id)
} }
labels := []string{labelMethod, "PrepareContext", labelQuery, name} labels := []string{labelMethod, "PrepareContext", labelQuery, name}
@ -208,21 +199,18 @@ func (w *wrapperConn) PrepareContext(ctx context.Context, query string) (driver.
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.SetStatus(tracer.SpanStatusError, err.Error()) span.SetStatus(tracer.SpanStatusError, err.Error())
/*
if w.opts.LoggerEnabled && w.opts.Logger.V(w.opts.LoggerLevel) { if w.opts.LoggerEnabled && w.opts.Logger.V(w.opts.LoggerLevel) {
w.opts.Logger.Log(ctx, w.opts.LoggerLevel, w.opts.LoggerObserver(ctx, "PrepareContext", getCallerName(), td, err)...) w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "PrepareContext", getCallerName(), 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()
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.V(w.opts.LoggerLevel) { if w.opts.LoggerEnabled && w.opts.Logger.V(w.opts.LoggerLevel) {
w.opts.Logger.Log(ctx, w.opts.LoggerLevel, w.opts.LoggerObserver(ctx, "PrepareContext", getCallerName(), td, err)...) w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "PrepareContext", getCallerName(), td, err)...).Log(ctx, w.opts.LoggerLevel)
} }
*/
return wrapStmt(stmt, query, w.opts), nil return wrapStmt(stmt, query, w.opts), nil
} }
@ -234,7 +222,6 @@ func (w *wrapperConn) Exec(query string, args []driver.Value) (driver.Result, er
} else { } else {
ctx = context.Background() ctx = context.Background()
} }
_ = ctx
labels := []string{labelMethod, "Exec", labelQuery, getCallerName()} labels := []string{labelMethod, "Exec", labelQuery, getCallerName()}
// nolint:staticcheck // nolint:staticcheck
@ -254,11 +241,9 @@ 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 && w.opts.Logger.V(w.opts.LoggerLevel) { if w.opts.LoggerEnabled && w.opts.Logger.V(w.opts.LoggerLevel) {
w.opts.Logger.Log(ctx, w.opts.LoggerLevel, w.opts.LoggerObserver(ctx, "Exec", getCallerName(), td, err)...) w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "Exec", getCallerName(), td, err)...).Log(ctx, w.opts.LoggerLevel)
} }
*/
return res, err return res, err
} }
@ -267,15 +252,15 @@ func (w *wrapperConn) ExecContext(ctx context.Context, query string, args []driv
var nctx context.Context var nctx context.Context
var span tracer.Span var span tracer.Span
name := getQueryName(ctx)
if w.ctx != nil { if w.ctx != nil {
nctx, span = w.opts.Tracer.Start(w.ctx, "sdk.database", tracer.WithSpanKind(tracer.SpanKindClient)) nctx, span = w.opts.Tracer.Start(w.ctx, "sdk.database", tracer.WithSpanKind(tracer.SpanKindClient))
} 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("db.method", "ExecContext") span.AddLabels("db.method", "ExecContext")
name := getQueryName(ctx)
span.AddLabels("db.statement", name) span.AddLabels("db.statement", name)
if id, ok := ctx.Value(requestid.XRequestIDKey{}).(string); ok { if id, ok := ctx.Value(requestid.XRequestIDKey).(string); ok {
span.AddLabels("x-request-id", id) span.AddLabels("x-request-id", id)
} }
defer span.Finish() defer span.Finish()
@ -303,11 +288,10 @@ 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 && w.opts.Logger.V(w.opts.LoggerLevel) { if w.opts.LoggerEnabled && w.opts.Logger.V(w.opts.LoggerLevel) {
w.opts.Logger.Log(ctx, w.opts.LoggerLevel, w.opts.LoggerObserver(ctx, "ExecContext", getCallerName(), td, err)...) w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "ExecContext", getCallerName(), td, err)...).Log(ctx, w.opts.LoggerLevel)
} }
*/
return res, err return res, err
} }
@ -325,17 +309,13 @@ func (w *wrapperConn) Ping(ctx context.Context) error {
} }
var nctx context.Context var nctx context.Context
nctx = ctx
/*
var span tracer.Span var span tracer.Span
if w.ctx != nil { if w.ctx != nil {
nctx, span = w.opts.Tracer.Start(w.ctx, "sdk.database", tracer.WithSpanKind(tracer.SpanKindClient)) nctx, span = w.opts.Tracer.Start(w.ctx, "sdk.database", tracer.WithSpanKind(tracer.SpanKindClient))
} 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("db.method", "Ping")
defer span.Finish() defer span.Finish()
*/
labels := []string{labelMethod, "Ping"} labels := []string{labelMethod, "Ping"}
ts := time.Now() ts := time.Now()
err := conn.Ping(nctx) err := conn.Ping(nctx)
@ -343,12 +323,10 @@ func (w *wrapperConn) Ping(ctx context.Context) error {
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()
// span.SetStatus(tracer.SpanStatusError, err.Error()) span.SetStatus(tracer.SpanStatusError, err.Error())
/*
if w.opts.LoggerEnabled && w.opts.Logger.V(w.opts.LoggerLevel) { if w.opts.LoggerEnabled && w.opts.Logger.V(w.opts.LoggerLevel) {
w.opts.Logger.Log(ctx, w.opts.LoggerLevel, w.opts.LoggerObserver(ctx, "Ping", getCallerName(), td, err)...) w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "Ping", getCallerName(), td, err)...).Log(ctx, w.opts.LoggerLevel)
} }
*/
return err return err
} else { } else {
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelSuccess)...).Inc() w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelSuccess)...).Inc()
@ -367,7 +345,6 @@ func (w *wrapperConn) Query(query string, args []driver.Value) (driver.Rows, err
} else { } else {
ctx = context.Background() ctx = context.Background()
} }
_ = ctx
// nolint:staticcheck // nolint:staticcheck
conn, ok := w.conn.(driver.Queryer) conn, ok := w.conn.(driver.Queryer)
if !ok { if !ok {
@ -386,11 +363,9 @@ 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 && w.opts.Logger.V(w.opts.LoggerLevel) { if w.opts.LoggerEnabled && w.opts.Logger.V(w.opts.LoggerLevel) {
w.opts.Logger.Log(ctx, w.opts.LoggerLevel, w.opts.LoggerObserver(ctx, "Query", getCallerName(), td, err)...) w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "Query", getCallerName(), td, err)...).Log(ctx, w.opts.LoggerLevel)
} }
*/
return rows, err return rows, err
} }
@ -399,15 +374,15 @@ func (w *wrapperConn) QueryContext(ctx context.Context, query string, args []dri
var nctx context.Context var nctx context.Context
var span tracer.Span var span tracer.Span
name := getQueryName(ctx)
if w.ctx != nil { if w.ctx != nil {
nctx, span = w.opts.Tracer.Start(w.ctx, "sdk.database", tracer.WithSpanKind(tracer.SpanKindClient)) nctx, span = w.opts.Tracer.Start(w.ctx, "sdk.database", tracer.WithSpanKind(tracer.SpanKindClient))
} 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("db.method", "QueryContext") span.AddLabels("db.method", "QueryContext")
name := getQueryName(ctx)
span.AddLabels("db.statement", name) span.AddLabels("db.statement", name)
if id, ok := ctx.Value(requestid.XRequestIDKey{}).(string); ok { if id, ok := ctx.Value(requestid.XRequestIDKey).(string); ok {
span.AddLabels("x-request-id", id) span.AddLabels("x-request-id", id)
} }
defer span.Finish() defer span.Finish()
@ -432,11 +407,10 @@ 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 && w.opts.Logger.V(w.opts.LoggerLevel) { if w.opts.LoggerEnabled && w.opts.Logger.V(w.opts.LoggerLevel) {
w.opts.Logger.Log(ctx, w.opts.LoggerLevel, w.opts.LoggerObserver(ctx, "QueryContext", getCallerName(), td, err)...) w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "QueryContext", getCallerName(), td, err)...).Log(ctx, w.opts.LoggerLevel)
} }
*/
return rows, err return rows, err
} }

View File

@ -78,12 +78,10 @@ func (w *wrapperDriver) Open(name string) (driver.Conn, error) {
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)
/*
if w.opts.LoggerEnabled { if w.opts.LoggerEnabled {
w.opts.Logger.Log(w.ctx, w.opts.LoggerLevel, w.opts.LoggerObserver(w.ctx, "Open", getCallerName(), td, err)...) w.opts.Logger.Fields(w.opts.LoggerObserver(w.ctx, "Open", getCallerName(), td, err)...).Log(w.ctx, w.opts.LoggerLevel)
} }
*/
_ = td
if err != nil { if err != nil {
return nil, err return nil, err
} }

18
go.mod
View File

@ -1,18 +1,8 @@
module go.unistack.org/micro-wrapper-sql/v3 module go.unistack.org/micro-wrapper-sql/v4
go 1.22 go 1.20
toolchain go1.23.2
require ( require (
go.unistack.org/micro-wrapper-requestid/v3 v3.9.2 go.unistack.org/micro-wrapper-requestid/v4 v4.0.3
go.unistack.org/micro/v3 v3.10.97 go.unistack.org/micro/v4 v4.0.8
)
require (
go.unistack.org/micro-proto/v3 v3.4.1 // indirect
golang.org/x/sys v0.26.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20241007155032-5fefd90f89a9 // indirect
google.golang.org/grpc v1.67.1 // indirect
google.golang.org/protobuf v1.35.1 // indirect
) )

43
go.sum
View File

@ -1,39 +1,4 @@
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= go.unistack.org/micro-wrapper-requestid/v4 v4.0.3 h1:LhloK8yLLsov4XiKw9i20wdBUfvWCG1TaBNgNVtNa8Q=
go.unistack.org/micro-proto/v3 v3.4.1 h1:UTjLSRz2YZuaHk9iSlVqqsA50JQNAEK2ZFboGqtEa9Q= go.unistack.org/micro-wrapper-requestid/v4 v4.0.3/go.mod h1:vRIkCMyhbBVus1xQlr/Fef65n07znrBGkpSCpjkEiqk=
go.unistack.org/micro-proto/v3 v3.4.1/go.mod h1:okx/cnOhzuCX0ggl/vToatbCupi0O44diiiLLsZ93Zo= go.unistack.org/micro/v4 v4.0.8 h1:D5n18+fhyRjJkKSPdIcW1ratWUuzy9TzrS5ud/qImWg=
go.unistack.org/micro-wrapper-requestid/v3 v3.8.10 h1:9QZy6w1HU2RCppZ1t8wJsDg1pk9RbvzUFYurq3Ai29Y= go.unistack.org/micro/v4 v4.0.8/go.mod h1:QT3gOIE4qGgBiQGm2Pad/62Sl5R53QfrgYHD448aX14=
go.unistack.org/micro-wrapper-requestid/v3 v3.8.10/go.mod h1:3tY5+DwQM7l07chnpu/Wzwnne/ZV75VsUwXR54/WYzo=
go.unistack.org/micro-wrapper-requestid/v3 v3.9.2 h1:Wu0oIGJieH37xroCjlaPivuqTGmv5l5KWgVaRQWEQMY=
go.unistack.org/micro-wrapper-requestid/v3 v3.9.2/go.mod h1:cqgjdSSASTnOLjvwndP9bi0b8DbuoKbDII884R5puwU=
go.unistack.org/micro/v3 v3.10.57 h1:VxG7Cs7kBOgxgQlP+K8TvTxIgh2pmqAwmAaKdTAQNtQ=
go.unistack.org/micro/v3 v3.10.57/go.mod h1:erMgt3Bl7vQQ0e9UpQyR5NlLiZ9pKeEJ9+1tfYFaqUg=
go.unistack.org/micro/v3 v3.10.91 h1:vuJY4tXwpqimwIkEJ3TozMYNVQQs+C5QMlQWPgSY/YM=
go.unistack.org/micro/v3 v3.10.91/go.mod h1:erMgt3Bl7vQQ0e9UpQyR5NlLiZ9pKeEJ9+1tfYFaqUg=
go.unistack.org/micro/v3 v3.10.97 h1:8l7fv+i06/PjPrBBhRC/ZQkWGIOuHPg3jJN0vktYE78=
go.unistack.org/micro/v3 v3.10.97/go.mod h1:YzMldzHN9Ei+zy5t/Psu7RUWDZwUfrNYiStSQtTz90g=
golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4=
golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o=
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo=
golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240401170217-c3f982113cda h1:LI5DOvAxUPMv/50agcLLoo+AdWc1irS9Rzz4vPuD1V4=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240401170217-c3f982113cda/go.mod h1:WtryC6hu0hhx87FDGxWCDptyssuo68sk10vYjF+T9fY=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240903143218-8af14fe29dc1 h1:pPJltXNxVzT4pK9yD8vR9X75DaWYYmLGMsEvBfFQZzQ=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240903143218-8af14fe29dc1/go.mod h1:UqMtugtsSgubUsoxbuAoiCXvqvErP7Gf0so0mK9tHxU=
google.golang.org/genproto/googleapis/rpc v0.0.0-20241007155032-5fefd90f89a9 h1:QCqS/PdaHTSWGvupk2F/ehwHtGc0/GYkT+3GAcR1CCc=
google.golang.org/genproto/googleapis/rpc v0.0.0-20241007155032-5fefd90f89a9/go.mod h1:GX3210XPVPUjJbTUbvwI8f2IpZDMZuPJWDzDuebbviI=
google.golang.org/grpc v1.63.2 h1:MUeiw1B2maTVZthpU5xvASfTh3LDbxHd6IJ6QQVU+xM=
google.golang.org/grpc v1.63.2/go.mod h1:WAX/8DgncnokcFUldAxq7GeB5DXHDbMF+lLvDomNkRA=
google.golang.org/grpc v1.67.0 h1:IdH9y6PF5MPSdAntIcpjQ+tXO41pcQsfZV2RxtQgVcw=
google.golang.org/grpc v1.67.0/go.mod h1:1gLDyUQU7CTLJI90u3nXZ9ekeghjeM7pTDZlqFNg2AA=
google.golang.org/grpc v1.67.1 h1:zWnc1Vrcno+lHZCOofnIMvycFcc0QRGIzm9dhnDX68E=
google.golang.org/grpc v1.67.1/go.mod h1:1gLDyUQU7CTLJI90u3nXZ9ekeghjeM7pTDZlqFNg2AA=
google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI=
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
google.golang.org/protobuf v1.35.1 h1:m3LfL6/Ca+fqnjnlqQXNpFPABW1UD7mjh8KO2mKFytA=
google.golang.org/protobuf v1.35.1/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=

View File

@ -2,21 +2,25 @@ package wrapper
import ( import (
"context" "context"
"database/sql"
"errors"
"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 (
// DefaultMeterStatsInterval holds default stats interval // DefaultMeterStatsInterval holds default stats interval
DefaultMeterStatsInterval = 5 * time.Second DefaultMeterStatsInterval = 5 * time.Second
// DefaultMeterMetricPrefix holds default metric prefix
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{}{"db.method", method, "took", fmt.Sprintf("%v", td)} labels := []interface{}{"db.method", method, "took", fmt.Sprintf("%v", td)}
if err != nil { if err != nil && !errors.Is(err, sql.ErrNoRows) {
labels = append(labels, "error", err.Error()) labels = append(labels, "error", err.Error())
} }
if query != labelUnknown { if query != labelUnknown {
@ -27,19 +31,19 @@ var (
) )
var ( var (
MaxOpenConnections = "micro_sql_max_open_conn" MaxOpenConnections = "max_open_conn"
OpenConnections = "micro_sql_open_conn" OpenConnections = "open_conn"
InuseConnections = "micro_sql_inuse_conn" InuseConnections = "inuse_conn"
IdleConnections = "micro_sql_idle_conn" IdleConnections = "idle_conn"
WaitConnections = "micro_sql_waited_conn" WaitConnections = "waited_conn"
BlockedSeconds = "micro_sql_blocked_seconds" BlockedSeconds = "blocked_seconds"
MaxIdleClosed = "micro_sql_max_idle_closed" MaxIdleClosed = "max_idle_closed"
MaxIdletimeClosed = "micro_sql_closed_max_idle" MaxIdletimeClosed = "closed_max_idle"
MaxLifetimeClosed = "micro_sql_closed_max_lifetime" MaxLifetimeClosed = "closed_max_lifetime"
meterRequestTotal = "micro_sql_request_total" meterRequestTotal = "request_total"
meterRequestLatencyMicroseconds = "micro_sql_latency_microseconds" meterRequestLatencyMicroseconds = "latency_microseconds"
meterRequestDurationSeconds = "micro_sql_request_duration_seconds" meterRequestDurationSeconds = "request_duration_seconds"
labelUnknown = "unknown" labelUnknown = "unknown"
labelQuery = "db_statement" labelQuery = "db_statement"
@ -58,6 +62,7 @@ type Options struct {
Tracer tracer.Tracer Tracer tracer.Tracer
DatabaseHost string DatabaseHost string
DatabaseName string DatabaseName string
MeterMetricPrefix string
MeterStatsInterval time.Duration MeterStatsInterval time.Duration
LoggerLevel logger.Level LoggerLevel logger.Level
LoggerEnabled bool LoggerEnabled bool
@ -74,6 +79,7 @@ func NewOptions(opts ...Option) Options {
Meter: meter.DefaultMeter, Meter: meter.DefaultMeter,
Tracer: tracer.DefaultTracer, Tracer: tracer.DefaultTracer,
MeterStatsInterval: DefaultMeterStatsInterval, MeterStatsInterval: DefaultMeterStatsInterval,
MeterMetricPrefix: DefaultMeterMetricPrefix,
LoggerLevel: logger.ErrorLevel, LoggerLevel: logger.ErrorLevel,
LoggerObserver: DefaultLoggerObserver, LoggerObserver: DefaultLoggerObserver,
} }
@ -82,13 +88,14 @@ func NewOptions(opts ...Option) Options {
} }
options.Meter = options.Meter.Clone( options.Meter = options.Meter.Clone(
meter.MetricPrefix(options.MeterMetricPrefix),
meter.Labels( meter.Labels(
labelHost, options.DatabaseHost, labelHost, options.DatabaseHost,
labelDatabase, options.DatabaseName, labelDatabase, options.DatabaseName,
), ),
) )
options.Logger = options.Logger.Clone(logger.WithAddCallerSkipCount(1)) options.Logger = options.Logger.Clone(logger.WithCallerSkipCount(1))
return options return options
} }
@ -100,6 +107,13 @@ func MetricInterval(td time.Duration) Option {
} }
} }
// MetricPrefix specifies prefix for each metric
func MetricPrefix(pref string) Option {
return func(o *Options) {
o.MeterMetricPrefix = pref
}
}
func DatabaseHost(host string) Option { func DatabaseHost(host string) Option {
return func(o *Options) { return func(o *Options) {
o.DatabaseHost = host o.DatabaseHost = host

View File

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

64
stmt.go
View File

@ -6,8 +6,8 @@ import (
"fmt" "fmt"
"time" "time"
requestid "go.unistack.org/micro-wrapper-requestid/v3" requestid "go.unistack.org/micro-wrapper-requestid/v4"
"go.unistack.org/micro/v3/tracer" "go.unistack.org/micro/v4/tracer"
) )
var ( var (
@ -33,7 +33,6 @@ func (w *wrapperStmt) Close() error {
} else { } else {
ctx = context.Background() ctx = context.Background()
} }
_ = ctx
labels := []string{labelMethod, "Close"} labels := []string{labelMethod, "Close"}
ts := time.Now() ts := time.Now()
err := w.stmt.Close() err := w.stmt.Close()
@ -46,11 +45,10 @@ 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 && w.opts.Logger.V(w.opts.LoggerLevel) { if w.opts.LoggerEnabled && w.opts.Logger.V(w.opts.LoggerLevel) {
w.opts.Logger.Log(ctx, w.opts.LoggerLevel, w.opts.LoggerObserver(ctx, "Close", getCallerName(), td, err)...) w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "Close", getCallerName(), td, err)).Log(ctx, w.opts.LoggerLevel)
} }
*/
return err return err
} }
@ -76,7 +74,6 @@ func (w *wrapperStmt) Exec(args []driver.Value) (driver.Result, error) {
} else { } else {
ctx = context.Background() ctx = context.Background()
} }
_ = ctx
labels := []string{labelMethod, "Exec"} labels := []string{labelMethod, "Exec"}
ts := time.Now() ts := time.Now()
res, err := w.stmt.Exec(args) // nolint:staticcheck res, err := w.stmt.Exec(args) // nolint:staticcheck
@ -89,11 +86,11 @@ 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 && w.opts.Logger.V(w.opts.LoggerLevel) { if w.opts.LoggerEnabled && w.opts.Logger.V(w.opts.LoggerLevel) {
w.opts.Logger.Log(ctx, w.opts.LoggerLevel, w.opts.LoggerObserver(ctx, "Exec", getCallerName(), td, err)...) w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "Exec", getCallerName(), td, err)).Log(ctx, w.opts.LoggerLevel)
} }
*/
return res, err return res, err
} }
@ -105,7 +102,6 @@ func (w *wrapperStmt) Query(args []driver.Value) (driver.Rows, error) {
} else { } else {
ctx = context.Background() ctx = context.Background()
} }
_ = ctx
labels := []string{labelMethod, "Query"} labels := []string{labelMethod, "Query"}
ts := time.Now() ts := time.Now()
rows, err := w.stmt.Query(args) // nolint:staticcheck rows, err := w.stmt.Query(args) // nolint:staticcheck
@ -118,11 +114,11 @@ 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 && w.opts.Logger.V(w.opts.LoggerLevel) { if w.opts.LoggerEnabled && w.opts.Logger.V(w.opts.LoggerLevel) {
w.opts.Logger.Log(ctx, w.opts.LoggerLevel, w.opts.LoggerObserver(ctx, "Query", getCallerName(), td, err)...) w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "Query", getCallerName(), td, err)).Log(ctx, w.opts.LoggerLevel)
} }
*/
return rows, err return rows, err
} }
@ -140,19 +136,19 @@ func (w *wrapperStmt) ExecContext(ctx context.Context, args []driver.NamedValue)
var nctx context.Context var nctx context.Context
var span tracer.Span var span tracer.Span
name := getQueryName(ctx)
if w.ctx != nil { if w.ctx != nil {
nctx, span = w.opts.Tracer.Start(w.ctx, "sdk.database", tracer.WithSpanKind(tracer.SpanKindClient)) nctx, span = w.opts.Tracer.Start(w.ctx, "sdk.database", tracer.WithSpanKind(tracer.SpanKindClient))
} 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("db.method", "ExecContext") span.AddLabels("db.method", "ExecContext")
name := getQueryName(ctx)
span.AddLabels("db.statement", name) span.AddLabels("db.statement", name)
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 { if id, ok := ctx.Value(requestid.XRequestIDKey).(string); ok {
span.AddLabels("x-request-id", id) span.AddLabels("x-request-id", id)
} }
labels := []string{labelMethod, "ExecContext", labelQuery, name} labels := []string{labelMethod, "ExecContext", labelQuery, name}
@ -170,11 +166,10 @@ func (w *wrapperStmt) ExecContext(ctx context.Context, args []driver.NamedValue)
} }
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.V(w.opts.LoggerLevel) { if w.opts.LoggerEnabled && w.opts.Logger.V(w.opts.LoggerLevel) {
w.opts.Logger.Log(ctx, w.opts.LoggerLevel, w.opts.LoggerObserver(ctx, "ExecContext", name, td, err)...) w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "ExecContext", name, td, err)).Log(ctx, w.opts.LoggerLevel)
} }
*/
return res, err return res, err
} }
@ -182,11 +177,10 @@ func (w *wrapperStmt) ExecContext(ctx context.Context, args []driver.NamedValue)
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.SetStatus(tracer.SpanStatusError, err.Error()) span.SetStatus(tracer.SpanStatusError, err.Error())
/*
if w.opts.LoggerEnabled && w.opts.Logger.V(w.opts.LoggerLevel) { if w.opts.LoggerEnabled && w.opts.Logger.V(w.opts.LoggerLevel) {
w.opts.Logger.Log(ctx, w.opts.LoggerLevel, w.opts.LoggerObserver(ctx, "ExecContext", name, 0, err)...) 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()
@ -202,11 +196,11 @@ func (w *wrapperStmt) ExecContext(ctx context.Context, args []driver.NamedValue)
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.V(w.opts.LoggerLevel) { if w.opts.LoggerEnabled && w.opts.Logger.V(w.opts.LoggerLevel) {
w.opts.Logger.Log(ctx, w.opts.LoggerLevel, w.opts.LoggerObserver(ctx, "ExecContext", name, td, err)...) w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "ExecContext", name, td, err)).Log(ctx, w.opts.LoggerLevel)
} }
*/
return res, err return res, err
} }
@ -215,19 +209,19 @@ func (w *wrapperStmt) QueryContext(ctx context.Context, args []driver.NamedValue
var nctx context.Context var nctx context.Context
var span tracer.Span var span tracer.Span
name := getQueryName(ctx)
if w.ctx != nil { if w.ctx != nil {
nctx, span = w.opts.Tracer.Start(w.ctx, "sdk.database", tracer.WithSpanKind(tracer.SpanKindClient)) nctx, span = w.opts.Tracer.Start(w.ctx, "sdk.database", tracer.WithSpanKind(tracer.SpanKindClient))
} 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("db.method", "QueryContext") span.AddLabels("db.method", "QueryContext")
name := getQueryName(ctx)
span.AddLabels("db.statement", name) span.AddLabels("db.statement", name)
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 { if id, ok := ctx.Value(requestid.XRequestIDKey).(string); ok {
span.AddLabels("x-request-id", id) span.AddLabels("x-request-id", id)
} }
labels := []string{labelMethod, "QueryContext", labelQuery, name} labels := []string{labelMethod, "QueryContext", labelQuery, name}
@ -245,11 +239,10 @@ func (w *wrapperStmt) QueryContext(ctx context.Context, args []driver.NamedValue
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.V(w.opts.LoggerLevel) { if w.opts.LoggerEnabled && w.opts.Logger.V(w.opts.LoggerLevel) {
w.opts.Logger.Log(ctx, w.opts.LoggerLevel, w.opts.LoggerObserver(ctx, "QueryContext", name, td, err)...) w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "QueryContext", name, td, err)).Log(ctx, w.opts.LoggerLevel)
} }
*/
return rows, err return rows, err
} }
@ -258,11 +251,10 @@ func (w *wrapperStmt) QueryContext(ctx context.Context, args []driver.NamedValue
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelFailure)...).Inc() w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelFailure)...).Inc()
span.SetStatus(tracer.SpanStatusError, err.Error()) span.SetStatus(tracer.SpanStatusError, err.Error())
/*
if w.opts.LoggerEnabled && w.opts.Logger.V(w.opts.LoggerLevel) { if w.opts.LoggerEnabled && w.opts.Logger.V(w.opts.LoggerLevel) {
w.opts.Logger.Log(ctx, w.opts.LoggerLevel, w.opts.LoggerObserver(ctx, "QueryContext", name, 0, err)...) 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()
@ -278,10 +270,10 @@ func (w *wrapperStmt) QueryContext(ctx context.Context, args []driver.NamedValue
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.V(w.opts.LoggerLevel) { if w.opts.LoggerEnabled && w.opts.Logger.V(w.opts.LoggerLevel) {
w.opts.Logger.Log(ctx, w.opts.LoggerLevel, w.opts.LoggerObserver(ctx, "QueryContext", name, td, err)...) w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "QueryContext", name, td, err)).Log(ctx, w.opts.LoggerLevel)
} }
*/
return rows, err return rows, err
} }

18
tx.go
View File

@ -5,7 +5,7 @@ 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)(nil) var _ driver.Tx = (*wrapperTx)(nil)
@ -23,18 +23,18 @@ func (w *wrapperTx) Commit() error {
ts := time.Now() ts := time.Now()
err := w.tx.Commit() err := w.tx.Commit()
td := time.Since(ts) td := time.Since(ts)
_ = td
if w.span != nil { if w.span != nil {
if err != nil { if err != nil {
w.span.SetStatus(tracer.SpanStatusError, err.Error()) w.span.SetStatus(tracer.SpanStatusError, err.Error())
} }
w.span.Finish() w.span.Finish()
} }
/*
if w.opts.LoggerEnabled && w.opts.Logger.V(w.opts.LoggerLevel) { if w.opts.LoggerEnabled && w.opts.Logger.V(w.opts.LoggerLevel) {
w.opts.Logger.Log(w.ctx, w.opts.LoggerLevel, w.opts.LoggerObserver(w.ctx, "Commit", getCallerName(), td, err)...) w.opts.Logger.Fields(w.opts.LoggerObserver(w.ctx, "Commit", getCallerName(), td, err)).Log(w.ctx, w.opts.LoggerLevel)
} }
*/
w.ctx = nil w.ctx = nil
return err return err
@ -45,18 +45,18 @@ func (w *wrapperTx) Rollback() error {
ts := time.Now() ts := time.Now()
err := w.tx.Rollback() err := w.tx.Rollback()
td := time.Since(ts) td := time.Since(ts)
_ = td
if w.span != nil { if w.span != nil {
if err != nil { if err != nil {
w.span.SetStatus(tracer.SpanStatusError, err.Error()) w.span.SetStatus(tracer.SpanStatusError, err.Error())
} }
w.span.Finish() w.span.Finish()
} }
/*
if w.opts.LoggerEnabled && w.opts.Logger.V(w.opts.LoggerLevel) { if w.opts.LoggerEnabled && w.opts.Logger.V(w.opts.LoggerLevel) {
w.opts.Logger.Log(w.ctx, w.opts.LoggerLevel, w.opts.LoggerObserver(w.ctx, "Rollback", getCallerName(), td, err)...) w.opts.Logger.Fields(w.opts.LoggerObserver(w.ctx, "Rollback", getCallerName(), td, err)).Log(w.ctx, w.opts.LoggerLevel)
} }
*/
w.ctx = nil w.ctx = nil
return err return err