From 564efef60b3be7392eed94a463a428bcbf4b5061 Mon Sep 17 00:00:00 2001 From: devstigneev Date: Thu, 11 Apr 2024 11:38:09 +0300 Subject: [PATCH] moved compare error to LoggerObserver --- conn.go | 31 ++++++++++++------------------- options.go | 4 +++- 2 files changed, 15 insertions(+), 20 deletions(-) diff --git a/conn.go b/conn.go index f114295..f599de1 100644 --- a/conn.go +++ b/conn.go @@ -3,7 +3,6 @@ package wrapper import ( "context" "database/sql/driver" - "errors" "fmt" "time" @@ -149,8 +148,7 @@ func (w *wrapperConn) Prepare(query string) (driver.Stmt, error) { stmt, err := w.conn.Prepare(query) td := time.Since(ts) te := td.Seconds() - compErr := errors.Is(err, driver.ErrSkip) - if err != nil && !compErr { + if err != nil { 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) @@ -163,7 +161,7 @@ func (w *wrapperConn) Prepare(query string) (driver.Stmt, 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) && !compErr { + if w.opts.LoggerEnabled && w.opts.Logger.V(w.opts.LoggerLevel) { w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "Prepare", getCallerName(), td, err)...).Log(ctx, w.opts.LoggerLevel) } @@ -196,8 +194,7 @@ func (w *wrapperConn) PrepareContext(ctx context.Context, query string) (driver. stmt, err := conn.PrepareContext(nctx, query) td := time.Since(ts) te := td.Seconds() - compErr := errors.Is(err, driver.ErrSkip) - if err != nil && !compErr { + if err != nil { 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) @@ -210,7 +207,7 @@ func (w *wrapperConn) PrepareContext(ctx context.Context, query string) (driver. 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) && !compErr { + if w.opts.LoggerEnabled && w.opts.Logger.V(w.opts.LoggerLevel) { w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "PrepareContext", getCallerName(), td, err)...).Log(ctx, w.opts.LoggerLevel) } @@ -237,15 +234,14 @@ func (w *wrapperConn) Exec(query string, args []driver.Value) (driver.Result, er res, err := conn.Exec(query, args) td := time.Since(ts) te := td.Seconds() - compErr := errors.Is(err, driver.ErrSkip) - if err != nil && !compErr { + 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) && !compErr { + if w.opts.LoggerEnabled && w.opts.Logger.V(w.opts.LoggerLevel) { w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "Exec", getCallerName(), td, err)...).Log(ctx, w.opts.LoggerLevel) } return res, err @@ -283,8 +279,7 @@ func (w *wrapperConn) ExecContext(ctx context.Context, query string, args []driv res, err := conn.ExecContext(nctx, query, args) td := time.Since(ts) te := td.Seconds() - compErr := errors.Is(err, driver.ErrSkip) - if err != nil && !compErr { + if err != nil { w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelFailure)...).Inc() span.SetStatus(tracer.SpanStatusError, err.Error()) } else { @@ -293,7 +288,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) && !compErr { + if w.opts.LoggerEnabled && w.opts.Logger.V(w.opts.LoggerLevel) { w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "ExecContext", getCallerName(), td, err)...).Log(ctx, w.opts.LoggerLevel) } @@ -361,15 +356,14 @@ func (w *wrapperConn) Query(query string, args []driver.Value) (driver.Rows, err rows, err := conn.Query(query, args) td := time.Since(ts) te := td.Seconds() - compErr := errors.Is(err, driver.ErrSkip) - if err != nil && !compErr { + 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) && !compErr { + if w.opts.LoggerEnabled && w.opts.Logger.V(w.opts.LoggerLevel) { w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "Query", getCallerName(), td, err)...).Log(ctx, w.opts.LoggerLevel) } return rows, err @@ -405,8 +399,7 @@ func (w *wrapperConn) QueryContext(ctx context.Context, query string, args []dri rows, err := conn.QueryContext(nctx, query, args) td := time.Since(ts) te := td.Seconds() - compErr := errors.Is(err, driver.ErrSkip) - if err != nil && !compErr { + if err != nil { w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelFailure)...).Inc() span.SetStatus(tracer.SpanStatusError, err.Error()) } else { @@ -414,7 +407,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) && !compErr { + if w.opts.LoggerEnabled && w.opts.Logger.V(w.opts.LoggerLevel) { w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "QueryContext", getCallerName(), td, err)...).Log(ctx, w.opts.LoggerLevel) } diff --git a/options.go b/options.go index f58ac28..47969cb 100644 --- a/options.go +++ b/options.go @@ -2,6 +2,8 @@ package wrapper import ( "context" + "database/sql" + "errors" "fmt" "time" @@ -18,7 +20,7 @@ var ( // 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 { + if err != nil && !errors.Is(err, sql.ErrNoRows) { labels = append(labels, "error", err.Error()) } if query != labelUnknown {