WIP: remove logger #85

Draft
vtolstov wants to merge 2 commits from clustering into v3
6 changed files with 2791 additions and 18710 deletions
Showing only changes of commit 600db7c67e - Show all commits

81
conn.go
View File

@ -16,9 +16,7 @@ var (
_ driver.ConnPrepareContext = (*wrapperConn)(nil) _ driver.ConnPrepareContext = (*wrapperConn)(nil)
_ driver.Pinger = (*wrapperConn)(nil) _ driver.Pinger = (*wrapperConn)(nil)
_ driver.Validator = (*wrapperConn)(nil) _ driver.Validator = (*wrapperConn)(nil)
_ driver.Queryer = (*wrapperConn)(nil) // nolint:staticcheck
_ driver.QueryerContext = (*wrapperConn)(nil) _ driver.QueryerContext = (*wrapperConn)(nil)
_ driver.Execer = (*wrapperConn)(nil) // nolint:staticcheck
_ driver.ExecerContext = (*wrapperConn)(nil) _ driver.ExecerContext = (*wrapperConn)(nil)
// _ driver.Connector // _ driver.Connector
// _ driver.Driver // _ driver.Driver
@ -32,7 +30,7 @@ type wrapperConn struct {
conn driver.Conn conn driver.Conn
opts Options opts Options
ctx context.Context ctx context.Context
span tracer.Span // span tracer.Span
} }
// Close implements driver.Conn Close // Close implements driver.Conn Close
@ -190,38 +188,6 @@ func (w *wrapperConn) PrepareContext(ctx context.Context, query string) (driver.
return wrapStmt(stmt, query, w.opts), nil return wrapStmt(stmt, query, w.opts), nil
} }
// Exec implements driver.Execer Exec
func (w *wrapperConn) Exec(query string, args []driver.Value) (driver.Result, error) {
var ctx context.Context
if w.ctx != nil {
ctx = w.ctx
} else {
ctx = context.Background()
}
_ = ctx
labels := []string{labelMethod, "Exec", labelQuery, getCallerName()}
// nolint:staticcheck
conn, ok := w.conn.(driver.Execer)
if !ok {
return nil, driver.ErrSkip
}
ts := time.Now()
res, err := conn.Exec(query, args)
td := time.Since(ts)
te := td.Seconds()
if err != nil {
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelFailure)...).Inc()
} else {
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelSuccess)...).Inc()
}
w.opts.Meter.Summary(meterRequestLatencyMicroseconds, labels...).Update(te)
w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te)
return res, err
}
// Exec implements driver.StmtExecContext ExecContext // Exec implements driver.StmtExecContext ExecContext
func (w *wrapperConn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) { func (w *wrapperConn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) {
var nctx context.Context var nctx context.Context
@ -280,27 +246,14 @@ func (w *wrapperConn) Ping(ctx context.Context) error {
return pc.Close() return pc.Close()
} }
var nctx context.Context
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"} labels := []string{labelMethod, "Ping"}
ts := time.Now() ts := time.Now()
err := conn.Ping(nctx) err := conn.Ping(ctx)
td := time.Since(ts) td := time.Since(ts)
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())
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()
@ -311,35 +264,35 @@ func (w *wrapperConn) Ping(ctx context.Context) error {
return nil return nil
} }
// Query implements driver.Queryer Query // Ping implements driver.Pinger PingContext
func (w *wrapperConn) Query(query string, args []driver.Value) (driver.Rows, error) { func (w *wrapperConn) PingContext(ctx context.Context) error {
var ctx context.Context conn, ok := w.conn.(driver.Pinger)
if w.ctx != nil {
ctx = w.ctx
} else {
ctx = context.Background()
}
_ = ctx
// nolint:staticcheck
conn, ok := w.conn.(driver.Queryer)
if !ok { if !ok {
return nil, driver.ErrSkip // fallback path to check db alive
pc, err := w.d.Open(w.dname)
if err != nil {
return err
}
return pc.Close()
} }
labels := []string{labelMethod, "Query", labelQuery, getCallerName()} labels := []string{labelMethod, "Ping"}
ts := time.Now() ts := time.Now()
rows, err := conn.Query(query, args) err := conn.Ping(ctx)
td := time.Since(ts) td := time.Since(ts)
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())
return err
} else { } else {
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)
return rows, err return nil
} }
// QueryContext implements Driver.QueryerContext QueryContext // QueryContext implements Driver.QueryerContext QueryContext

View File

@ -11,16 +11,16 @@ var (
// _ driver.Connector = (*wrapperDriver)(nil) // _ driver.Connector = (*wrapperDriver)(nil)
) )
/*
type conn interface { type conn interface {
driver.Pinger driver.Pinger
driver.Execer
driver.ExecerContext driver.ExecerContext
driver.Queryer
driver.QueryerContext driver.QueryerContext
driver.Conn driver.Conn
driver.ConnPrepareContext driver.ConnPrepareContext
driver.ConnBeginTx driver.ConnBeginTx
} }
*/
// wrapperDriver defines a wrapper for driver.Driver // wrapperDriver defines a wrapper for driver.Driver
type wrapperDriver struct { type wrapperDriver struct {
@ -36,7 +36,7 @@ func NewWrapper(d driver.Driver, opts ...Option) driver.Driver {
type wrappedConnector struct { type wrappedConnector struct {
connector driver.Connector connector driver.Connector
name string // name string
opts Options opts Options
ctx context.Context ctx context.Context
} }

2
gen.go
View File

@ -14,11 +14,9 @@ import (
var connIfaces = []string{ var connIfaces = []string{
"driver.ConnBeginTx", "driver.ConnBeginTx",
"driver.ConnPrepareContext", "driver.ConnPrepareContext",
"driver.Execer",
"driver.ExecerContext", "driver.ExecerContext",
"driver.NamedValueChecker", "driver.NamedValueChecker",
"driver.Pinger", "driver.Pinger",
"driver.Queryer",
"driver.QueryerContext", "driver.QueryerContext",
"driver.SessionResetter", "driver.SessionResetter",
"driver.Validator", "driver.Validator",

8
go.mod
View File

@ -11,9 +11,9 @@ require (
require ( require (
go.unistack.org/micro-proto/v3 v3.4.1 // indirect go.unistack.org/micro-proto/v3 v3.4.1 // indirect
golang.org/x/net v0.32.0 // indirect golang.org/x/net v0.33.0 // indirect
golang.org/x/sys v0.28.0 // indirect golang.org/x/sys v0.28.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20241209162323-e6fa225c2576 // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20241223144023-3abc09e42ca8 // indirect
google.golang.org/grpc v1.68.1 // indirect google.golang.org/grpc v1.69.2 // indirect
google.golang.org/protobuf v1.35.2 // indirect google.golang.org/protobuf v1.36.1 // indirect
) )

View File

@ -4,12 +4,14 @@ import (
"database/sql/driver" "database/sql/driver"
) )
/*
func wrapDriver(d driver.Driver, opts Options) driver.Driver { func wrapDriver(d driver.Driver, opts Options) driver.Driver {
if _, ok := d.(driver.DriverContext); ok { if _, ok := d.(driver.DriverContext); ok {
return &wrapperDriver{driver: d, opts: opts} return &wrapperDriver{driver: d, opts: opts}
} }
return struct{ driver.Driver }{&wrapperDriver{driver: d, opts: opts}} return struct{ driver.Driver }{&wrapperDriver{driver: d, opts: opts}}
} }
*/
// WrapConn allows an existing driver.Conn to be wrapped. // WrapConn allows an existing driver.Conn to be wrapped.
func WrapConn(c driver.Conn, opts ...Option) driver.Conn { func WrapConn(c driver.Conn, opts ...Option) driver.Conn {

21274
wrap_gen.go

File diff suppressed because it is too large Load Diff