From 7ac873c62f4eec152fd6d9ee4f71c74c82252113 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Fri, 20 Sep 2024 18:17:16 +0300 Subject: [PATCH] update deps, minimise memory allocations Signed-off-by: Vasiliy Tolstov --- conn.go | 35 +++++++++++++++++++---------------- go.mod | 17 ++++++++++------- go.sum | 14 ++++++++++++++ options.go | 36 ++++++++++++------------------------ stmt.go | 8 ++++---- 5 files changed, 59 insertions(+), 51 deletions(-) diff --git a/conn.go b/conn.go index caf976a..a0c9fb5 100644 --- a/conn.go +++ b/conn.go @@ -100,7 +100,7 @@ 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) { 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.statement", name) 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) 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 { - 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.statement", name) @@ -254,9 +254,9 @@ func (w *wrapperConn) ExecContext(ctx context.Context, query string, args []driv name := getQueryName(ctx) 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 { - 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.statement", name) @@ -309,14 +309,17 @@ func (w *wrapperConn) Ping(ctx context.Context) error { } var nctx context.Context - var span tracer.Span - if w.ctx != nil { - nctx, span = w.opts.Tracer.Start(w.ctx, "sdk.database Ping", tracer.WithSpanKind(tracer.SpanKindClient)) - } else { - nctx, span = w.opts.Tracer.Start(ctx, "sdk.database Ping", tracer.WithSpanKind(tracer.SpanKindClient)) - } - span.AddLabels("db.method", "Ping") - defer span.Finish() + nctx = ctx + /* + var span tracer.Span + if w.ctx != nil { + nctx, span = w.opts.Tracer.Start(w.ctx, "sdk.database", tracer.WithSpanKind(tracer.SpanKindClient)) + } else { + nctx, span = w.opts.Tracer.Start(ctx, "sdk.database", tracer.WithSpanKind(tracer.SpanKindClient)) + } + span.AddLabels("db.method", "Ping") + defer span.Finish() + */ labels := []string{labelMethod, "Ping"} ts := time.Now() err := conn.Ping(nctx) @@ -324,7 +327,7 @@ func (w *wrapperConn) Ping(ctx context.Context) error { te := td.Seconds() if err != nil { 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) { 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) 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 { - 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.statement", name) diff --git a/go.mod b/go.mod index c2bc0f9..0226f3f 100644 --- a/go.mod +++ b/go.mod @@ -1,15 +1,18 @@ module go.unistack.org/micro-wrapper-sql/v3 -go 1.20 +go 1.21 + +toolchain go1.23.1 require ( - go.unistack.org/micro-wrapper-requestid/v3 v3.8.10 - go.unistack.org/micro/v3 v3.10.57 + go.unistack.org/micro-wrapper-requestid/v3 v3.9.2 + go.unistack.org/micro/v3 v3.10.91 ) require ( - golang.org/x/sys v0.19.0 // indirect - google.golang.org/genproto/googleapis/rpc v0.0.0-20240401170217-c3f982113cda // indirect - google.golang.org/grpc v1.63.2 // indirect - google.golang.org/protobuf v1.33.0 // indirect + go.unistack.org/micro-proto/v3 v3.4.1 // indirect + golang.org/x/sys v0.25.0 // indirect + google.golang.org/genproto/googleapis/rpc v0.0.0-20240903143218-8af14fe29dc1 // indirect + google.golang.org/grpc v1.67.0 // indirect + google.golang.org/protobuf v1.34.2 // indirect ) diff --git a/go.sum b/go.sum index 14b4335..8c30117 100644 --- a/go.sum +++ b/go.sum @@ -1,15 +1,29 @@ 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/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= 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/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/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/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= diff --git a/options.go b/options.go index 7e35d13..b7cad73 100644 --- a/options.go +++ b/options.go @@ -13,8 +13,6 @@ import ( var ( // DefaultMeterStatsInterval holds default stats interval DefaultMeterStatsInterval = 5 * time.Second - // DefaultMeterMetricPrefix holds default metric prefix - 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{}{"db.method", method, "took", fmt.Sprintf("%v", td)} @@ -29,19 +27,19 @@ var ( ) var ( - MaxOpenConnections = "max_open_conn" - OpenConnections = "open_conn" - InuseConnections = "inuse_conn" - IdleConnections = "idle_conn" - WaitConnections = "waited_conn" - BlockedSeconds = "blocked_seconds" - MaxIdleClosed = "max_idle_closed" - MaxIdletimeClosed = "closed_max_idle" - MaxLifetimeClosed = "closed_max_lifetime" + MaxOpenConnections = "micro_sql_max_open_conn" + OpenConnections = "micro_sql_open_conn" + InuseConnections = "micro_sql_inuse_conn" + IdleConnections = "micro_sql_idle_conn" + WaitConnections = "micro_sql_waited_conn" + BlockedSeconds = "micro_sql_blocked_seconds" + MaxIdleClosed = "micro_sql_max_idle_closed" + MaxIdletimeClosed = "micro_sql_closed_max_idle" + MaxLifetimeClosed = "micro_sql_closed_max_lifetime" - meterRequestTotal = "request_total" - meterRequestLatencyMicroseconds = "latency_microseconds" - meterRequestDurationSeconds = "request_duration_seconds" + meterRequestTotal = "micro_sql_request_total" + meterRequestLatencyMicroseconds = "micro_sql_latency_microseconds" + meterRequestDurationSeconds = "micro_sql_request_duration_seconds" labelUnknown = "unknown" labelQuery = "db_statement" @@ -60,7 +58,6 @@ type Options struct { Tracer tracer.Tracer DatabaseHost string DatabaseName string - MeterMetricPrefix string MeterStatsInterval time.Duration LoggerLevel logger.Level LoggerEnabled bool @@ -77,7 +74,6 @@ func NewOptions(opts ...Option) Options { Meter: meter.DefaultMeter, Tracer: tracer.DefaultTracer, MeterStatsInterval: DefaultMeterStatsInterval, - MeterMetricPrefix: DefaultMeterMetricPrefix, LoggerLevel: logger.ErrorLevel, LoggerObserver: DefaultLoggerObserver, } @@ -86,7 +82,6 @@ func NewOptions(opts ...Option) Options { } options.Meter = options.Meter.Clone( - meter.MetricPrefix(options.MeterMetricPrefix), meter.Labels( labelHost, options.DatabaseHost, 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 { return func(o *Options) { o.DatabaseHost = host diff --git a/stmt.go b/stmt.go index 60634d2..163dcaf 100644 --- a/stmt.go +++ b/stmt.go @@ -138,9 +138,9 @@ func (w *wrapperStmt) ExecContext(ctx context.Context, args []driver.NamedValue) name := getQueryName(ctx) 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 { - 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.statement", name) @@ -211,9 +211,9 @@ func (w *wrapperStmt) QueryContext(ctx context.Context, args []driver.NamedValue name := getQueryName(ctx) 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 { - 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.statement", name)