moved compare error to LoggerObserver
Some checks failed
automerge / automerge (pull_request) Has been skipped
dependabot-automerge / automerge (pull_request) Has been skipped
autoapprove / autoapprove (pull_request) Successful in 8s
codeql / analyze (go) (pull_request) Has been cancelled
prbuild / test (pull_request) Has been cancelled
prbuild / lint (pull_request) Has been cancelled

This commit is contained in:
Денис Евстигнеев 2024-04-11 11:38:09 +03:00
parent cb9d32bf1c
commit 564efef60b
2 changed files with 15 additions and 20 deletions

31
conn.go
View File

@ -3,7 +3,6 @@ package wrapper
import ( import (
"context" "context"
"database/sql/driver" "database/sql/driver"
"errors"
"fmt" "fmt"
"time" "time"
@ -149,8 +148,7 @@ func (w *wrapperConn) Prepare(query string) (driver.Stmt, error) {
stmt, err := w.conn.Prepare(query) stmt, err := w.conn.Prepare(query)
td := time.Since(ts) td := time.Since(ts)
te := td.Seconds() te := td.Seconds()
compErr := errors.Is(err, driver.ErrSkip) if err != nil {
if err != nil && !compErr {
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)
@ -163,7 +161,7 @@ func (w *wrapperConn) Prepare(query string) (driver.Stmt, 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) && !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) 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) stmt, err := conn.PrepareContext(nctx, query)
td := time.Since(ts) td := time.Since(ts)
te := td.Seconds() te := td.Seconds()
compErr := errors.Is(err, driver.ErrSkip) if err != nil {
if err != nil && !compErr {
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)
@ -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.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) && !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) 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) res, err := conn.Exec(query, args)
td := time.Since(ts) td := time.Since(ts)
te := td.Seconds() te := td.Seconds()
compErr := errors.Is(err, driver.ErrSkip) if err != nil {
if err != nil && !compErr {
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelFailure)...).Inc() w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelFailure)...).Inc()
} 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) && !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) w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "Exec", getCallerName(), td, err)...).Log(ctx, w.opts.LoggerLevel)
} }
return res, err 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) res, err := conn.ExecContext(nctx, query, args)
td := time.Since(ts) td := time.Since(ts)
te := td.Seconds() te := td.Seconds()
compErr := errors.Is(err, driver.ErrSkip) if err != nil {
if err != nil && !compErr {
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())
} else { } 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.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) && !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) 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) rows, err := conn.Query(query, args)
td := time.Since(ts) td := time.Since(ts)
te := td.Seconds() te := td.Seconds()
compErr := errors.Is(err, driver.ErrSkip) if err != nil {
if err != nil && !compErr {
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelFailure)...).Inc() w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelFailure)...).Inc()
} 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) && !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) w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "Query", getCallerName(), td, err)...).Log(ctx, w.opts.LoggerLevel)
} }
return rows, err 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) rows, err := conn.QueryContext(nctx, query, args)
td := time.Since(ts) td := time.Since(ts)
te := td.Seconds() te := td.Seconds()
compErr := errors.Is(err, driver.ErrSkip) if err != nil {
if err != nil && !compErr {
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())
} else { } 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.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) && !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) w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "QueryContext", getCallerName(), td, err)...).Log(ctx, w.opts.LoggerLevel)
} }

View File

@ -2,6 +2,8 @@ package wrapper
import ( import (
"context" "context"
"database/sql"
"errors"
"fmt" "fmt"
"time" "time"
@ -18,7 +20,7 @@ var (
// DefaultLoggerObserver used to prepare labels for logger // DefaultLoggerObserver used to prepare labels for logger
DefaultLoggerObserver = func(ctx context.Context, method string, query string, td time.Duration, err error) []interface{} { 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)} 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()) labels = append(labels, "error", err.Error())
} }
if query != labelUnknown { if query != labelUnknown {