fix ping #63

Merged
vtolstov merged 1 commits from ping-fix into v3 2023-01-30 21:01:14 +03:00
Showing only changes of commit 8369576f3c - Show all commits

137
conn.go
View File

@ -241,28 +241,30 @@ func (w *wrapperConn) Exec(query string, args []driver.Value) (driver.Result, er
ctx = context.Background() ctx = context.Background()
} }
// nolint:staticcheck // nolint:staticcheck
labels := []string{labelMethod, "Exec", labelQuery, labelUnknown} execer, ok := w.conn.(driver.Execer)
if execer, ok := w.conn.(driver.Execer); ok { if !ok {
ts := time.Now()
res, err := execer.Exec(query, args)
td := time.Since(ts)
te := td.Seconds()
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 { if w.opts.LoggerEnabled {
w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "Exec", labelUnknown, td, err)...).Log(ctx, w.opts.LoggerLevel) w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "Exec", labelUnknown, 0, ErrUnsupported)...).Log(ctx, w.opts.LoggerLevel)
} }
return res, err return nil, ErrUnsupported
} }
labels := []string{labelMethod, "Exec", labelQuery, labelUnknown}
ts := time.Now()
res, err := execer.Exec(query, args)
td := time.Since(ts)
te := td.Seconds()
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 { if w.opts.LoggerEnabled {
w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "Exec", labelUnknown, 0, ErrUnsupported)...).Log(ctx, w.opts.LoggerLevel) w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "Exec", labelUnknown, td, err)...).Log(ctx, w.opts.LoggerLevel)
} }
return nil, ErrUnsupported return res, err
} }
// Exec implements driver.StmtExecContext ExecContext // Exec implements driver.StmtExecContext ExecContext
@ -337,38 +339,42 @@ 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)
var nctx context.Context if !ok {
var span tracer.Span if w.opts.LoggerEnabled {
if w.ctx != nil { w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "Ping", labelUnknown, 0, ErrUnsupported)...).Log(ctx, w.opts.LoggerLevel)
nctx, span = w.opts.Tracer.Start(w.ctx, "Ping")
} else {
nctx, span = w.opts.Tracer.Start(ctx, "Ping")
} }
defer span.Finish() return ErrUnsupported
labels := []string{labelMethod, "Ping"} }
ts := time.Now()
err := conn.Ping(nctx) var nctx context.Context
td := time.Since(ts) var span tracer.Span
te := td.Seconds() if w.ctx != nil {
if err != nil { nctx, span = w.opts.Tracer.Start(w.ctx, "Ping")
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelFailure)...).Inc() } else {
span.AddLabels("error", true) nctx, span = w.opts.Tracer.Start(ctx, "Ping")
span.AddLabels("err", err.Error()) }
if w.opts.LoggerEnabled { defer span.Finish()
w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "Ping", labelUnknown, td, err)...).Log(ctx, w.opts.LoggerLevel) labels := []string{labelMethod, "Ping"}
} ts := time.Now()
return err err := conn.Ping(nctx)
} else { td := time.Since(ts)
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelSuccess)...).Inc() te := td.Seconds()
if err != nil {
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelFailure)...).Inc()
span.AddLabels("error", true)
span.AddLabels("err", err.Error())
if w.opts.LoggerEnabled {
w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "Ping", labelUnknown, td, err)...).Log(ctx, w.opts.LoggerLevel)
} }
w.opts.Meter.Summary(meterRequestLatencyMicroseconds, labels...).Update(te) return err
w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te) } else {
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelSuccess)...).Inc()
} }
if w.opts.LoggerEnabled { w.opts.Meter.Summary(meterRequestLatencyMicroseconds, labels...).Update(te)
w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "Ping", labelUnknown, 0, ErrUnsupported)...).Log(ctx, w.opts.LoggerLevel) w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te)
}
return ErrUnsupported return nil
} }
// Query implements driver.Queryer Query // Query implements driver.Queryer Query
@ -379,29 +385,32 @@ func (w *wrapperConn) Query(query string, args []driver.Value) (driver.Rows, err
} else { } else {
ctx = context.Background() ctx = context.Background()
} }
// nolint:staticcheck
if conn, ok := w.conn.(driver.Queryer); ok { //nolint:staticcheck
labels := []string{labelMethod, "Query", labelQuery, labelUnknown} conn, ok := w.conn.(driver.Queryer)
ts := time.Now() if !ok {
rows, err := conn.Query(query, args)
td := time.Since(ts)
te := td.Seconds()
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 { if w.opts.LoggerEnabled {
w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "Query", labelUnknown, td, err)...).Log(ctx, w.opts.LoggerLevel) w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "Query", labelUnknown, 0, ErrUnsupported)...).Log(ctx, w.opts.LoggerLevel)
} }
return rows, err return nil, ErrUnsupported
} }
labels := []string{labelMethod, "Query", labelQuery, labelUnknown}
ts := time.Now()
rows, err := conn.Query(query, args)
td := time.Since(ts)
te := td.Seconds()
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 { if w.opts.LoggerEnabled {
w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "Query", labelUnknown, 0, ErrUnsupported)...).Log(ctx, w.opts.LoggerLevel) w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "Query", labelUnknown, td, err)...).Log(ctx, w.opts.LoggerLevel)
} }
return nil, ErrUnsupported return rows, err
} }
// QueryContext implements Driver.QueryerContext QueryContext // QueryContext implements Driver.QueryerContext QueryContext