Compare commits
2 Commits
v3
...
clustering
Author | SHA1 | Date | |
---|---|---|---|
600db7c67e | |||
fdebb257ae |
@ -54,8 +54,6 @@ func Connect(cfg *PostgresConfig) (*sqlx.DB, error) {
|
|||||||
sql.Register("micro-wrapper-sql", wrapper.NewWrapper(&sqlite.Driver{},
|
sql.Register("micro-wrapper-sql", wrapper.NewWrapper(&sqlite.Driver{},
|
||||||
wrapper.DatabaseHost("localhost"),
|
wrapper.DatabaseHost("localhost"),
|
||||||
wrapper.DatabaseName("mydb"),
|
wrapper.DatabaseName("mydb"),
|
||||||
wrapper.LoggerLevel(logger.DebugLevel),
|
|
||||||
wrapper.LoggerEnabled(true),
|
|
||||||
))
|
))
|
||||||
|
|
||||||
wdb, err := sql.Open("micro-wrapper-sql", dsn)
|
wdb, err := sql.Open("micro-wrapper-sql", dsn)
|
||||||
|
161
conn.go
161
conn.go
@ -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
|
||||||
@ -56,11 +54,7 @@ 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) {
|
|
||||||
w.opts.Logger.Log(ctx, w.opts.LoggerLevel, w.opts.LoggerObserver(ctx, "Close", getCallerName(), td, err)...)
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -82,21 +76,13 @@ 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) {
|
|
||||||
w.opts.Logger.Log(ctx, w.opts.LoggerLevel, w.opts.LoggerObserver(ctx, "Begin", getCallerName(), td, err)...)
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
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) {
|
|
||||||
w.opts.Logger.Log(ctx, w.opts.LoggerLevel, w.opts.LoggerObserver(ctx, "Begin", getCallerName(), td, err)...)
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
return &wrapperTx{tx: tx, opts: w.opts, ctx: ctx}, nil
|
return &wrapperTx{tx: tx, opts: w.opts, ctx: ctx}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -125,19 +111,11 @@ 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) {
|
|
||||||
w.opts.Logger.Log(ctx, w.opts.LoggerLevel, w.opts.LoggerObserver(ctx, "BeginTx", getCallerName(), td, err)...)
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
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) {
|
|
||||||
w.opts.Logger.Log(ctx, w.opts.LoggerLevel, w.opts.LoggerObserver(ctx, "BeginTx", getCallerName(), td, err)...)
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
return &wrapperTx{tx: tx, opts: w.opts, ctx: ctx, span: span}, nil
|
return &wrapperTx{tx: tx, opts: w.opts, ctx: ctx, span: span}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -159,21 +137,13 @@ 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) {
|
|
||||||
w.opts.Logger.Log(ctx, w.opts.LoggerLevel, w.opts.LoggerObserver(ctx, "Prepare", getCallerName(), td, err)...)
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
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) {
|
|
||||||
w.opts.Logger.Log(ctx, w.opts.LoggerLevel, w.opts.LoggerObserver(ctx, "Prepare", getCallerName(), td, err)...)
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
return wrapStmt(stmt, query, w.opts), nil
|
return wrapStmt(stmt, query, w.opts), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -208,60 +178,16 @@ 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) {
|
|
||||||
w.opts.Logger.Log(ctx, w.opts.LoggerLevel, w.opts.LoggerObserver(ctx, "PrepareContext", getCallerName(), td, err)...)
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
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) {
|
|
||||||
w.opts.Logger.Log(ctx, w.opts.LoggerLevel, w.opts.LoggerObserver(ctx, "PrepareContext", getCallerName(), td, err)...)
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
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)
|
|
||||||
/*
|
|
||||||
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)...)
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
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
|
||||||
@ -303,11 +229,7 @@ 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) {
|
|
||||||
w.opts.Logger.Log(ctx, w.opts.LoggerLevel, w.opts.LoggerObserver(ctx, "ExecContext", getCallerName(), td, err)...)
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -324,31 +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())
|
||||||
/*
|
|
||||||
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)...)
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
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()
|
||||||
@ -359,39 +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)
|
||||||
/*
|
|
||||||
if w.opts.LoggerEnabled && w.opts.Logger.V(w.opts.LoggerLevel) {
|
return nil
|
||||||
w.opts.Logger.Log(ctx, w.opts.LoggerLevel, w.opts.LoggerObserver(ctx, "Query", getCallerName(), td, err)...)
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
return rows, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// QueryContext implements Driver.QueryerContext QueryContext
|
// QueryContext implements Driver.QueryerContext QueryContext
|
||||||
@ -432,11 +333,7 @@ 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) {
|
|
||||||
w.opts.Logger.Log(ctx, w.opts.LoggerLevel, w.opts.LoggerObserver(ctx, "QueryContext", getCallerName(), td, err)...)
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
return rows, err
|
return rows, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
2
doc.go
2
doc.go
@ -1,2 +1,2 @@
|
|||||||
// package wrapper provides SQL driver wrapper with micro tracing, logging, metering capabilities
|
// package wrapper provides SQL driver wrapper with micro tracing and metering capabilities
|
||||||
package wrapper
|
package wrapper
|
||||||
|
10
driver.go
10
driver.go
@ -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,9 +36,9 @@ 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
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewWrapperConnector(c driver.Connector, opts ...Option) driver.Connector {
|
func NewWrapperConnector(c driver.Connector, opts ...Option) driver.Connector {
|
||||||
|
2
gen.go
2
gen.go
@ -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
8
go.mod
@ -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
|
||||||
)
|
)
|
||||||
|
4
go.sum
4
go.sum
@ -4,12 +4,8 @@ github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
|||||||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||||
go.unistack.org/micro-proto/v3 v3.4.1 h1:UTjLSRz2YZuaHk9iSlVqqsA50JQNAEK2ZFboGqtEa9Q=
|
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-proto/v3 v3.4.1/go.mod h1:okx/cnOhzuCX0ggl/vToatbCupi0O44diiiLLsZ93Zo=
|
||||||
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-wrapper-requestid/v3 v3.9.3 h1:KQS4nZhQ7S08xjMbpkXwQBhydfCC8YcHuORkoUDeAqI=
|
go.unistack.org/micro-wrapper-requestid/v3 v3.9.3 h1:KQS4nZhQ7S08xjMbpkXwQBhydfCC8YcHuORkoUDeAqI=
|
||||||
go.unistack.org/micro-wrapper-requestid/v3 v3.9.3/go.mod h1:cqgjdSSASTnOLjvwndP9bi0b8DbuoKbDII884R5puwU=
|
go.unistack.org/micro-wrapper-requestid/v3 v3.9.3/go.mod h1:cqgjdSSASTnOLjvwndP9bi0b8DbuoKbDII884R5puwU=
|
||||||
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=
|
|
||||||
go.unistack.org/micro/v3 v3.11.14 h1:3e9T30Ih9cvqZTCD8inG1qsBWRk4x5ZinWuTiDFM4CE=
|
go.unistack.org/micro/v3 v3.11.14 h1:3e9T30Ih9cvqZTCD8inG1qsBWRk4x5ZinWuTiDFM4CE=
|
||||||
go.unistack.org/micro/v3 v3.11.14/go.mod h1:k++F5Ej4LIy3XnOW/oj3P7B97wp2t9yLSlqtUzMpatM=
|
go.unistack.org/micro/v3 v3.11.14/go.mod h1:k++F5Ej4LIy3XnOW/oj3P7B97wp2t9yLSlqtUzMpatM=
|
||||||
golang.org/x/net v0.32.0 h1:ZqPmj8Kzc+Y6e0+skZsuACbx+wzMgo5MQsJh9Qd6aYI=
|
golang.org/x/net v0.32.0 h1:ZqPmj8Kzc+Y6e0+skZsuACbx+wzMgo5MQsJh9Qd6aYI=
|
||||||
|
52
options.go
52
options.go
@ -2,7 +2,6 @@ package wrapper
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"go.unistack.org/micro/v3/logger"
|
"go.unistack.org/micro/v3/logger"
|
||||||
@ -10,21 +9,8 @@ import (
|
|||||||
"go.unistack.org/micro/v3/tracer"
|
"go.unistack.org/micro/v3/tracer"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
// DefaultMeterStatsInterval holds default stats interval
|
||||||
// DefaultMeterStatsInterval holds default stats interval
|
var DefaultMeterStatsInterval = 5 * time.Second
|
||||||
DefaultMeterStatsInterval = 5 * time.Second
|
|
||||||
// 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)}
|
|
||||||
if err != nil {
|
|
||||||
labels = append(labels, "error", err.Error())
|
|
||||||
}
|
|
||||||
if query != labelUnknown {
|
|
||||||
labels = append(labels, "query", query)
|
|
||||||
}
|
|
||||||
return labels
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
var (
|
||||||
MaxOpenConnections = "micro_sql_max_open_conn"
|
MaxOpenConnections = "micro_sql_max_open_conn"
|
||||||
@ -53,7 +39,6 @@ var (
|
|||||||
|
|
||||||
// Options struct holds wrapper options
|
// Options struct holds wrapper options
|
||||||
type Options struct {
|
type Options struct {
|
||||||
Logger logger.Logger
|
|
||||||
Meter meter.Meter
|
Meter meter.Meter
|
||||||
Tracer tracer.Tracer
|
Tracer tracer.Tracer
|
||||||
DatabaseHost string
|
DatabaseHost string
|
||||||
@ -70,12 +55,9 @@ type Option func(*Options)
|
|||||||
// NewOptions create new Options struct from provided option slice
|
// NewOptions create new Options struct from provided option slice
|
||||||
func NewOptions(opts ...Option) Options {
|
func NewOptions(opts ...Option) Options {
|
||||||
options := Options{
|
options := Options{
|
||||||
Logger: logger.DefaultLogger,
|
|
||||||
Meter: meter.DefaultMeter,
|
Meter: meter.DefaultMeter,
|
||||||
Tracer: tracer.DefaultTracer,
|
Tracer: tracer.DefaultTracer,
|
||||||
MeterStatsInterval: DefaultMeterStatsInterval,
|
MeterStatsInterval: DefaultMeterStatsInterval,
|
||||||
LoggerLevel: logger.ErrorLevel,
|
|
||||||
LoggerObserver: DefaultLoggerObserver,
|
|
||||||
}
|
}
|
||||||
for _, o := range opts {
|
for _, o := range opts {
|
||||||
o(&options)
|
o(&options)
|
||||||
@ -88,8 +70,6 @@ func NewOptions(opts ...Option) Options {
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
options.Logger = options.Logger.Clone(logger.WithAddCallerSkipCount(1))
|
|
||||||
|
|
||||||
return options
|
return options
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -119,34 +99,6 @@ func Meter(m meter.Meter) Option {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Logger passes logger.Logger to wrapper
|
|
||||||
func Logger(l logger.Logger) Option {
|
|
||||||
return func(o *Options) {
|
|
||||||
o.Logger = l
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// LoggerEnabled enable sql logging
|
|
||||||
func LoggerEnabled(b bool) Option {
|
|
||||||
return func(o *Options) {
|
|
||||||
o.LoggerEnabled = b
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// LoggerLevel passes logger.Level option
|
|
||||||
func LoggerLevel(lvl logger.Level) Option {
|
|
||||||
return func(o *Options) {
|
|
||||||
o.LoggerLevel = lvl
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// LoggerObserver passes observer to fill logger fields
|
|
||||||
func LoggerObserver(obs func(context.Context, string, string, time.Duration, error) []interface{}) Option {
|
|
||||||
return func(o *Options) {
|
|
||||||
o.LoggerObserver = obs
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Tracer passes tracer.Tracer to wrapper
|
// Tracer passes tracer.Tracer to wrapper
|
||||||
func Tracer(t tracer.Tracer) Option {
|
func Tracer(t tracer.Tracer) Option {
|
||||||
return func(o *Options) {
|
return func(o *Options) {
|
||||||
|
2
wrap.go
2
wrap.go
@ -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 {
|
||||||
|
21398
wrap_gen.go
21398
wrap_gen.go
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user