Merge pull request #63 from unistack-org/ping-fix

fix ping
This commit is contained in:
Василий Толстов 2023-01-30 21:01:14 +03:00 committed by GitHub
commit 38a14743de
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

45
conn.go
View File

@ -241,8 +241,15 @@ func (w *wrapperConn) Exec(query string, args []driver.Value) (driver.Result, er
ctx = context.Background() ctx = context.Background()
} }
// nolint:staticcheck // 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} labels := []string{labelMethod, "Exec", labelQuery, labelUnknown}
if execer, ok := w.conn.(driver.Execer); ok {
ts := time.Now() ts := time.Now()
res, err := execer.Exec(query, args) res, err := execer.Exec(query, args)
td := time.Since(ts) td := time.Since(ts)
@ -259,11 +266,6 @@ func (w *wrapperConn) Exec(query string, args []driver.Value) (driver.Result, er
} }
return res, err 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 // Exec implements driver.StmtExecContext ExecContext
func (w *wrapperConn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) { func (w *wrapperConn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) {
@ -337,7 +339,14 @@ func (w *wrapperConn) ExecContext(ctx context.Context, query string, args []driv
// Ping implements driver.Pinger Ping // Ping implements driver.Pinger Ping
func (w *wrapperConn) Ping(ctx context.Context) error { 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 nctx context.Context
var span tracer.Span var span tracer.Span
if w.ctx != nil { 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.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 { return nil
w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "Ping", labelUnknown, 0, ErrUnsupported)...).Log(ctx, w.opts.LoggerLevel)
}
return ErrUnsupported
} }
// Query implements driver.Queryer Query // Query implements driver.Queryer Query
@ -379,8 +385,16 @@ func (w *wrapperConn) Query(query string, args []driver.Value) (driver.Rows, err
} else { } else {
ctx = context.Background() ctx = context.Background()
} }
//nolint:staticcheck //nolint:staticcheck
if conn, ok := w.conn.(driver.Queryer); ok { 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} labels := []string{labelMethod, "Query", labelQuery, labelUnknown}
ts := time.Now() ts := time.Now()
rows, err := conn.Query(query, args) rows, err := conn.Query(query, args)
@ -398,11 +412,6 @@ func (w *wrapperConn) Query(query string, args []driver.Value) (driver.Rows, err
} }
return rows, err 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 // QueryContext implements Driver.QueryerContext QueryContext
func (w *wrapperConn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) { func (w *wrapperConn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {