2021-10-27 18:49:33 +03:00
|
|
|
package wrapper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql/driver"
|
2022-12-25 15:35:05 +03:00
|
|
|
"fmt"
|
2023-01-05 16:03:21 +03:00
|
|
|
"time"
|
2023-01-16 23:26:58 +03:00
|
|
|
|
2023-08-29 01:30:27 +03:00
|
|
|
"go.unistack.org/micro/v3/tracer"
|
2021-10-27 18:49:33 +03:00
|
|
|
)
|
|
|
|
|
2023-06-10 13:09:25 +03:00
|
|
|
var (
|
|
|
|
_ driver.Conn = (*wrapperConn)(nil)
|
|
|
|
_ driver.ConnBeginTx = (*wrapperConn)(nil)
|
|
|
|
_ driver.ConnPrepareContext = (*wrapperConn)(nil)
|
|
|
|
_ driver.Pinger = (*wrapperConn)(nil)
|
|
|
|
_ driver.Validator = (*wrapperConn)(nil)
|
2023-09-01 00:21:08 +03:00
|
|
|
_ driver.Queryer = (*wrapperConn)(nil) // nolint:staticcheck
|
2023-06-10 13:09:25 +03:00
|
|
|
_ driver.QueryerContext = (*wrapperConn)(nil)
|
2023-09-01 00:21:08 +03:00
|
|
|
_ driver.Execer = (*wrapperConn)(nil) // nolint:staticcheck
|
2023-06-10 13:09:25 +03:00
|
|
|
_ driver.ExecerContext = (*wrapperConn)(nil)
|
|
|
|
// _ driver.Connector
|
|
|
|
// _ driver.Driver
|
|
|
|
// _ driver.DriverContext
|
|
|
|
)
|
2023-01-16 23:26:58 +03:00
|
|
|
|
2021-10-27 18:49:33 +03:00
|
|
|
// wrapperConn defines a wrapper for driver.Conn
|
|
|
|
type wrapperConn struct {
|
2023-02-17 23:54:50 +03:00
|
|
|
d *wrapperDriver
|
|
|
|
dname string
|
|
|
|
conn driver.Conn
|
|
|
|
opts Options
|
|
|
|
ctx context.Context
|
2023-06-10 13:09:25 +03:00
|
|
|
span tracer.Span
|
2021-10-27 18:49:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close implements driver.Conn Close
|
|
|
|
func (w *wrapperConn) Close() error {
|
2023-01-16 23:38:29 +03:00
|
|
|
var ctx context.Context
|
|
|
|
if w.ctx != nil {
|
|
|
|
ctx = w.ctx
|
|
|
|
} else {
|
|
|
|
ctx = context.Background()
|
|
|
|
}
|
|
|
|
|
2023-01-05 16:03:21 +03:00
|
|
|
labels := []string{labelMethod, "Close"}
|
|
|
|
ts := time.Now()
|
|
|
|
err := w.conn.Close()
|
2023-01-06 23:24:39 +03:00
|
|
|
td := time.Since(ts)
|
|
|
|
te := td.Seconds()
|
2023-01-05 16:03:21 +03:00
|
|
|
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)
|
2023-01-06 23:24:39 +03:00
|
|
|
|
2023-06-10 13:09:25 +03:00
|
|
|
if w.opts.LoggerEnabled && w.opts.Logger.V(w.opts.LoggerLevel) {
|
2023-09-01 00:21:08 +03:00
|
|
|
w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "Close", getCallerName(), td, err)...).Log(ctx, w.opts.LoggerLevel)
|
2023-01-06 23:24:39 +03:00
|
|
|
}
|
|
|
|
|
2023-01-05 16:03:21 +03:00
|
|
|
return err
|
2021-10-27 18:49:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Begin implements driver.Conn Begin
|
|
|
|
func (w *wrapperConn) Begin() (driver.Tx, error) {
|
2023-01-16 23:38:29 +03:00
|
|
|
var ctx context.Context
|
|
|
|
if w.ctx != nil {
|
|
|
|
ctx = w.ctx
|
|
|
|
} else {
|
|
|
|
ctx = context.Background()
|
|
|
|
}
|
|
|
|
|
2023-01-05 16:03:21 +03:00
|
|
|
labels := []string{labelMethod, "Begin"}
|
|
|
|
ts := time.Now()
|
2023-06-10 13:09:25 +03:00
|
|
|
tx, err := w.conn.Begin() // nolint:staticcheck
|
2023-01-06 23:24:39 +03:00
|
|
|
td := time.Since(ts)
|
|
|
|
te := td.Seconds()
|
2021-10-27 18:49:33 +03:00
|
|
|
if err != nil {
|
2023-01-05 16:03:21 +03:00
|
|
|
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)
|
2023-06-10 13:09:25 +03:00
|
|
|
if w.opts.LoggerEnabled && w.opts.Logger.V(w.opts.LoggerLevel) {
|
2023-09-01 00:21:08 +03:00
|
|
|
w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "Begin", getCallerName(), td, err)...).Log(ctx, w.opts.LoggerLevel)
|
2023-01-06 23:24:39 +03:00
|
|
|
}
|
2021-10-27 18:49:33 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
2023-01-05 16:03:21 +03:00
|
|
|
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)
|
2023-06-10 13:09:25 +03:00
|
|
|
if w.opts.LoggerEnabled && w.opts.Logger.V(w.opts.LoggerLevel) {
|
2023-09-01 00:21:08 +03:00
|
|
|
w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "Begin", getCallerName(), td, err)...).Log(ctx, w.opts.LoggerLevel)
|
2023-01-06 23:24:39 +03:00
|
|
|
}
|
2023-06-10 13:09:25 +03:00
|
|
|
|
2023-01-16 23:38:29 +03:00
|
|
|
return &wrapperTx{tx: tx, opts: w.opts, ctx: ctx}, nil
|
2021-10-27 18:49:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// BeginTx implements driver.ConnBeginTx BeginTx
|
|
|
|
func (w *wrapperConn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error) {
|
2023-09-01 00:21:08 +03:00
|
|
|
nctx, span := w.opts.Tracer.Start(ctx, "sdk.database", tracer.WithSpanKind(tracer.SpanKindClient))
|
2023-01-05 16:03:21 +03:00
|
|
|
span.AddLabels("method", "BeginTx")
|
|
|
|
name := getQueryName(ctx)
|
|
|
|
if name != "" {
|
2023-09-01 00:21:08 +03:00
|
|
|
span.AddLabels("db.query", name)
|
2023-01-05 16:03:21 +03:00
|
|
|
} else {
|
2023-09-01 00:21:08 +03:00
|
|
|
name = getCallerName()
|
2022-12-25 15:35:05 +03:00
|
|
|
}
|
2023-01-05 16:03:21 +03:00
|
|
|
labels := []string{labelMethod, "BeginTx", labelQuery, name}
|
2023-06-10 13:09:25 +03:00
|
|
|
|
|
|
|
connBeginTx, ok := w.conn.(driver.ConnBeginTx)
|
|
|
|
if !ok {
|
|
|
|
return w.Begin()
|
2021-10-27 18:49:33 +03:00
|
|
|
}
|
2023-06-10 13:09:25 +03:00
|
|
|
|
2023-01-05 16:03:21 +03:00
|
|
|
ts := time.Now()
|
2023-06-10 13:09:25 +03:00
|
|
|
tx, err := connBeginTx.BeginTx(nctx, opts)
|
2023-01-06 23:24:39 +03:00
|
|
|
td := time.Since(ts)
|
|
|
|
te := td.Seconds()
|
2022-12-25 15:35:05 +03:00
|
|
|
if err != nil {
|
2023-01-05 16:03:21 +03:00
|
|
|
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)
|
2023-09-01 00:21:08 +03:00
|
|
|
span.SetStatus(tracer.SpanStatusError, err.Error())
|
2023-06-10 13:09:25 +03:00
|
|
|
if w.opts.LoggerEnabled && w.opts.Logger.V(w.opts.LoggerLevel) {
|
2023-09-01 00:21:08 +03:00
|
|
|
w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "BeginTx", getCallerName(), td, err)...).Log(ctx, w.opts.LoggerLevel)
|
2023-06-10 13:09:25 +03:00
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelSuccess)...).Inc()
|
|
|
|
if w.opts.LoggerEnabled && w.opts.Logger.V(w.opts.LoggerLevel) {
|
2023-09-01 00:21:08 +03:00
|
|
|
w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "BeginTx", getCallerName(), td, err)...).Log(ctx, w.opts.LoggerLevel)
|
2023-06-10 13:09:25 +03:00
|
|
|
}
|
|
|
|
return &wrapperTx{tx: tx, opts: w.opts, ctx: ctx, span: span}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prepare implements driver.Conn Prepare
|
|
|
|
func (w *wrapperConn) Prepare(query string) (driver.Stmt, error) {
|
|
|
|
var ctx context.Context
|
|
|
|
if w.ctx != nil {
|
|
|
|
ctx = w.ctx
|
|
|
|
} else {
|
|
|
|
ctx = context.Background()
|
|
|
|
}
|
|
|
|
|
2023-09-01 00:21:08 +03:00
|
|
|
labels := []string{labelMethod, "Prepare", labelQuery, getCallerName()}
|
2023-06-10 13:09:25 +03:00
|
|
|
ts := time.Now()
|
|
|
|
stmt, err := w.conn.Prepare(query)
|
|
|
|
td := time.Since(ts)
|
|
|
|
te := td.Seconds()
|
|
|
|
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)
|
|
|
|
if w.opts.LoggerEnabled && w.opts.Logger.V(w.opts.LoggerLevel) {
|
2023-09-01 00:21:08 +03:00
|
|
|
w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "Prepare", getCallerName(), td, err)...).Log(ctx, w.opts.LoggerLevel)
|
2023-06-10 13:09:25 +03:00
|
|
|
}
|
|
|
|
return nil, err
|
2022-12-25 15:35:05 +03:00
|
|
|
}
|
2023-01-05 16:03:21 +03:00
|
|
|
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)
|
2023-06-10 13:09:25 +03:00
|
|
|
|
|
|
|
if w.opts.LoggerEnabled && w.opts.Logger.V(w.opts.LoggerLevel) {
|
2023-09-01 00:21:08 +03:00
|
|
|
w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "Prepare", getCallerName(), td, err)...).Log(ctx, w.opts.LoggerLevel)
|
2023-01-06 23:24:39 +03:00
|
|
|
}
|
2023-06-10 13:09:25 +03:00
|
|
|
|
|
|
|
return wrapStmt(stmt, query, w.opts), nil
|
2021-10-27 18:49:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// PrepareContext implements driver.ConnPrepareContext PrepareContext
|
|
|
|
func (w *wrapperConn) PrepareContext(ctx context.Context, query string) (driver.Stmt, error) {
|
2023-01-16 23:26:58 +03:00
|
|
|
var nctx context.Context
|
|
|
|
var span tracer.Span
|
2023-09-01 00:21:08 +03:00
|
|
|
|
2023-01-16 23:26:58 +03:00
|
|
|
if w.ctx != nil {
|
2023-09-01 00:21:08 +03:00
|
|
|
nctx, span = w.opts.Tracer.Start(w.ctx, "sdk.database", tracer.WithSpanKind(tracer.SpanKindClient))
|
2023-01-16 23:26:58 +03:00
|
|
|
} else {
|
2023-09-01 00:21:08 +03:00
|
|
|
nctx, span = w.opts.Tracer.Start(ctx, "sdk.database", tracer.WithSpanKind(tracer.SpanKindClient))
|
2023-01-16 23:26:58 +03:00
|
|
|
}
|
2023-01-05 16:03:21 +03:00
|
|
|
span.AddLabels("method", "PrepareContext")
|
|
|
|
name := getQueryName(ctx)
|
|
|
|
if name != "" {
|
2023-09-01 00:21:08 +03:00
|
|
|
span.AddLabels("db.query", name)
|
2023-01-05 16:03:21 +03:00
|
|
|
} else {
|
2023-09-01 00:21:08 +03:00
|
|
|
name = getCallerName()
|
2022-12-25 15:35:05 +03:00
|
|
|
}
|
2023-06-10 13:09:25 +03:00
|
|
|
|
2023-01-05 16:03:21 +03:00
|
|
|
labels := []string{labelMethod, "PrepareContext", labelQuery, name}
|
2023-06-10 13:09:25 +03:00
|
|
|
conn, ok := w.conn.(driver.ConnPrepareContext)
|
|
|
|
if !ok {
|
|
|
|
return w.Prepare(query)
|
2021-10-27 18:49:33 +03:00
|
|
|
}
|
2023-06-10 13:09:25 +03:00
|
|
|
|
2023-01-05 16:03:21 +03:00
|
|
|
ts := time.Now()
|
2023-06-10 13:09:25 +03:00
|
|
|
stmt, err := conn.PrepareContext(nctx, query)
|
2023-01-06 23:24:39 +03:00
|
|
|
td := time.Since(ts)
|
|
|
|
te := td.Seconds()
|
2022-12-25 15:35:05 +03:00
|
|
|
if err != nil {
|
2023-01-05 16:03:21 +03:00
|
|
|
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)
|
2023-09-01 00:21:08 +03:00
|
|
|
span.SetStatus(tracer.SpanStatusError, err.Error())
|
2023-06-10 13:09:25 +03:00
|
|
|
if w.opts.LoggerEnabled && w.opts.Logger.V(w.opts.LoggerLevel) {
|
2023-09-01 00:21:08 +03:00
|
|
|
w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "PrepareContext", getCallerName(), td, err)...).Log(ctx, w.opts.LoggerLevel)
|
2023-06-10 13:09:25 +03:00
|
|
|
}
|
|
|
|
return nil, err
|
2022-12-25 15:35:05 +03:00
|
|
|
}
|
2023-01-05 16:03:21 +03:00
|
|
|
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)
|
2023-06-10 13:09:25 +03:00
|
|
|
if w.opts.LoggerEnabled && w.opts.Logger.V(w.opts.LoggerLevel) {
|
2023-09-01 00:21:08 +03:00
|
|
|
w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "PrepareContext", getCallerName(), td, err)...).Log(ctx, w.opts.LoggerLevel)
|
2023-01-06 23:24:39 +03:00
|
|
|
}
|
2023-06-10 13:09:25 +03:00
|
|
|
|
|
|
|
return wrapStmt(stmt, query, w.opts), nil
|
2021-10-27 18:49:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Exec implements driver.Execer Exec
|
|
|
|
func (w *wrapperConn) Exec(query string, args []driver.Value) (driver.Result, error) {
|
2023-01-16 23:38:29 +03:00
|
|
|
var ctx context.Context
|
|
|
|
if w.ctx != nil {
|
|
|
|
ctx = w.ctx
|
|
|
|
} else {
|
|
|
|
ctx = context.Background()
|
|
|
|
}
|
2023-09-01 00:21:08 +03:00
|
|
|
labels := []string{labelMethod, "Exec", labelQuery, getCallerName()}
|
2023-06-10 13:09:25 +03:00
|
|
|
|
2023-01-05 16:03:21 +03:00
|
|
|
// nolint:staticcheck
|
2023-06-10 13:09:25 +03:00
|
|
|
conn, ok := w.conn.(driver.Execer)
|
2023-01-30 20:59:04 +03:00
|
|
|
if !ok {
|
2023-02-17 23:54:50 +03:00
|
|
|
return nil, driver.ErrSkip
|
2023-01-30 20:59:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ts := time.Now()
|
2023-06-10 13:09:25 +03:00
|
|
|
res, err := conn.Exec(query, args)
|
2023-01-30 20:59:04 +03:00
|
|
|
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()
|
2021-10-27 18:49:33 +03:00
|
|
|
}
|
2023-01-30 20:59:04 +03:00
|
|
|
w.opts.Meter.Summary(meterRequestLatencyMicroseconds, labels...).Update(te)
|
|
|
|
w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te)
|
2023-06-10 13:09:25 +03:00
|
|
|
if w.opts.LoggerEnabled && w.opts.Logger.V(w.opts.LoggerLevel) {
|
2023-09-01 00:21:08 +03:00
|
|
|
w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "Exec", getCallerName(), td, err)...).Log(ctx, w.opts.LoggerLevel)
|
2023-01-06 23:24:39 +03:00
|
|
|
}
|
2023-01-30 20:59:04 +03:00
|
|
|
return res, err
|
2021-10-27 18:49:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Exec implements driver.StmtExecContext ExecContext
|
|
|
|
func (w *wrapperConn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) {
|
2023-01-16 23:26:58 +03:00
|
|
|
var nctx context.Context
|
|
|
|
var span tracer.Span
|
2023-06-10 13:09:25 +03:00
|
|
|
|
2023-01-16 23:26:58 +03:00
|
|
|
if w.ctx != nil {
|
2023-09-01 00:21:08 +03:00
|
|
|
nctx, span = w.opts.Tracer.Start(w.ctx, "sdk.database", tracer.WithSpanKind(tracer.SpanKindClient))
|
2023-01-16 23:26:58 +03:00
|
|
|
} else {
|
2023-09-01 00:21:08 +03:00
|
|
|
nctx, span = w.opts.Tracer.Start(ctx, "sdk.database", tracer.WithSpanKind(tracer.SpanKindClient))
|
2023-01-16 23:26:58 +03:00
|
|
|
}
|
2023-01-05 16:03:21 +03:00
|
|
|
span.AddLabels("method", "ExecContext")
|
|
|
|
name := getQueryName(ctx)
|
|
|
|
if name != "" {
|
2023-09-01 00:21:08 +03:00
|
|
|
span.AddLabels("db.query", name)
|
2023-01-05 16:03:21 +03:00
|
|
|
} else {
|
2023-09-01 00:21:08 +03:00
|
|
|
name = getCallerName()
|
2022-12-25 15:35:05 +03:00
|
|
|
}
|
2021-10-27 18:49:33 +03:00
|
|
|
defer span.Finish()
|
2022-12-31 15:50:03 +03:00
|
|
|
if len(args) > 0 {
|
2023-09-01 00:21:08 +03:00
|
|
|
span.AddLabels("db.args", fmt.Sprintf("%v", namedValueToLabels(args)))
|
2022-12-25 15:35:05 +03:00
|
|
|
}
|
2023-01-05 16:03:21 +03:00
|
|
|
labels := []string{labelMethod, "ExecContext", labelQuery, name}
|
2023-06-10 13:09:25 +03:00
|
|
|
|
|
|
|
conn, ok := w.conn.(driver.ExecerContext)
|
|
|
|
if !ok {
|
|
|
|
// nolint:staticcheck
|
|
|
|
return nil, driver.ErrSkip
|
2021-10-27 18:49:33 +03:00
|
|
|
}
|
2023-06-10 13:09:25 +03:00
|
|
|
|
2023-01-05 16:03:21 +03:00
|
|
|
ts := time.Now()
|
2023-06-10 13:09:25 +03:00
|
|
|
res, err := conn.ExecContext(nctx, query, args)
|
2023-01-06 23:24:39 +03:00
|
|
|
td := time.Since(ts)
|
|
|
|
te := td.Seconds()
|
2022-12-25 15:35:05 +03:00
|
|
|
if err != nil {
|
2023-01-05 16:03:21 +03:00
|
|
|
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelFailure)...).Inc()
|
2023-09-01 00:21:08 +03:00
|
|
|
span.SetStatus(tracer.SpanStatusError, err.Error())
|
2023-01-05 16:03:21 +03:00
|
|
|
} else {
|
|
|
|
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelSuccess)...).Inc()
|
2022-12-25 15:35:05 +03:00
|
|
|
}
|
2023-06-10 13:09:25 +03:00
|
|
|
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelSuccess)...).Inc()
|
2023-01-05 16:03:21 +03:00
|
|
|
w.opts.Meter.Summary(meterRequestLatencyMicroseconds, labels...).Update(te)
|
|
|
|
w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te)
|
2023-06-10 13:09:25 +03:00
|
|
|
if w.opts.LoggerEnabled && w.opts.Logger.V(w.opts.LoggerLevel) {
|
2023-09-01 00:21:08 +03:00
|
|
|
w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "ExecContext", getCallerName(), td, err)...).Log(ctx, w.opts.LoggerLevel)
|
2023-01-06 23:24:39 +03:00
|
|
|
}
|
2023-06-10 13:09:25 +03:00
|
|
|
|
2022-12-25 15:35:05 +03:00
|
|
|
return res, err
|
2021-10-27 18:49:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ping implements driver.Pinger Ping
|
|
|
|
func (w *wrapperConn) Ping(ctx context.Context) error {
|
2023-01-30 20:59:04 +03:00
|
|
|
conn, ok := w.conn.(driver.Pinger)
|
2023-06-10 13:09:25 +03:00
|
|
|
|
2023-01-30 20:59:04 +03:00
|
|
|
if !ok {
|
2023-06-10 13:09:25 +03:00
|
|
|
// fallback path to check db alive
|
|
|
|
pc, err := w.d.Open(w.dname)
|
2023-02-17 23:54:50 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-12-31 15:50:03 +03:00
|
|
|
}
|
2023-06-10 13:09:25 +03:00
|
|
|
return pc.Close()
|
2021-10-27 18:49:33 +03:00
|
|
|
}
|
2023-01-30 20:59:04 +03:00
|
|
|
|
|
|
|
var nctx context.Context
|
|
|
|
var span tracer.Span
|
|
|
|
if w.ctx != nil {
|
2023-09-01 00:21:08 +03:00
|
|
|
nctx, span = w.opts.Tracer.Start(w.ctx, "sdk.database", tracer.WithSpanKind(tracer.SpanKindClient))
|
2023-01-30 20:59:04 +03:00
|
|
|
} else {
|
2023-09-01 00:21:08 +03:00
|
|
|
nctx, span = w.opts.Tracer.Start(ctx, "sdk.database", tracer.WithSpanKind(tracer.SpanKindClient))
|
2023-01-30 20:59:04 +03:00
|
|
|
}
|
|
|
|
defer span.Finish()
|
|
|
|
labels := []string{labelMethod, "Ping"}
|
|
|
|
ts := time.Now()
|
|
|
|
err := conn.Ping(nctx)
|
|
|
|
td := time.Since(ts)
|
|
|
|
te := td.Seconds()
|
|
|
|
if err != nil {
|
|
|
|
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelFailure)...).Inc()
|
2023-09-01 00:21:08 +03:00
|
|
|
span.SetStatus(tracer.SpanStatusError, err.Error())
|
2023-06-10 13:09:25 +03:00
|
|
|
if w.opts.LoggerEnabled && w.opts.Logger.V(w.opts.LoggerLevel) {
|
2023-09-01 00:21:08 +03:00
|
|
|
w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "Ping", getCallerName(), td, err)...).Log(ctx, w.opts.LoggerLevel)
|
2023-01-30 20:59:04 +03:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelSuccess)...).Inc()
|
2023-01-06 23:24:39 +03:00
|
|
|
}
|
2023-01-30 20:59:04 +03:00
|
|
|
w.opts.Meter.Summary(meterRequestLatencyMicroseconds, labels...).Update(te)
|
|
|
|
w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te)
|
|
|
|
|
|
|
|
return nil
|
2021-10-27 18:49:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Query implements driver.Queryer Query
|
|
|
|
func (w *wrapperConn) Query(query string, args []driver.Value) (driver.Rows, error) {
|
2023-01-16 23:38:29 +03:00
|
|
|
var ctx context.Context
|
|
|
|
if w.ctx != nil {
|
|
|
|
ctx = w.ctx
|
|
|
|
} else {
|
|
|
|
ctx = context.Background()
|
|
|
|
}
|
2023-06-10 13:09:25 +03:00
|
|
|
// nolint:staticcheck
|
2023-01-30 20:59:04 +03:00
|
|
|
conn, ok := w.conn.(driver.Queryer)
|
|
|
|
if !ok {
|
2023-02-17 23:54:50 +03:00
|
|
|
return nil, driver.ErrSkip
|
2021-10-27 18:49:33 +03:00
|
|
|
}
|
2023-01-30 20:59:04 +03:00
|
|
|
|
2023-09-01 00:21:08 +03:00
|
|
|
labels := []string{labelMethod, "Query", labelQuery, getCallerName()}
|
2023-01-30 20:59:04 +03:00
|
|
|
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)
|
2023-06-10 13:09:25 +03:00
|
|
|
if w.opts.LoggerEnabled && w.opts.Logger.V(w.opts.LoggerLevel) {
|
2023-09-01 00:21:08 +03:00
|
|
|
w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "Query", getCallerName(), td, err)...).Log(ctx, w.opts.LoggerLevel)
|
2023-01-06 23:24:39 +03:00
|
|
|
}
|
2023-01-30 20:59:04 +03:00
|
|
|
return rows, err
|
2021-10-27 18:49:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// QueryContext implements Driver.QueryerContext QueryContext
|
2022-12-25 15:35:05 +03:00
|
|
|
func (w *wrapperConn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {
|
2023-01-16 23:26:58 +03:00
|
|
|
var nctx context.Context
|
|
|
|
var span tracer.Span
|
2023-09-01 00:21:08 +03:00
|
|
|
|
2023-01-16 23:26:58 +03:00
|
|
|
if w.ctx != nil {
|
2023-09-01 00:21:08 +03:00
|
|
|
nctx, span = w.opts.Tracer.Start(w.ctx, "sdk.database", tracer.WithSpanKind(tracer.SpanKindClient))
|
2023-01-16 23:26:58 +03:00
|
|
|
} else {
|
2023-09-01 00:21:08 +03:00
|
|
|
nctx, span = w.opts.Tracer.Start(ctx, "sdk.database", tracer.WithSpanKind(tracer.SpanKindClient))
|
2023-01-16 23:26:58 +03:00
|
|
|
}
|
2023-01-05 16:03:21 +03:00
|
|
|
span.AddLabels("method", "QueryContext")
|
|
|
|
name := getQueryName(ctx)
|
|
|
|
if name != "" {
|
2023-09-01 00:21:08 +03:00
|
|
|
span.AddLabels("db.statement", name)
|
2023-01-05 16:03:21 +03:00
|
|
|
} else {
|
2023-09-01 00:21:08 +03:00
|
|
|
name = getCallerName()
|
2022-12-25 15:35:05 +03:00
|
|
|
}
|
2021-10-27 18:49:33 +03:00
|
|
|
defer span.Finish()
|
2022-12-31 15:50:03 +03:00
|
|
|
if len(args) > 0 {
|
2023-09-01 00:21:08 +03:00
|
|
|
span.AddLabels("db.args", fmt.Sprintf("%v", namedValueToLabels(args)))
|
2022-12-25 15:35:05 +03:00
|
|
|
}
|
2023-01-05 16:03:21 +03:00
|
|
|
labels := []string{labelMethod, "QueryContext", labelQuery, name}
|
2023-06-10 13:09:25 +03:00
|
|
|
conn, ok := w.conn.(driver.QueryerContext)
|
|
|
|
if !ok {
|
|
|
|
return nil, driver.ErrSkip
|
2021-10-27 18:49:33 +03:00
|
|
|
}
|
2023-06-10 13:09:25 +03:00
|
|
|
|
2023-01-05 16:03:21 +03:00
|
|
|
ts := time.Now()
|
2023-06-10 13:09:25 +03:00
|
|
|
rows, err := conn.QueryContext(nctx, query, args)
|
2023-01-06 23:24:39 +03:00
|
|
|
td := time.Since(ts)
|
|
|
|
te := td.Seconds()
|
2022-12-25 15:35:05 +03:00
|
|
|
if err != nil {
|
2023-01-05 16:03:21 +03:00
|
|
|
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelFailure)...).Inc()
|
2023-09-01 00:21:08 +03:00
|
|
|
span.SetStatus(tracer.SpanStatusError, err.Error())
|
2023-01-05 16:03:21 +03:00
|
|
|
} else {
|
|
|
|
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelSuccess)...).Inc()
|
2022-12-25 15:35:05 +03:00
|
|
|
}
|
2023-01-05 16:03:21 +03:00
|
|
|
w.opts.Meter.Summary(meterRequestLatencyMicroseconds, labels...).Update(te)
|
|
|
|
w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te)
|
2023-06-10 13:09:25 +03:00
|
|
|
if w.opts.LoggerEnabled && w.opts.Logger.V(w.opts.LoggerLevel) {
|
2023-09-01 00:21:08 +03:00
|
|
|
w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "QueryContext", getCallerName(), td, err)...).Log(ctx, w.opts.LoggerLevel)
|
2023-01-06 23:24:39 +03:00
|
|
|
}
|
2023-06-10 13:09:25 +03:00
|
|
|
|
2022-12-25 15:35:05 +03:00
|
|
|
return rows, err
|
2021-10-27 18:49:33 +03:00
|
|
|
}
|
2023-06-10 13:09:25 +03:00
|
|
|
|
|
|
|
// CheckNamedValue implements driver.NamedValueChecker
|
|
|
|
func (w *wrapperConn) CheckNamedValue(v *driver.NamedValue) error {
|
|
|
|
s, ok := w.conn.(driver.NamedValueChecker)
|
|
|
|
if !ok {
|
|
|
|
return driver.ErrSkip
|
|
|
|
}
|
|
|
|
return s.CheckNamedValue(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsValid implements driver.Validator
|
|
|
|
func (w *wrapperConn) IsValid() bool {
|
|
|
|
v, ok := w.conn.(driver.Validator)
|
|
|
|
if !ok {
|
|
|
|
return w.conn != nil
|
|
|
|
}
|
|
|
|
return v.IsValid()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *wrapperConn) ResetSession(ctx context.Context) error {
|
|
|
|
s, ok := w.conn.(driver.SessionResetter)
|
|
|
|
if !ok {
|
|
|
|
return driver.ErrSkip
|
|
|
|
}
|
|
|
|
return s.ResetSession(ctx)
|
|
|
|
}
|