From cb9d32bf1c7edb1073fda84bfb0a61b170d02a68 Mon Sep 17 00:00:00 2001 From: devstigneev Date: Sun, 7 Apr 2024 22:12:39 +0300 Subject: [PATCH 1/2] add err skip --- .gitignore | 9 +++++++++ conn.go | 31 +++++++++++++++++++------------ 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/.gitignore b/.gitignore index 66fd13c..9e16696 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ *.dll *.so *.dylib +bin # Test binary, built with `go test -c` *.test @@ -13,3 +14,11 @@ # Dependency directories (remove the comment below to include it) # vendor/ + +# Go workspace file +go.work + +# General +.DS_Store +.idea +.vscode \ No newline at end of file diff --git a/conn.go b/conn.go index f599de1..f114295 100644 --- a/conn.go +++ b/conn.go @@ -3,6 +3,7 @@ package wrapper import ( "context" "database/sql/driver" + "errors" "fmt" "time" @@ -148,7 +149,8 @@ func (w *wrapperConn) Prepare(query string) (driver.Stmt, error) { stmt, err := w.conn.Prepare(query) td := time.Since(ts) te := td.Seconds() - if err != nil { + compErr := errors.Is(err, driver.ErrSkip) + if err != nil && !compErr { 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) @@ -161,7 +163,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) { + if w.opts.LoggerEnabled && w.opts.Logger.V(w.opts.LoggerLevel) && !compErr { w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "Prepare", getCallerName(), td, err)...).Log(ctx, w.opts.LoggerLevel) } @@ -194,7 +196,8 @@ func (w *wrapperConn) PrepareContext(ctx context.Context, query string) (driver. stmt, err := conn.PrepareContext(nctx, query) td := time.Since(ts) te := td.Seconds() - if err != nil { + compErr := errors.Is(err, driver.ErrSkip) + if err != nil && !compErr { 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) @@ -207,7 +210,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) { + if w.opts.LoggerEnabled && w.opts.Logger.V(w.opts.LoggerLevel) && !compErr { w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "PrepareContext", getCallerName(), td, err)...).Log(ctx, w.opts.LoggerLevel) } @@ -234,14 +237,15 @@ 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() - if err != nil { + compErr := errors.Is(err, driver.ErrSkip) + if err != nil && !compErr { 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) { + if w.opts.LoggerEnabled && w.opts.Logger.V(w.opts.LoggerLevel) && !compErr { w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "Exec", getCallerName(), td, err)...).Log(ctx, w.opts.LoggerLevel) } return res, err @@ -279,7 +283,8 @@ 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() - if err != nil { + compErr := errors.Is(err, driver.ErrSkip) + if err != nil && !compErr { w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelFailure)...).Inc() span.SetStatus(tracer.SpanStatusError, err.Error()) } else { @@ -288,7 +293,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) { + if w.opts.LoggerEnabled && w.opts.Logger.V(w.opts.LoggerLevel) && !compErr { w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "ExecContext", getCallerName(), td, err)...).Log(ctx, w.opts.LoggerLevel) } @@ -356,14 +361,15 @@ 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() - if err != nil { + compErr := errors.Is(err, driver.ErrSkip) + if err != nil && !compErr { 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) { + if w.opts.LoggerEnabled && w.opts.Logger.V(w.opts.LoggerLevel) && !compErr { w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "Query", getCallerName(), td, err)...).Log(ctx, w.opts.LoggerLevel) } return rows, err @@ -399,7 +405,8 @@ 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() - if err != nil { + compErr := errors.Is(err, driver.ErrSkip) + if err != nil && !compErr { w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelFailure)...).Inc() span.SetStatus(tracer.SpanStatusError, err.Error()) } else { @@ -407,7 +414,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) { + if w.opts.LoggerEnabled && w.opts.Logger.V(w.opts.LoggerLevel) && !compErr { w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "QueryContext", getCallerName(), td, err)...).Log(ctx, w.opts.LoggerLevel) } From 564efef60b3be7392eed94a463a428bcbf4b5061 Mon Sep 17 00:00:00 2001 From: devstigneev Date: Thu, 11 Apr 2024 11:38:09 +0300 Subject: [PATCH 2/2] 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 {