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-09-01 14:32:27 +03:00
|
|
|
requestid "go.unistack.org/micro-wrapper-requestid/v3"
|
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.Stmt = (*wrapperStmt)(nil)
|
|
|
|
_ driver.StmtQueryContext = (*wrapperStmt)(nil)
|
|
|
|
_ driver.StmtExecContext = (*wrapperStmt)(nil)
|
|
|
|
_ driver.NamedValueChecker = (*wrapperStmt)(nil)
|
|
|
|
)
|
2023-01-16 23:26:58 +03:00
|
|
|
|
2021-10-27 18:49:33 +03:00
|
|
|
// wrapperStmt defines a wrapper for driver.Stmt
|
|
|
|
type wrapperStmt struct {
|
2023-06-10 13:09:25 +03:00
|
|
|
stmt driver.Stmt
|
|
|
|
opts Options
|
|
|
|
query string
|
|
|
|
ctx context.Context
|
2021-10-27 18:49:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close implements driver.Stmt Close
|
|
|
|
func (w *wrapperStmt) 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.stmt.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 14:32:27 +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
|
|
|
}
|
|
|
|
|
|
|
|
// NumInput implements driver.Stmt NumInput
|
|
|
|
func (w *wrapperStmt) NumInput() int {
|
|
|
|
return w.stmt.NumInput()
|
|
|
|
}
|
|
|
|
|
2023-06-10 13:09:25 +03:00
|
|
|
// CheckNamedValue implements driver.NamedValueChecker
|
|
|
|
func (w *wrapperStmt) CheckNamedValue(v *driver.NamedValue) error {
|
|
|
|
s, ok := w.stmt.(driver.NamedValueChecker)
|
|
|
|
if !ok {
|
|
|
|
return driver.ErrSkip
|
|
|
|
}
|
|
|
|
return s.CheckNamedValue(v)
|
|
|
|
}
|
|
|
|
|
2021-10-27 18:49:33 +03:00
|
|
|
// Exec implements driver.Stmt Exec
|
|
|
|
func (w *wrapperStmt) Exec(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-01-05 16:03:21 +03:00
|
|
|
labels := []string{labelMethod, "Exec"}
|
|
|
|
ts := time.Now()
|
2023-08-29 01:25:45 +03:00
|
|
|
res, err := w.stmt.Exec(args) // nolint:staticcheck
|
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 14:32:27 +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-06-10 13:09:25 +03:00
|
|
|
|
2023-01-05 16:03:21 +03:00
|
|
|
return res, err
|
2021-10-27 18:49:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Query implements driver.Stmt Query
|
|
|
|
func (w *wrapperStmt) Query(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-01-05 16:03:21 +03:00
|
|
|
labels := []string{labelMethod, "Query"}
|
|
|
|
ts := time.Now()
|
2023-09-01 14:32:27 +03:00
|
|
|
rows, err := w.stmt.Query(args) // nolint:staticcheck
|
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 14:32:27 +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-06-10 13:09:25 +03:00
|
|
|
|
2023-01-05 16:03:21 +03:00
|
|
|
return rows, err
|
2021-10-27 18:49:33 +03:00
|
|
|
}
|
|
|
|
|
2023-09-01 14:32:27 +03:00
|
|
|
// ColumnConverter implements driver.ColumnConverter
|
|
|
|
func (w *wrapperStmt) ColumnConverter(idx int) driver.ValueConverter {
|
|
|
|
s, ok := w.stmt.(driver.ColumnConverter) // nolint:staticcheck
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return s.ColumnConverter(idx)
|
|
|
|
}
|
|
|
|
|
2023-06-10 13:09:25 +03:00
|
|
|
// ExecContext implements driver.StmtExecContext ExecContext
|
|
|
|
func (w *wrapperStmt) ExecContext(ctx context.Context, args []driver.NamedValue) (driver.Result, error) {
|
2023-01-16 23:26:58 +03:00
|
|
|
var nctx context.Context
|
|
|
|
var span tracer.Span
|
2023-09-01 14:32:27 +03:00
|
|
|
|
2023-01-16 23:26:58 +03:00
|
|
|
if w.ctx != nil {
|
2023-09-01 14:32:27 +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 14:32:27 +03:00
|
|
|
nctx, span = w.opts.Tracer.Start(ctx, "sdk.database", tracer.WithSpanKind(tracer.SpanKindClient))
|
2023-01-16 23:26:58 +03:00
|
|
|
}
|
2023-09-01 14:32:27 +03:00
|
|
|
span.AddLabels("db.method", "ExecContext")
|
2023-01-05 16:03:21 +03:00
|
|
|
name := getQueryName(ctx)
|
2023-09-01 14:32:27 +03:00
|
|
|
span.AddLabels("db.statement", name)
|
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 14:32:27 +03:00
|
|
|
span.AddLabels("db.args", fmt.Sprintf("%v", namedValueToLabels(args)))
|
|
|
|
}
|
|
|
|
if id, ok := ctx.Value(requestid.XRequestIDKey).(string); ok {
|
|
|
|
span.AddLabels("x-request-id", id)
|
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
|
|
|
|
|
|
|
if conn, ok := w.stmt.(driver.StmtExecContext); ok {
|
2023-01-05 16:03:21 +03:00
|
|
|
ts := time.Now()
|
2023-06-10 13:09:25 +03:00
|
|
|
res, err := conn.ExecContext(nctx, 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 14:32:27 +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-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 14:32:27 +03:00
|
|
|
w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "ExecContext", name, td, err)).Log(ctx, w.opts.LoggerLevel)
|
2023-01-06 23:24:39 +03:00
|
|
|
}
|
2022-12-25 15:35:05 +03:00
|
|
|
return res, err
|
2021-10-27 18:49:33 +03:00
|
|
|
}
|
2023-06-10 13:09:25 +03:00
|
|
|
|
2021-10-27 18:49:33 +03:00
|
|
|
values, err := namedValueToValue(args)
|
|
|
|
if err != nil {
|
2023-01-05 16:03:21 +03:00
|
|
|
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelFailure)...).Inc()
|
2023-09-01 14:32:27 +03:00
|
|
|
span.SetStatus(tracer.SpanStatusError, err.Error())
|
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 14:32:27 +03:00
|
|
|
w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "ExecContext", name, 0, 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-06 23:24:39 +03:00
|
|
|
ts := time.Now()
|
2023-06-10 13:09:25 +03:00
|
|
|
res, err := w.Exec(values) // nolint:staticcheck
|
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-06 23:24:39 +03:00
|
|
|
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelFailure)...).Inc()
|
2023-09-01 14:32:27 +03:00
|
|
|
span.SetStatus(tracer.SpanStatusError, err.Error())
|
2023-01-06 23:24:39 +03:00
|
|
|
} 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 14:32:27 +03:00
|
|
|
w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "ExecContext", name, td, err)).Log(ctx, w.opts.LoggerLevel)
|
2022-12-25 15:35:05 +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
|
|
|
}
|
|
|
|
|
2023-06-10 13:09:25 +03:00
|
|
|
// QueryContext implements driver.StmtQueryContext StmtQueryContext
|
|
|
|
func (w *wrapperStmt) QueryContext(ctx context.Context, 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 14:32:27 +03:00
|
|
|
|
2023-01-16 23:26:58 +03:00
|
|
|
if w.ctx != nil {
|
2023-09-01 14:32:27 +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 14:32:27 +03:00
|
|
|
nctx, span = w.opts.Tracer.Start(ctx, "sdk.database", tracer.WithSpanKind(tracer.SpanKindClient))
|
2023-01-16 23:26:58 +03:00
|
|
|
}
|
2023-09-01 14:32:27 +03:00
|
|
|
span.AddLabels("db.method", "QueryContext")
|
2023-01-05 16:03:21 +03:00
|
|
|
name := getQueryName(ctx)
|
2023-09-01 14:32:27 +03:00
|
|
|
span.AddLabels("db.statement", name)
|
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 14:32:27 +03:00
|
|
|
span.AddLabels("db.args", fmt.Sprintf("%v", namedValueToLabels(args)))
|
|
|
|
}
|
|
|
|
if id, ok := ctx.Value(requestid.XRequestIDKey).(string); ok {
|
|
|
|
span.AddLabels("x-request-id", id)
|
2022-12-25 15:35:05 +03:00
|
|
|
}
|
2023-01-06 23:24:39 +03:00
|
|
|
labels := []string{labelMethod, "QueryContext", labelQuery, name}
|
2023-06-10 13:09:25 +03:00
|
|
|
if conn, ok := w.stmt.(driver.StmtQueryContext); ok {
|
2023-01-06 23:24:39 +03:00
|
|
|
ts := time.Now()
|
2023-06-10 13:09:25 +03:00
|
|
|
rows, err := conn.QueryContext(nctx, 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-06 23:24:39 +03:00
|
|
|
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelFailure)...).Inc()
|
2023-09-01 14:32:27 +03:00
|
|
|
span.SetStatus(tracer.SpanStatusError, err.Error())
|
2023-01-06 23:24:39 +03:00
|
|
|
} 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 14:32:27 +03:00
|
|
|
w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "QueryContext", name, td, err)).Log(ctx, w.opts.LoggerLevel)
|
2022-12-25 15:35:05 +03:00
|
|
|
}
|
2021-10-27 18:49:33 +03:00
|
|
|
return rows, err
|
|
|
|
}
|
2023-06-10 13:09:25 +03:00
|
|
|
|
2021-10-27 18:49:33 +03:00
|
|
|
values, err := namedValueToValue(args)
|
|
|
|
if err != nil {
|
2023-01-06 23:24:39 +03:00
|
|
|
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelFailure)...).Inc()
|
|
|
|
|
2023-09-01 14:32:27 +03:00
|
|
|
span.SetStatus(tracer.SpanStatusError, err.Error())
|
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 14:32:27 +03:00
|
|
|
w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "QueryContext", name, 0, 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-06 23:24:39 +03:00
|
|
|
ts := time.Now()
|
2023-06-10 13:09:25 +03:00
|
|
|
rows, err := w.Query(values) // nolint:staticcheck
|
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-06 23:24:39 +03:00
|
|
|
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelFailure)...).Inc()
|
2023-09-01 14:32:27 +03:00
|
|
|
span.SetStatus(tracer.SpanStatusError, err.Error())
|
2023-01-06 23:24:39 +03:00
|
|
|
} 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 14:32:27 +03:00
|
|
|
w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "QueryContext", name, td, err)).Log(ctx, w.opts.LoggerLevel)
|
2022-12-25 15:35:05 +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
|
|
|
}
|