update deps, minimise memory allocations
Some checks failed
build / test (push) Failing after 8s
build / lint (push) Failing after 7s
codeql / analyze (go) (push) Failing after 10s

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2024-09-20 18:17:16 +03:00
parent 44256cd62f
commit 7ac873c62f
5 changed files with 59 additions and 51 deletions

23
conn.go
View File

@ -100,7 +100,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) {
name := getQueryName(ctx) name := getQueryName(ctx)
nctx, span := w.opts.Tracer.Start(ctx, "sdk.database "+name, 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")
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 {
@ -175,9 +175,9 @@ func (w *wrapperConn) PrepareContext(ctx context.Context, query string) (driver.
name := getQueryName(ctx) name := getQueryName(ctx)
if w.ctx != nil { if w.ctx != nil {
nctx, span = w.opts.Tracer.Start(w.ctx, "sdk.database "+name, 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 "+name, 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")
span.AddLabels("db.statement", name) span.AddLabels("db.statement", name)
@ -254,9 +254,9 @@ func (w *wrapperConn) ExecContext(ctx context.Context, query string, args []driv
name := getQueryName(ctx) name := getQueryName(ctx)
if w.ctx != nil { if w.ctx != nil {
nctx, span = w.opts.Tracer.Start(w.ctx, "sdk.database "+name, 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 "+name, 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")
span.AddLabels("db.statement", name) span.AddLabels("db.statement", name)
@ -309,14 +309,17 @@ 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 Ping", 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 Ping", tracer.WithSpanKind(tracer.SpanKindClient)) nctx, span = w.opts.Tracer.Start(ctx, "sdk.database", tracer.WithSpanKind(tracer.SpanKindClient))
} }
span.AddLabels("db.method", "Ping") 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)
@ -324,7 +327,7 @@ 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.Log(ctx, w.opts.LoggerLevel, w.opts.LoggerObserver(ctx, "Ping", getCallerName(), td, err)...)
} }
@ -377,9 +380,9 @@ func (w *wrapperConn) QueryContext(ctx context.Context, query string, args []dri
name := getQueryName(ctx) name := getQueryName(ctx)
if w.ctx != nil { if w.ctx != nil {
nctx, span = w.opts.Tracer.Start(w.ctx, "sdk.database "+name, 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 "+name, 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")
span.AddLabels("db.statement", name) span.AddLabels("db.statement", name)

17
go.mod
View File

@ -1,15 +1,18 @@
module go.unistack.org/micro-wrapper-sql/v3 module go.unistack.org/micro-wrapper-sql/v3
go 1.20 go 1.21
toolchain go1.23.1
require ( require (
go.unistack.org/micro-wrapper-requestid/v3 v3.8.10 go.unistack.org/micro-wrapper-requestid/v3 v3.9.2
go.unistack.org/micro/v3 v3.10.57 go.unistack.org/micro/v3 v3.10.91
) )
require ( require (
golang.org/x/sys v0.19.0 // indirect go.unistack.org/micro-proto/v3 v3.4.1 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240401170217-c3f982113cda // indirect golang.org/x/sys v0.25.0 // indirect
google.golang.org/grpc v1.63.2 // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20240903143218-8af14fe29dc1 // indirect
google.golang.org/protobuf v1.33.0 // indirect google.golang.org/grpc v1.67.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
) )

14
go.sum
View File

@ -1,15 +1,29 @@
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
go.unistack.org/micro-proto/v3 v3.4.1 h1:UTjLSRz2YZuaHk9iSlVqqsA50JQNAEK2ZFboGqtEa9Q=
go.unistack.org/micro-proto/v3 v3.4.1/go.mod h1:okx/cnOhzuCX0ggl/vToatbCupi0O44diiiLLsZ93Zo=
go.unistack.org/micro-wrapper-requestid/v3 v3.8.10 h1:9QZy6w1HU2RCppZ1t8wJsDg1pk9RbvzUFYurq3Ai29Y= go.unistack.org/micro-wrapper-requestid/v3 v3.8.10 h1:9QZy6w1HU2RCppZ1t8wJsDg1pk9RbvzUFYurq3Ai29Y=
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.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 h1:VxG7Cs7kBOgxgQlP+K8TvTxIgh2pmqAwmAaKdTAQNtQ=
go.unistack.org/micro/v3 v3.10.57/go.mod h1:erMgt3Bl7vQQ0e9UpQyR5NlLiZ9pKeEJ9+1tfYFaqUg= 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=
golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4= 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 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o=
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= 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/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= 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 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-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/grpc v1.63.2 h1:MUeiw1B2maTVZthpU5xvASfTh3LDbxHd6IJ6QQVU+xM= 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.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/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI= 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.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=

View File

@ -13,8 +13,6 @@ import (
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)}
@ -29,19 +27,19 @@ var (
) )
var ( var (
MaxOpenConnections = "max_open_conn" MaxOpenConnections = "micro_sql_max_open_conn"
OpenConnections = "open_conn" OpenConnections = "micro_sql_open_conn"
InuseConnections = "inuse_conn" InuseConnections = "micro_sql_inuse_conn"
IdleConnections = "idle_conn" IdleConnections = "micro_sql_idle_conn"
WaitConnections = "waited_conn" WaitConnections = "micro_sql_waited_conn"
BlockedSeconds = "blocked_seconds" BlockedSeconds = "micro_sql_blocked_seconds"
MaxIdleClosed = "max_idle_closed" MaxIdleClosed = "micro_sql_max_idle_closed"
MaxIdletimeClosed = "closed_max_idle" MaxIdletimeClosed = "micro_sql_closed_max_idle"
MaxLifetimeClosed = "closed_max_lifetime" MaxLifetimeClosed = "micro_sql_closed_max_lifetime"
meterRequestTotal = "request_total" meterRequestTotal = "micro_sql_request_total"
meterRequestLatencyMicroseconds = "latency_microseconds" meterRequestLatencyMicroseconds = "micro_sql_latency_microseconds"
meterRequestDurationSeconds = "request_duration_seconds" meterRequestDurationSeconds = "micro_sql_request_duration_seconds"
labelUnknown = "unknown" labelUnknown = "unknown"
labelQuery = "db_statement" labelQuery = "db_statement"
@ -60,7 +58,6 @@ 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
@ -77,7 +74,6 @@ 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,
} }
@ -86,7 +82,6 @@ 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,
@ -105,13 +100,6 @@ 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

@ -138,9 +138,9 @@ func (w *wrapperStmt) ExecContext(ctx context.Context, args []driver.NamedValue)
name := getQueryName(ctx) name := getQueryName(ctx)
if w.ctx != nil { if w.ctx != nil {
nctx, span = w.opts.Tracer.Start(w.ctx, "sdk.database "+name, 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 "+name, 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")
span.AddLabels("db.statement", name) span.AddLabels("db.statement", name)
@ -211,9 +211,9 @@ func (w *wrapperStmt) QueryContext(ctx context.Context, args []driver.NamedValue
name := getQueryName(ctx) name := getQueryName(ctx)
if w.ctx != nil { if w.ctx != nil {
nctx, span = w.opts.Tracer.Start(w.ctx, "sdk.database "+name, 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 "+name, 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")
span.AddLabels("db.statement", name) span.AddLabels("db.statement", name)