From fdebb257ae2fe4327761b0883cce155e697d1663 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Thu, 2 Jan 2025 23:18:13 +0300 Subject: [PATCH] remove logger Signed-off-by: Vasiliy Tolstov --- README.md | 2 -- conn.go | 84 +++++++++--------------------------------------------- doc.go | 2 +- go.sum | 4 --- options.go | 52 ++------------------------------- 5 files changed, 17 insertions(+), 127 deletions(-) diff --git a/README.md b/README.md index 83c7039..7d948e5 100644 --- a/README.md +++ b/README.md @@ -54,8 +54,6 @@ func Connect(cfg *PostgresConfig) (*sqlx.DB, error) { sql.Register("micro-wrapper-sql", wrapper.NewWrapper(&sqlite.Driver{}, wrapper.DatabaseHost("localhost"), wrapper.DatabaseName("mydb"), - wrapper.LoggerLevel(logger.DebugLevel), - wrapper.LoggerEnabled(true), )) wdb, err := sql.Open("micro-wrapper-sql", dsn) diff --git a/conn.go b/conn.go index 354e191..afd0270 100644 --- a/conn.go +++ b/conn.go @@ -56,11 +56,7 @@ func (w *wrapperConn) Close() error { } 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, "Close", getCallerName(), td, err)...) - } - */ + return err } @@ -82,21 +78,13 @@ func (w *wrapperConn) Begin() (driver.Tx, error) { w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelFailure)...).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, "Begin", getCallerName(), td, err)...) - } - */ + return nil, err } 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, "Begin", getCallerName(), td, err)...) - } - */ + return &wrapperTx{tx: tx, opts: w.opts, ctx: ctx}, nil } @@ -125,19 +113,11 @@ func (w *wrapperConn) BeginTx(ctx context.Context, opts driver.TxOptions) (drive w.opts.Meter.Summary(meterRequestLatencyMicroseconds, labels...).Update(te) w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te) 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 } 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 } @@ -159,21 +139,13 @@ func (w *wrapperConn) Prepare(query string) (driver.Stmt, error) { w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelFailure)...).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, "Prepare", getCallerName(), td, err)...) - } - */ + return nil, err } 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, "Prepare", getCallerName(), td, err)...) - } - */ + return wrapStmt(stmt, query, w.opts), nil } @@ -208,21 +180,13 @@ func (w *wrapperConn) PrepareContext(ctx context.Context, query string) (driver. w.opts.Meter.Summary(meterRequestLatencyMicroseconds, labels...).Update(te) w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te) 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 } 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, "PrepareContext", getCallerName(), td, err)...) - } - */ + return wrapStmt(stmt, query, w.opts), nil } @@ -254,11 +218,7 @@ func (w *wrapperConn) Exec(query string, args []driver.Value) (driver.Result, er } 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 } @@ -303,11 +263,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.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, "ExecContext", getCallerName(), td, err)...) - } - */ + return res, err } @@ -344,11 +300,7 @@ func (w *wrapperConn) Ping(ctx context.Context) error { if err != nil { w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelFailure)...).Inc() // 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 } else { w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelSuccess)...).Inc() @@ -386,11 +338,7 @@ func (w *wrapperConn) Query(query string, args []driver.Value) (driver.Rows, err } 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, "Query", getCallerName(), td, err)...) - } - */ + return rows, err } @@ -432,11 +380,7 @@ func (w *wrapperConn) QueryContext(ctx context.Context, query string, args []dri } 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, "QueryContext", getCallerName(), td, err)...) - } - */ + return rows, err } diff --git a/doc.go b/doc.go index fb88238..b538dd6 100644 --- a/doc.go +++ b/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 diff --git a/go.sum b/go.sum index 2328b10..de6c3ea 100644 --- a/go.sum +++ b/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= 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.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/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/go.mod h1:k++F5Ej4LIy3XnOW/oj3P7B97wp2t9yLSlqtUzMpatM= golang.org/x/net v0.32.0 h1:ZqPmj8Kzc+Y6e0+skZsuACbx+wzMgo5MQsJh9Qd6aYI= diff --git a/options.go b/options.go index 84494b1..0c84dfc 100644 --- a/options.go +++ b/options.go @@ -2,7 +2,6 @@ package wrapper import ( "context" - "fmt" "time" "go.unistack.org/micro/v3/logger" @@ -10,21 +9,8 @@ import ( "go.unistack.org/micro/v3/tracer" ) -var ( - // DefaultMeterStatsInterval holds default stats interval - 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 - } -) +// DefaultMeterStatsInterval holds default stats interval +var DefaultMeterStatsInterval = 5 * time.Second var ( MaxOpenConnections = "micro_sql_max_open_conn" @@ -53,7 +39,6 @@ var ( // Options struct holds wrapper options type Options struct { - Logger logger.Logger Meter meter.Meter Tracer tracer.Tracer DatabaseHost string @@ -70,12 +55,9 @@ type Option func(*Options) // NewOptions create new Options struct from provided option slice func NewOptions(opts ...Option) Options { options := Options{ - Logger: logger.DefaultLogger, Meter: meter.DefaultMeter, Tracer: tracer.DefaultTracer, MeterStatsInterval: DefaultMeterStatsInterval, - LoggerLevel: logger.ErrorLevel, - LoggerObserver: DefaultLoggerObserver, } for _, o := range opts { o(&options) @@ -88,8 +70,6 @@ func NewOptions(opts ...Option) Options { ), ) - options.Logger = options.Logger.Clone(logger.WithAddCallerSkipCount(1)) - 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 func Tracer(t tracer.Tracer) Option { return func(o *Options) {