fix ping #63

Merged
vtolstov merged 1 commits from ping-fix into v3 2023-01-30 21:01:14 +03:00

47
conn.go
View File

@ -241,8 +241,15 @@ func (w *wrapperConn) Exec(query string, args []driver.Value) (driver.Result, er
ctx = context.Background()
}
// nolint:staticcheck
execer, ok := w.conn.(driver.Execer)
if !ok {
if w.opts.LoggerEnabled {
w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "Exec", labelUnknown, 0, ErrUnsupported)...).Log(ctx, w.opts.LoggerLevel)
}
return nil, ErrUnsupported
}
labels := []string{labelMethod, "Exec", labelQuery, labelUnknown}
if execer, ok := w.conn.(driver.Execer); ok {
ts := time.Now()
res, err := execer.Exec(query, args)
td := time.Since(ts)
@ -258,11 +265,6 @@ func (w *wrapperConn) Exec(query string, args []driver.Value) (driver.Result, er
w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "Exec", labelUnknown, td, err)...).Log(ctx, w.opts.LoggerLevel)
}
return res, err
}
if w.opts.LoggerEnabled {
w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "Exec", labelUnknown, 0, ErrUnsupported)...).Log(ctx, w.opts.LoggerLevel)
}
return nil, ErrUnsupported
}
// Exec implements driver.StmtExecContext ExecContext
@ -337,7 +339,14 @@ func (w *wrapperConn) ExecContext(ctx context.Context, query string, args []driv
// Ping implements driver.Pinger Ping
func (w *wrapperConn) Ping(ctx context.Context) error {
if conn, ok := w.conn.(driver.Pinger); ok {
conn, ok := w.conn.(driver.Pinger)
if !ok {
if w.opts.LoggerEnabled {
w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "Ping", labelUnknown, 0, ErrUnsupported)...).Log(ctx, w.opts.LoggerLevel)
}
return ErrUnsupported
}
var nctx context.Context
var span tracer.Span
if w.ctx != nil {
@ -364,11 +373,8 @@ func (w *wrapperConn) Ping(ctx context.Context) error {
}
w.opts.Meter.Summary(meterRequestLatencyMicroseconds, labels...).Update(te)
w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te)
}
if w.opts.LoggerEnabled {
w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "Ping", labelUnknown, 0, ErrUnsupported)...).Log(ctx, w.opts.LoggerLevel)
}
return ErrUnsupported
return nil
}
// Query implements driver.Queryer Query
@ -379,8 +385,16 @@ func (w *wrapperConn) Query(query string, args []driver.Value) (driver.Rows, err
} else {
ctx = context.Background()
}
// nolint:staticcheck
if conn, ok := w.conn.(driver.Queryer); ok {
//nolint:staticcheck
conn, ok := w.conn.(driver.Queryer)
if !ok {
if w.opts.LoggerEnabled {
w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "Query", labelUnknown, 0, ErrUnsupported)...).Log(ctx, w.opts.LoggerLevel)
}
return nil, ErrUnsupported
}
labels := []string{labelMethod, "Query", labelQuery, labelUnknown}
ts := time.Now()
rows, err := conn.Query(query, args)
@ -397,11 +411,6 @@ func (w *wrapperConn) Query(query string, args []driver.Value) (driver.Rows, err
w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "Query", labelUnknown, td, err)...).Log(ctx, w.opts.LoggerLevel)
}
return rows, err
}
if w.opts.LoggerEnabled {
w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "Query", labelUnknown, 0, ErrUnsupported)...).Log(ctx, w.opts.LoggerLevel)
}
return nil, ErrUnsupported
}
// QueryContext implements Driver.QueryerContext QueryContext