5 Commits

Author SHA1 Message Date
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
8 changed files with 49 additions and 49 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()
func getCallerName() string {
pc, _, _, ok := runtime.Caller(2)
pc, _, _, ok := runtime.Caller(3)
details := runtime.FuncForPC(pc)
var callerName string
if ok && details != nil {

40
conn.go
View File

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

7
go.mod
View File

@@ -1,5 +1,8 @@
module go.unistack.org/micro-wrapper-sql/v3
module go.unistack.org/micro-wrapper-sql/v4
go 1.20
require go.unistack.org/micro/v3 v3.10.25
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/v3 v3.10.25 h1:A0epdZHOqjnXx103wwFhPKgmvVVbScvfbmn3HmHz1wE=
go.unistack.org/micro/v3 v3.10.25/go.mod h1:ALkeXpqChYDjx8KPi7tz9mmIyOnob6nlNswsg8BnZjQ=
go.unistack.org/micro-wrapper-requestid/v4 v4.0.3 h1:LhloK8yLLsov4XiKw9i20wdBUfvWCG1TaBNgNVtNa8Q=
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

@@ -5,9 +5,9 @@ import (
"fmt"
"time"
"go.unistack.org/micro/v3/logger"
"go.unistack.org/micro/v3/meter"
"go.unistack.org/micro/v3/tracer"
"go.unistack.org/micro/v4/logger"
"go.unistack.org/micro/v4/meter"
"go.unistack.org/micro/v4/tracer"
)
var (
@@ -17,7 +17,7 @@ var (
DefaultMeterMetricPrefix = "micro_sql_"
// DefaultLoggerObserver used to prepare labels for logger
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 {
labels = append(labels, "error", err.Error())
}
@@ -44,8 +44,8 @@ var (
meterRequestDurationSeconds = "request_duration_seconds"
labelUnknown = "unknown"
labelQuery = "query"
labelMethod = "method"
labelQuery = "db_statement"
labelMethod = "db_method"
labelStatus = "status"
labelSuccess = "success"
labelFailure = "failure"
@@ -180,5 +180,5 @@ func getQueryName(ctx context.Context) string {
if v, ok := ctx.Value(queryNameKey{}).(string); ok && v != labelUnknown {
return v
}
return ""
return getCallerName()
}

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 (
"context"

25
stmt.go
View File

@@ -6,7 +6,8 @@ import (
"fmt"
"time"
"go.unistack.org/micro/v3/tracer"
requestid "go.unistack.org/micro-wrapper-requestid/v4"
"go.unistack.org/micro/v4/tracer"
)
var (
@@ -140,17 +141,16 @@ func (w *wrapperStmt) ExecContext(ctx context.Context, args []driver.NamedValue)
} else {
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)
if name != "" {
span.AddLabels("db.query", name)
} else {
name = getCallerName()
}
span.AddLabels("db.statement", name)
defer span.Finish()
if len(args) > 0 {
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}
if conn, ok := w.stmt.(driver.StmtExecContext); ok {
@@ -214,17 +214,16 @@ func (w *wrapperStmt) QueryContext(ctx context.Context, args []driver.NamedValue
} else {
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)
if name != "" {
span.AddLabels("db.query", name)
} else {
name = getCallerName()
}
span.AddLabels("db.statement", name)
defer span.Finish()
if len(args) > 0 {
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}
if conn, ok := w.stmt.(driver.StmtQueryContext); ok {
ts := time.Now()

2
tx.go
View File

@@ -5,7 +5,7 @@ import (
"database/sql/driver"
"time"
"go.unistack.org/micro/v3/tracer"
"go.unistack.org/micro/v4/tracer"
)
var _ driver.Tx = (*wrapperTx)(nil)